首先 确定要上传的目录
WEB.XML 文件
- <listener>
- <listener-class><!-- 临时文件收集器 , 支持超大附件必须项 -->
- org.apache.commons.fileupload.servlet.FileCleanerCleanup
- </listener-class>
- </listener>
- <context-param>
- <param-name>tempXlsPath</param-name><!-- 要上传的目录 -->
- <param-value>/WEB-INF/temp/</param-value>
- </context-param>
- <servlet>
- <servlet-name>UploadServlet</servlet-name>
- <servlet-class>
- com.thams.web.fileServlet.UploadServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>UploadServlet</servlet-name>
- <url-pattern>/servlet/UploadServlet</url-pattern>
- </servlet-mapping>
servlet
- package com.thams.web.fileServlet;
- import java.io.File;
- import java.io.IOException;
- import java.util.List;
- import javax.servlet.ServletContext;
- 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.FileItemFactory;
- import org.apache.commons.fileupload.FileUploadException;
- import org.apache.commons.fileupload.disk.DiskFileItemFactory;
- import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import org.apache.commons.io.FileCleaningTracker;
- import org.apache.commons.io.FilenameUtils;
- import org.apache.log4j.Logger;
- /**
- * @author serv
- */
- public class UploadServlet extends HttpServlet {
- private static Logger log = Logger.getLogger(UploadServlet.class);
- protected void service(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- // Check that we have a file upload request
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);
- if (!isMultipart) {
- log.debug("没有可上传的文件, 你想干什么 !");
- return;
- }
- // Create a factory for disk-based file items
- String tempFilePath = getServletContext().getRealPath("/")+getServletContext().getInitParameter("tempXlsPath");
- FileItemFactory factory = newDiskFileItemFactory(getServletContext(), new File(tempFilePath));
- // Create a new file upload handler
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setHeaderEncoding("UTF-8");
- /* FileItem */
- List<FileItem> items = null;
- try {
- // Parse the request
- items = (List<FileItem>) upload.parseRequest(request);
- String filename = null;
- for (FileItem fileItem : items) {
- if (!fileItem.isFormField()){//file
- filename = FilenameUtils.getName(fileItem.getName());
- fileItem.write(new File(tempFilePath+filename));
- response.getWriter().write(tempFilePath+filename+"<br>");
- }
- }
- } catch (FileUploadException e) {
- log.error(e.getMessage());
- } catch (Exception e) {
- log.error(e.getMessage());
- }
- }
- /**
- * The FileCleanerCleanup provides an instance of org.apache.commons.io.FileCleaningTracker.
- * This instance must be used when creating a org.apache.commons.fileupload.disk.DiskFileItemFactory.
- */
- public static DiskFileItemFactory newDiskFileItemFactory(
- ServletContext context, File repository) {
- FileCleaningTracker fileCleaningTracker = FileCleanerCleanup
- .getFileCleaningTracker(context);
- DiskFileItemFactory factory = new DiskFileItemFactory(
- DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, repository);
- factory.setFileCleaningTracker(fileCleaningTracker);
- return factory;
- }
- }
测试页面
- <form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">
- <input id="file1" name="file1" type="file" class="input400"><br>
- <input id="file2" name="file2" type="file" class="input400"><br>
- <input id="file3" name="file3" type="file" class="input400"><br>
- <input id="file4" name="file4" type="file" class="input400"><br>
- <input type="submit" id="submit" value="提交" />
- </form>
测试结果
页面输出:
文件上传完成。
DEMO下载地址:https://dwz.cn/fgXtRtnu