• java 支持 超大上G , 多附件上传


    首先 确定要上传的目录

     

    WEB.XML 文件

     

    Java代码  收藏代码
    1.         <listener>  
    2.         <listener-class><!-- 临时文件收集器 , 支持超大附件必须项 -->  
    3.           org.apache.commons.fileupload.servlet.FileCleanerCleanup  
    4.         </listener-class>  
    5.     </listener>  
    6.   
    7.     <context-param>  
    8.         <param-name>tempXlsPath</param-name><!-- 要上传的目录 -->  
    9.         <param-value>/WEB-INF/temp/</param-value>  
    10.     </context-param>  
    11.   
    12.   
    13. <servlet>  
    14.  <servlet-name>UploadServlet</servlet-name>  
    15.         <servlet-class>  
    16.             com.thams.web.fileServlet.UploadServlet  
    17.         </servlet-class>  
    18.     </servlet>  
    19.     <servlet-mapping>  
    20.         <servlet-name>UploadServlet</servlet-name>  
    21.         <url-pattern>/servlet/UploadServlet</url-pattern>  
    22.     </servlet-mapping>  
     

     

    servlet

     

    Java代码  收藏代码
    1. package com.thams.web.fileServlet;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import java.util.List;  
    6.   
    7. import javax.servlet.ServletContext;  
    8. import javax.servlet.ServletException;  
    9. import javax.servlet.http.HttpServlet;  
    10. import javax.servlet.http.HttpServletRequest;  
    11. import javax.servlet.http.HttpServletResponse;  
    12.   
    13. import org.apache.commons.fileupload.FileItem;  
    14. import org.apache.commons.fileupload.FileItemFactory;  
    15. import org.apache.commons.fileupload.FileUploadException;  
    16. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
    17. import org.apache.commons.fileupload.servlet.FileCleanerCleanup;  
    18. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
    19. import org.apache.commons.io.FileCleaningTracker;  
    20. import org.apache.commons.io.FilenameUtils;  
    21. import org.apache.log4j.Logger;  
    22.   
    23. /** 
    24.  * @author serv 
    25.  */  
    26. public class UploadServlet extends HttpServlet {  
    27.   
    28.     private static Logger log = Logger.getLogger(UploadServlet.class);  
    29.       
    30.     protected void service(HttpServletRequest request,  
    31.             HttpServletResponse response) throws ServletException, IOException {  
    32.         response.setContentType("text/html;charset=UTF-8");  
    33.         // Check that we have a file upload request  
    34.         boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
    35.         if (!isMultipart) {  
    36.             log.debug("没有可上传的文件, 你想干什么 !");  
    37.             return;  
    38.         }  
    39.         // Create a factory for disk-based file items  
    40.         String tempFilePath = getServletContext().getRealPath("/")+getServletContext().getInitParameter("tempXlsPath");  
    41.         FileItemFactory factory = newDiskFileItemFactory(getServletContext(), new File(tempFilePath));  
    42.         // Create a new file upload handler  
    43.         ServletFileUpload upload = new ServletFileUpload(factory);  
    44.         upload.setHeaderEncoding("UTF-8");  
    45.         /* FileItem */  
    46.         List<FileItem> items = null;  
    47.         try {  
    48.             // Parse the request  
    49.             items = (List<FileItem>) upload.parseRequest(request);  
    50.             String filename = null;  
    51.             for (FileItem fileItem : items) {  
    52.                 if (!fileItem.isFormField()){//file  
    53.                     filename = FilenameUtils.getName(fileItem.getName());  
    54.                     fileItem.write(new File(tempFilePath+filename));  
    55.                     response.getWriter().write(tempFilePath+filename+"<br>");  
    56.                 }  
    57.             }  
    58.         } catch (FileUploadException e) {  
    59.             log.error(e.getMessage());  
    60.         } catch (Exception e) {  
    61.             log.error(e.getMessage());  
    62.         }  
    63.     }  
    64.     /** 
    65.      * The FileCleanerCleanup provides an instance of org.apache.commons.io.FileCleaningTracker.  
    66.      * This instance must be used when creating a org.apache.commons.fileupload.disk.DiskFileItemFactory. 
    67.      */  
    68.     public static DiskFileItemFactory newDiskFileItemFactory(  
    69.             ServletContext context, File repository) {  
    70.         FileCleaningTracker fileCleaningTracker = FileCleanerCleanup  
    71.                 .getFileCleaningTracker(context);  
    72.         DiskFileItemFactory factory = new DiskFileItemFactory(  
    73.                 DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository);  
    74.         factory.setFileCleaningTracker(fileCleaningTracker);  
    75.         return factory;  
    76.     }  
    77.   
    78. }  

     

    测试页面

     

    Java代码  收藏代码
    1. <form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">  
    2.             <input id="file1" name="file1" type="file" class="input400"><br>  
    3.             <input id="file2" name="file2" type="file" class="input400"><br>  
    4.             <input id="file3" name="file3" type="file" class="input400"><br>  
    5.             <input id="file4" name="file4" type="file" class="input400"><br>  
    6.             <input type="submit" id="submit" value="提交" />  
    7.         </form>  
     

    测试结果


    页面输出:


    文件上传完成。

    DEMO下载地址:https://dwz.cn/fgXtRtnu 

  • 相关阅读:
    1323. Maximum 69 Number
    1217. Play with Chips
    1296. Divide Array in Sets of K Consecutive Numbers
    1288. Remove Covered Intervals
    1290. Convert Binary Number in a Linked List to Integer
    1295. Find Numbers with Even Number of Digits
    1306. Jump Game III
    1305. All Elements in Two Binary Search Trees
    1304. Find N Unique Integers Sum up to Zero
    浅谈 JSON.stringify 方法【转】
  • 原文地址:https://www.cnblogs.com/xproer/p/10778097.html
Copyright © 2020-2023  润新知