package com.hzml.serve; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; 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.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @WebServlet("/FileUploadServlet") public class FileUploadServlet extends HttpServlet { private String filePath; // 文件存放目錄 private String tempPath; // 臨時文件目錄 // 初始化 public void init() throws ServletException { super.init(); // 從配置文件中得到初始化參數 ServletContext context = getServletContext(); filePath = context.getRealPath("/") + "/hzmlFile"; tempPath = context.getRealPath("/") + "/hzmlFile"; System.out.println("文件存放目錄、臨時文件目錄準備完畢 ..."); } private void process(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ res.setContentType("text/plain;charset=gbk"); PrintWriter pw = res.getWriter(); try{ DiskFileItemFactory diskFactory = new DiskFileItemFactory(); // threshold 極限、臨界值,即硬盤緩存 1M diskFactory.setSizeThreshold(4 * 1024); // repository 貯藏室,即臨時文件目錄 diskFactory.setRepository(new File(tempPath)); ServletFileUpload upload = new ServletFileUpload(diskFactory); // 設置容許上傳的最大文件大小 4M upload.setSizeMax(4 * 1024 * 1024); // 解析HTTP請求消息頭 List fileItems = upload.parseRequest(req); Iterator iter = fileItems.iterator(); while(iter.hasNext()) { FileItem item = (FileItem)iter.next(); if(item.isFormField()) { System.out.println("處理表單內容 ..."); processFormField(item, pw); }else{ System.out.println("處理上傳的文件 ..."); processUploadFile(item, pw); } }// end while() pw.close(); }catch(Exception e){ System.out.println("使用 fileupload 包時發生異常 ..."); e.printStackTrace(); }// end try ... catch ... } public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { process(req, res); } protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { process(req, res); } // 處理表單內容 private void processFormField(FileItem item, PrintWriter pw) throws Exception { String name = item.getFieldName(); String value = item.getString(); pw.println(name + " : " + value + "\r\n"); } // 處理上傳的文件 private void processUploadFile(FileItem item, PrintWriter pw) throws Exception { // 此時的文件名包含了完整的路徑,得注意加工一下 String filename = item.getName(); System.out.println("完整的文件名:" + filename); int index = filename.lastIndexOf("\\"); filename = filename.substring(index + 1, filename.length()); long fileSize = item.getSize(); if("".equals(filename) && fileSize == 0) { System.out.println("文件名爲空 ..."); return; } pw.println(filePath); File uploadFile = new File(filePath + "/" + filename); item.write(uploadFile); pw.println(filename + " 文件保存完畢 ..."); pw.println("文件大小爲 :" + fileSize + "\r\n"); } }