jsp文件上传大多采用采用开源项目来简化处理,这里列出常用的两个jar包的实现,并进行比较,说明他们的优缺点和应该注意的问题。 Commons FileUpload,可以在http://jakarta.apache.org/commons/fileupload/下载,这个包需要Commons IO的支持,可以在http://jakarta.apache.org/commons/io/下载 com.oreilly.servlet,可以在http://www.servlets.com/cos/下载 com.oreilly.servlet也提供了三种文件上传的处理方式,MultipartWrapper、MultipartRequest和MultipartParser三种方式,其中MultipartWrapper和MultipartRequest的用法基本相同,并且没有MultipartRequest提供的操作多,所以这里介绍MultipartRequest,MultipartParser和前两者有些不同,可以用来处理某些特殊情况,例如表单中有两个同名的文件上传选择框。 我们暂时称三面三种文件上传方式分别为:ServletFileUpload方式(MultipartTestServlet)、MultipartRequest方式(MultipartTestServlet2)、MultipartParser方式(MultipartTestServlet3) 代码如下:
<%
@ page language = " java " import = " java.util.* " contentType = " text/html;charset=gbk " pageEncoding = " gbk "
%>
< html > < body > < form action ="MultipartTestServlet" enctype ="multipart/form-data" method ="post" > < input type ="text" name ="username" />< br /> < input type ="file" name ="myfile" />< br /> < input type ="file" name ="myfile" />< br /> < input type ="submit" /> </ form > < br />< br />< br />< br /> < form action ="MultipartTestServlet2" enctype ="multipart/form-data" method ="post" > < input type ="text" name ="username" />< br /> < input type ="file" name ="myfile" />< br /> < input type ="file" name ="myfile" />< br /> < input type ="submit" /> </ form > < br />< br />< br />< br /> < form action ="MultipartTestServlet3" enctype ="multipart/form-data" method ="post" > < input type ="text" name ="username" />< br /> < input type ="file" name ="myfile" />< br /> < input type ="file" name ="myfile" />< br /> < input type ="submit" /> </ form > </ body > </ html > MultipartTestServlet.java
package
com.bug.servlet;
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.RequestContext; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletRequestContext; public class MultipartTestServlet extends HttpServlet { public MultipartTestServlet() { super (); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( " gbk " ); RequestContext requestContext = new ServletRequestContext(request); if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository( new File( " c:/tmp/ " )); ServletFileUpload upload = new ServletFileUpload(factory); // upload.setHeaderEncoding("gbk"); upload.setSizeMax( 2000000 ); List items = new ArrayList(); try { items = upload.parseRequest(request); } catch (FileUploadException e1) { System.out.println( " 文件上传发生错误 " + e1.getMessage()); } Iterator it = items.iterator(); while (it.hasNext()) { FileItem fileItem = (FileItem) it.next(); if (fileItem.isFormField()) { System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + new String(fileItem.getString().getBytes( " iso8859-1 " ), " gbk " )); } else { System.out.println(fileItem.getFieldName() + " " + fileItem.getName() + " " + fileItem.isInMemory() + " " + fileItem.getContentType() + " " + fileItem.getSize()); if (fileItem.getName() != null && fileItem.getSize() != 0 ) { File fullFile = new File(fileItem.getName()); File newFile = new File( " c:/temp/ " + fullFile.getName()); try { fileItem.write(newFile); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println( " 文件没有选择 或 文件内容为空 " ); } } } } } } MultipartTestServlet2.java
package
com.bug.servlet;
import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.oreilly.servlet.MultipartRequest; import com.oreilly.servlet.multipart.DefaultFileRenamePolicy; public class MultipartTestServlet2 extends HttpServlet { public MultipartTestServlet2() { super (); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // request.setCharacterEncoding("gbk"); 不起作用 System.out.println( " start " ); MultipartRequest multi = new MultipartRequest(request, " c:/tmp/ " , 2 * 1024 * 1024 , " gbk " , new DefaultFileRenamePolicy()); System.out.println( " start " ); Enumeration filesName = multi.getFileNames(); Enumeration paramsName = multi.getParameterNames(); while (paramsName.hasMoreElements()) { String paramName = (String) paramsName.nextElement(); System.out.println(multi.getParameter(paramName)); } while (filesName.hasMoreElements()) { String fileName = (String) filesName.nextElement(); System.out.println(multi.getFilesystemName(fileName) + " " + multi.getOriginalFileName(fileName) + " " + multi.getContentType(fileName) + " " ); if (multi.getFilesystemName(fileName) != null && ! multi.getFilesystemName(fileName).equals( "" )) System.out.println(multi.getFile(fileName).toURI()); } } } MultipartTestServlet3.java
package
com.bug.servlet;
import java.io.File; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.oreilly.servlet.multipart.FilePart; import com.oreilly.servlet.multipart.MultipartParser; import com.oreilly.servlet.multipart.ParamPart; import com.oreilly.servlet.multipart.Part; public class MultipartTestServlet3 extends HttpServlet { public MultipartTestServlet3() { super (); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { MultipartParser mp = new MultipartParser(request, 2 * 1024 * 1024 , false , false , " gbk " ); Part part; while ((part = mp.readNextPart()) != null ) { String name = part.getName(); if (part.isParam()) { ParamPart paramPart = (ParamPart) part; String value = paramPart.getStringValue(); System.out.println( " param: name= " + name + " ; value= " + value); } else if (part.isFile()) { // it's a file part FilePart filePart = (FilePart) part; String fileName = filePart.getFileName(); if (fileName != null ) { long size = filePart.writeTo( new File( " c:/tmp/ " )); System.out.println( " file: name= " + name + " ; fileName= " + fileName + " , filePath= " + filePart.getFilePath() + " , contentType= " + filePart.getContentType() + " , size= " + size); } else { System.out.println( " file: name= " + name + " ; EMPTY " ); } System.out.flush(); } } } } web.xml中加入
<
servlet
>
< servlet-name > MultipartTestServlet </ servlet-name > < servlet-class > com.bug.servlet.MultipartTestServlet </ servlet-class > </ servlet > < servlet > < servlet-name > MultipartTestServlet2 </ servlet-name > < servlet-class > com.bug.servlet.MultipartTestServlet2 </ servlet-class > </ servlet > < servlet > < servlet-name > MultipartTestServlet3 </ servlet-name > < servlet-class > com.bug.servlet.MultipartTestServlet3 </ servlet-class > </ servlet > < servlet-mapping > < servlet-name > MultipartTestServlet </ servlet-name > < url-pattern > /MultipartTestServlet </ url-pattern > </ servlet-mapping > < servlet-mapping > < servlet-name > MultipartTestServlet2 </ servlet-name > < url-pattern > /MultipartTestServlet2 </ url-pattern > </ servlet-mapping > < servlet-mapping > < servlet-name > MultipartTestServlet3 </ servlet-name > < url-pattern > /MultipartTestServlet3 </ url-pattern > </ servlet-mapping > 问题1、中文问题: 参考: |