common-fileupload 上传单个或者多个文件 示例

时间:2022-09-30 23:10:48

下载地址:google一下

该组件的介绍:google一下

想说的是:

  该组件对中文支持良好,支持单文件与多文件的同时上传

主要类介绍

  DiskFIleItemFactory类 ,该类创建FileItem对象的工厂类,开发人员可以在这个工厂类上配置上传过程中的

内存缓冲区大小和存放临时文件的目录,其中的repository属性主要指定上传文件保存的目录,sizeThreshold属性

则设置在内存中缓冲区大小

  ServletFileUpload类 该类首先负责处理上传的文件的文件数据,最终完成文件上传的功能。在下面代码中,注意该

类使用时会抛出的一些异常。

  FileItem类,该类包装上传表单中的各个请求属性,可以通过其中的isFormField属性访问方法识别某个FileItem类

对象实际代表的是普通表单成员域还是file类型的成员域

 

 

 

具体使用:

JSP页面: 注意form中标红色的部分,该方法以二进制流的方式来处理表单数据,并且把文件域指定的文件的内容也封装进了请求参数里

 

  
  
  
1 <% @ page language = " java " import = " java.util.* " pageEncoding = " utf-8 " %>
2   <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
5   %>
6
7   <! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
8   < html >
9 < head >
10 < base href = " <%=basePath%> " >
11
12 < title > My JSP ' filesUpload.jsp ' starting page </ title >
13
14 </ head >
15
16 < body >
17 < form action = " <%=basePath%>servlet/FileUploadServlet "
        enctype
="multipart/form-data" method = " post " >
18 < input type = " file " name = " file1 " >< br >
19 < input type = " file " name = " file2 " >< br >
20 < input type = " file " name = " file3 " >< br >
21 < input type = " submit " >
22 </ form >
23 </ body >
24   </ html >
25  

 

 

 

Servlet代码

 

  
  
  
1 package experiment4;
2
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.List;
6
7   import javax.servlet.ServletException;
8   import javax.servlet.http.HttpServlet;
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.apache.commons.fileupload.FileItem;
13 import org.apache.commons.fileupload.FileUploadException;
14 import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
15 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
16 import org.apache.commons.fileupload.servlet.ServletFileUpload;
17
18 public class FileUploadServlet extends HttpServlet {
19
20 public void doPost(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22 request.setCharacterEncoding( " utf-8 " ); // 防止中文名乱码
23 int sizeThreshold = 1024 * 6 ; // 缓存区大小
24 String basePath = this .getServletContext().getRealPath( " /upload/ " );
25 File repository = new File(basePath); // 缓存区目录
26 long sizeMax = 1024 * 1024 * 2 ; // 设置文件的大小为2M
27 final String allowExtNames = " jpg,gif,bmp,rar,rar,txt,docx " ;
28 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
29 diskFileItemFactory.setRepository(repository);
30 diskFileItemFactory.setSizeThreshold(sizeThreshold);
31 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
32 servletFileUpload.setSizeMax(sizeMax);
33
34 List < FileItem > fileItems = null ;
35 try {
36 fileItems = servletFileUpload.parseRequest(request);
37
38 for (FileItem fileItem:fileItems){
39 long size = 0 ;
40 String filePath = fileItem.getName();
41 System.out.println(filePath);
42 if (filePath == null || filePath.trim().length() == 0 )
43 continue ;
44 String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1 );
45 // String fileName=String.valueOf(System.currentTimeMillis());
46 String extName = filePath.substring(filePath.lastIndexOf( " . " ) + 1 );
47 // fileName+="."+extName;
48 if (allowExtNames.indexOf(extName) !=- 1 )
49 {
50 try {
51 fileItem.write( new File(basePath + File.separator + fileName));
52 } catch (Exception e) {
53 e.printStackTrace();
54 }
55 }
56 else {
57 throw new FileUploadException( " file type is not allowed " );
58 }
59 }
60 } catch (FileSizeLimitExceededException e){
61 System.out.println( " file size is not allowed " );
62 } catch (FileUploadException e1){
63 e1.printStackTrace();
64 }
65
66
67 }
68
69 }
70