- commons-fileupload框架源碼解析(一)--實例
- commons-fileupload框架源碼解析(二)--HTTP
- commons-fileupload框架源碼解析(三)--ParseRequest
- commons-fileupload框架源碼解析(四)--FileItemIterator
- commons-fileupload框架源碼解析(五)--MultipartStream
- commons-fileupload框架源碼解析(六)--ParameterParser
- commons-fileupload框架源碼解析(七)--FileCleaningTracker
- commons-fileupload框架源碼解析(八)--DeferredFileOutputStream
前言
commons-fileupload雖然我經(jīng)常會在項目里用的弄贿,它有非常強大的文件上傳功能翎承,雖然經(jīng)常用响鹃,卻不曾了解過其源碼的實現(xiàn)亮垫,為了讓自己更加牛B,更好的掌握文件上傳的原理和具體實現(xiàn)方式谍倦,我開始了長達一個月的解析該框架源碼的工作瘦锹,總算將框架的實現(xiàn)原理給摸清悉患。是時候?qū)懸幌盗胁┛蛠硌bB,comons-fileupload的版本是1.4
第一章簡介
第一章的我并不是馬上講起原理季俩,而是直接我的用例展現(xiàn)出來,我打算通過該用例去解析其原理翎卓。
用例
只是一個普通的JavaWeb項目,代碼的實現(xiàn)都在FileUploadServlet里
目錄結(jié)構(gòu)截圖
項目目錄結(jié)構(gòu).png
pom.xml
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
FileUploadServlet
package bin.study;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;
import java.util.List;
/**
* 實現(xiàn)文件上傳的Servlet
* @author Administrator
*
*/
public class FileUploadServlet extends HttpServlet {
//上傳路徑
private File uploadPath;
//當文件過大時偶房,需要設置一個臨時路徑
private File tempPath;
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 內(nèi)存存儲的最大值
factory.setSizeThreshold(4096);
factory.setRepository(tempPath);
ServletFileUpload upload = new ServletFileUpload(factory);
//設置文件上傳大小
upload.setSizeMax(1000000 * 20);
try {
List fileItems = upload.parseRequest(req);
String itemNo = "";
for (Iterator iter = fileItems.iterator(); iter.hasNext(); ) {
FileItem item = (FileItem) iter.next();
//是普通的表單輸入域
if (item.isFormField()) {
if ("itemNo".equals(item.getFieldName())) {
itemNo = item.getString();
}
}
//是否為input="type"輸入域
if (!item.isFormField()) {
String fileName = item.getName();
long size = item.getSize();
if ((fileName == null || fileName.equals("")) && size == 0) {
continue;
}
//截取字符串 如:C:\WINDOWS\Debug\PASSWD.LOG
fileName = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
//item.write(new File(uploadPath + itemNo + ".gif"));
item.write(new File(uploadPath, itemNo + ".gif"));
}
}
//重定向頁面
res.sendRedirect(req.getContextPath() + "/servlet/item/SearchItemServlet");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化方法趁曼,設定目錄
*/
public void init() throws ServletException {
uploadPath = new File(getServletContext().getRealPath("upload"));
//System.out.println("uploadPath=====" + uploadPath);
//如果目錄不存在
if (!uploadPath.exists()) {
//創(chuàng)建目錄
uploadPath.mkdir();
}
//臨時目錄
tempPath = new File(getServletContext().getRealPath("temp"));
if (!tempPath.exists()) {
tempPath.mkdir();
}
}
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>fileUploadServlet</servlet-name>
<servlet-class>bin.study.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fileUploadServlet</servlet-name>
<url-pattern>/fileUploadServlet</url-pattern>
</servlet-mapping>
</web-app>