jsp高手帮下忙,谢谢了!
执行页面:<%@ page language="java"
import="java.util.*"
pageEncoding="GBK"%>
<jsp:directive.page import="myjava.mycompany.operate.FileUploadTools;"/>
<%
request.setCharacterEncoding("GBK");
%>
<html>
<head>
<title>康复网图片上传</title>
</head>
<body>
<%
FileUploadTools fut=new FileUploadTools(request,70*1024,this.getServletContext().getRealPath("/")+"uploadtemp");
String pictureName = fut.getParameter("pictureName");
String show = fut.getParameter("show");
List <String> all = fut.saveAll(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator);
%>
<h3><%=pictureName %></h3>
<h3><%=show %></h3>
<%
Iterator <String> iter=all.iterator();
while(iter.hasNext()){
%>
<img src="../upload/<%=iter.next()%>"/>
<%
}
%>
</body>
</html>
类:
package myjava.mycompany.operate;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
public class FileUploadTools {
private HttpServletRequest request=null;
private List<FileItem> items=null;
private Map<String,List<String>> params= new HashMap<String,List<String>>() ;
private Map<String, FileItem> files=new HashMap<String,FileItem>();
public FileUploadTools(HttpServletRequest request,int maxSize,String tempDir)throws Exception{
this.request=request;
DiskFileItemFactory factory=new DiskFileItemFactory();
if(tempDir!=null){
factory.setRepository(new File(tempDir));
}
ServletFileUpload upload=new ServletFileUpload(factory);
if(maxSize>0){
upload.setFileSizeMax(maxSize);
}
this.items=upload.parseRequest(request);
this.init();
}
private void init(){
Iterator<FileItem> iter=this.items.iterator();
IPTimeStamp its=new IPTimeStamp(this.request.getRemoteAddr());
while(iter.hasNext()){
FileItem item=iter.next();
if(item.isFormField()){//普通参数
String name=item.getFieldName();
String value=item.getString();
List<String>temp=null;
if(this.params.containsKey(name)){
temp=this.params.get(name);
}else{
temp=new ArrayList<String>();
}
temp.add(value);
this.params.put(name, temp);
}else{
String fileName=its.getIPTimeRand()+"."+item.getName().split("\\.")[1];
this.files.put(fileName, item);
}
}
}
public String getParameter(String name){//根据参数名称取出内容
String ret=null;
List<String> temp=this.params.get(name);
if(temp!=null){
ret=temp.get(0);
}
return ret;
}
public String[]getParameterValues(String name){
String ret[]=null;
List <String >temp=this.params.get(name);
if(temp!=null){
ret=temp.toArray(new String[]{});
}
return ret;
}
public Map<String,FileItem> getUploadFiles(){
return this.files;
}
//保存之后一定要有名字。
public List<String> saveAll(String saveDir) throws IOException{
List<String> names=new ArrayList<String>();
if(this.files.size()>0){
Set<String> keys=this.files.keySet();
Iterator<String> iter=keys.iterator();
File saveFile=null;
InputStream input=null;
OutputStream out=null;
while(iter.hasNext()){
FileItem item=this.files.get(iter.next());
String fileName=new IPTimeStamp(request.getRemoteAddr()).getIPTimeRand()+"."+item.getName().split("\\.")[1];
saveFile =new File(saveDir+fileName);
names.add(fileName);//名字不返回后期无法操作
try{
input=item.getInputStream();
out=new FileOutputStream(saveFile);
byte data[]=new byte[512];
while((input.read(data,0,512))!=-1){
out.write(data);
}
}catch(IOException e){
throw e;
}finally{
try{
input.close();
out.close();
}catch(IOException ex){
throw ex;
}
}
}
}
return names;
}
}
--------------------编程问答-------------------- 报错:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 14 in the jsp file: /admin/web/KFpictureCheck.jsp
Generated servlet error:
Syntax error on token "<", invalid AssignmentOperator
An error occurred at line: 14 in the jsp file: /admin/web/KFpictureCheck.jsp
Generated servlet error:
Syntax error on token "=", != expected
An error occurred at line: 22 in the jsp file: /admin/web/KFpictureCheck.jsp
Generated servlet error:
Syntax error on token "<", invalid AssignmentOperator
An error occurred at line: 22 in the jsp file: /admin/web/KFpictureCheck.jsp
Generated servlet error:
Syntax error on token "=", != expected
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:386)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:280)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:259)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:247)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.3 logs.
--------------------------------------------------------------------------------
Apache Tomcat/5.5.3 --------------------编程问答-------------------- 500说明你服务器有错,并且提示你14行和20行的路径有误 --------------------编程问答-------------------- IPTimeStamp这个类没有给出代码,同时<jsp:directive.page import="myjava.mycompany.operate.FileUploadTools;">似乎用的也不很好 --------------------编程问答--------------------
赞一个
补充:Java , Java EE