檔案的上傳(FileItem)
阿新 • • 發佈:2018-11-04
加入兩個jar包
commons-fileupload-1.2.1.jar
commons-io-2.0.jar
package com.greatest.flie.servlet; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.util.List; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; 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.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; /** * Servlet implementation class FileServlet */ @WebServlet("/uploadServlet") public class FileUploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 建立基於磁碟的檔案項的工廠 //FileItemFactory factory = new DiskFileItemFactory(); // 新建一個檔案上傳處理程式 //ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request //> 複雜的方式: 可以為檔案的上傳加入一些限制條件和其他的屬性 // 建立基於磁碟的檔案項的工廠 DiskFileItemFactory factory1 = new DiskFileItemFactory(); FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(getServletContext()); factory1.setFileCleaningTracker(fileCleaningTracker); //設定記憶體中最多可以存放的上傳檔案的大小, 若超出則把檔案寫到一個臨時資料夾中. 以 byte 為單位 factory1.setSizeThreshold(1024 * 500); //設定那個臨時資料夾 File tempDirectory = new File("d:\\tempDirectory"); factory1.setRepository(tempDirectory); // 新建一個檔案上傳處理程式 ServletFileUpload upload1 = new ServletFileUpload(factory1); //設定上傳檔案的總的大小. 也可以設定單個檔案的大小. upload1.setSizeMax(1024 * 1024 * 50); // 解析請求 // 1. 得到 FileItem 的集合 items // Parse the request try { List <FileItem>/* FileItem */ items1= upload1.parseRequest(request); // 2. 遍歷 items: for (FileItem item : items1) { // 若是一個一般的表單域, 列印資訊 if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); System.out.println(name + ": " + value); } /* flie commons-beanutils-1.8.0(1).jar application/octet-stream 231320 d:\files\commons-beanutils-1.8.0(1).jar desc: Zxc getName方法用於獲得檔案上傳欄位中的檔名。 注意IE或FireFox中獲取的檔名是不一樣的,IE中是絕對路徑,FireFox中只是檔名。 * */ // 若是檔案域則把檔案儲存到 d:\\files 目錄下. else { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); long sizeInBytes = item.getSize(); System.out.println(fieldName); System.out.println(fileName); System.out.println(contentType); System.out.println(sizeInBytes); InputStream in = item.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; fileName = "d:\\files\\" + fileName; System.out.println(fileName); OutputStream out = new FileOutputStream(fileName); while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); in.close(); } } } catch (FileUploadException e) { e.printStackTrace(); } // InputStream f=request.getInputStream(); // Reader in=new InputStreamReader(f); // BufferedReader b=new BufferedReader(in); // String str=null; // while((str=b.readLine())!=null){ // System.out.println(str); // } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="uploadServlet" method="post" enctype="multipart/form-data"> File:<input type="file" name="flie"/> <br><br> desc: <input type="text" name="desc"/> <br><br> <input type="submit" value="Submit"/> </form> </body> </html>