天天看点

Apache Commons fileUpload实现文件上传

Apache鐨刢ommons-fileupload.jar鍙柟渚跨殑瀹炵幇鏂囦欢鐨勪笂浼犲姛鑳斤紝鏈枃閫氳繃瀹炰緥鏉ヤ粙缁嶅浣曚娇鐢╟ommons-fileupload.jar銆? @author:ZJ 07-2-22 Blog: http://zhangjunhd.blog.51cto.com/ 聽聽聽 灏咥pache鐨刢ommons-fileupload.jar鏀惧湪搴旂敤绋嬪簭鐨刉EB-INF/lib涓嬶紝鍗冲彲浣跨敤銆備笅闈妇渚嬩粙缁嶅浣曚娇鐢ㄥ畠鐨勬枃浠朵笂浼犲姛鑳姐?? 鎵?浣跨敤鐨刦ileUpload鐗堟湰涓?2锛岀幆澧冧负Eclipse3.3+MyEclipse6.0銆侳ileUpload 鏄熀浜?Commons IO鐨勶紝鎵?浠ュ湪杩涘叆椤圭洰鍓嶅厛纭畾Commons IO鐨刯ar鍖咃紙鏈枃浣跨敤commons-io-1.3.2.jar锛夊湪WEB-INF/lib涓嬨?? 姝ゆ枃浣滅ず渚嬪伐绋嬪彲鍦ㄦ枃绔犳渶鍚庣殑闄勪欢涓笅杞姐??

绀轰緥 1

鏈?绠?鍗曠殑渚嬪瓙锛岄?氳繃ServletFileUpload闈欐?佺被鏉ヨВ鏋怰equest锛屽伐鍘傜被FileItemFactory浼氬mulipart绫荤殑琛ㄥ崟涓殑鎵?鏈夊瓧娈佃繘琛屽鐞嗭紝涓嶅彧鏄痜ile瀛楁銆俫etName锛堬級寰楀埌鏂囦欢鍚嶏紝getString锛堬級寰楀埌琛ㄥ崟鏁版嵁鍐呭锛宨sFormField锛堬級鍙垽鏂槸鍚︿负鏅?氱殑琛ㄥ崟椤广?? demo1.html

<html> <head> 聽聽聽 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 聽聽聽 <title>File upload</title> </head> <body> 聽聽聽聽聽聽 //蹇呴』鏄痬ultipart鐨勮〃鍗曟暟鎹?? 聽聽聽 <form name="myform" action="demo1.jsp" method="post" 聽聽聽聽聽聽 enctype="multipart/form-data"> 聽聽聽聽聽聽 Your name: <br> 聽聽聽聽聽聽 <input type="text" name="name" size="15"><br> 聽聽聽聽聽聽 File:<br> 聽聽聽聽聽聽 <input type="file" name="myfile"><br> 聽聽聽聽聽聽 <br> 聽聽聽聽聽聽 <input type="submit" name="submit" value="Commit"> 聽聽聽 </form> </body> </html>

demo1.jsp

<%@ page language="java" contentType="text/html; charset=GB18030" 聽聽聽 pageEncoding="GB18030"%> <%@ page import="org.apache.commons.fileupload.*"%> <%@ page import="org.apache.commons.fileupload.servlet.*"%> <%@ page import="org.apache.commons.fileupload.disk.*"%> <%@ page import="java.util.*"%> <!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN"> <% 聽聽聽 boolean isMultipart = ServletFileUpload.isMultipartContent(request);//妫?鏌ヨ緭鍏ヨ姹傛槸鍚︿负multipart琛ㄥ崟鏁版嵁銆? 聽聽聽 if (isMultipart == true ) { 聽聽聽聽聽聽 FileItemFactory factory = new DiskFileItemFactory();//涓鸿璇锋眰鍒涘缓涓?涓狣iskFileItemFactory瀵硅薄锛岄?氳繃瀹冩潵瑙f瀽璇锋眰銆傛墽琛岃В鏋愬悗锛屾墍鏈夌殑琛ㄥ崟椤圭洰閮戒繚瀛樺湪涓?涓狶ist涓?? 聽聽聽聽聽聽 ServletFileUpload upload = new ServletFileUpload(factory); 聽聽聽聽聽聽 List<FileItem> items = upload.parseRequest(request); 聽聽聽聽聽聽 Iterator<FileItem> itr = items.iterator(); 聽聽聽聽聽聽 while (itr.hasNext()) { 聽聽聽聽聽聽聽聽聽聽 FileItem item = (FileItem) itr.next(); 聽聽聽聽聽聽聽聽聽聽 //妫?鏌ュ綋鍓嶉」鐩槸鏅?氳〃鍗曢」鐩繕鏄笂浼犳枃浠躲?? 聽聽聽聽聽聽聽聽聽聽 if (item.isFormField()) {//濡傛灉鏄櫘閫氳〃鍗曢」鐩紝鏄剧ず琛ㄥ崟鍐呭銆? 聽聽聽聽聽聽 String fieldName = item.getFieldName(); 聽聽聽聽聽聽 if (fieldName.equals("name")) //瀵瑰簲demo1.html涓璽ype="text" name="name" 聽聽聽聽聽聽聽聽聽聽 out.print("the field name is" + item.getString());//鏄剧ず琛ㄥ崟鍐呭銆? 聽聽聽聽聽聽 out.print("<br>"); 聽聽聽聽聽聽聽聽聽聽 } else {//濡傛灉鏄笂浼犳枃浠讹紝鏄剧ず鏂囦欢鍚嶃?? 聽聽聽聽聽聽 out.print("the upload file name is" + item.getName()); 聽聽聽聽聽聽 out.print("<br>"); 聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽 } 聽聽聽 } else { 聽聽聽聽聽聽 out.print("the enctype must be multipart/form-data"); 聽聽聽 } %> <html> <head> 聽聽聽 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 聽聽聽 <title>File upload</title> </head> <body> </body> </html>

缁撴灉锛?

the field name isjeff

the upload file name isD:/C璇█鑰冭瘯鏍烽/浣滀笟棰?rar

绀轰緥 2

涓婁紶涓や釜鏂囦欢鍒版寚瀹氱殑鐩綍銆? demo2.html

<html> <head> 聽聽聽 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 聽聽聽 <title>File upload</title> </head> <body> 聽聽聽 <form name="myform" action="demo2.jsp" method="post" 聽聽聽聽聽聽 enctype="multipart/form-data"> 聽聽聽聽聽聽 File1:<br> 聽聽聽聽聽聽 <input type="file" name="myfile"><br> 聽聽聽聽聽聽 File2:<br> 聽聽聽聽聽聽 <input type="file" name="myfile"><br> 聽聽聽聽聽聽 <br> 聽聽聽聽聽聽 <input type="submit" name="submit" value="Commit"> 聽聽聽 </form> </body> </html>

demo2.jsp

<%@ page language="java" contentType="text/html; charset=GB18030" 聽聽聽 pageEncoding="GB18030"%> <%@ page import="org.apache.commons.fileupload.*"%> <%@ page import="org.apache.commons.fileupload.servlet.*"%> <%@ page import="org.apache.commons.fileupload.disk.*"%> <%@ page import="java.util.*"%> <%@ page import="java.io.*"%> <!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN"> <%String uploadPath="D://temp"; 聽 boolean isMultipart = ServletFileUpload.isMultipartContent(request); 聽 if (isMultipart== true ){ 聽聽聽 聽 try { 聽聽聽 聽聽聽 FileItemFactory factory = new DiskFileItemFactory(); 聽聽聽 聽聽聽 ServletFileUpload upload = new ServletFileUpload(factory); 聽聽聽 聽聽聽 List<FileItem> items = upload.parseRequest(request);//寰楀埌鎵?鏈夌殑鏂囦欢 聽聽聽 聽聽 Iterator<FileItem> itr = items.iterator(); 聽聽聽 聽聽聽 while (itr.hasNext()){//渚濇澶勭悊姣忎釜鏂囦欢 聽聽聽聽聽聽 聽 FileItem item=(FileItem)itr.next(); 聽聽聽聽聽聽 聽 String fileName=item.getName();//鑾峰緱鏂囦欢鍚嶏紝鍖呮嫭璺緞 聽聽聽聽聽聽 聽 if (fileName!= null ){ 聽聽聽聽聽聽聽聽聽聽 聽 File fullFile= new File(item.getName()); 聽聽聽聽聽聽聽聽聽聽 聽 File savedFile= new File(uploadPath,fullFile.getName()); 聽聽聽聽聽聽聽聽聽聽 聽 item.write(savedFile); 聽聽聽聽聽聽 聽 } 聽聽聽 聽聽聽 } 聽聽聽 聽聽聽 out.print("upload succeed"); 聽聽聽 聽 } 聽聽聽 聽 catch (Exception e){ 聽聽聽聽聽聽 聽 e.printStackTrace(); 聽聽聽 聽 } 聽 } 聽 else { 聽聽聽 聽 out.println("the enctype must be multipart/form-data"); 聽 } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>File upload</title> </head> <body> </body> </html>

缁撴灉锛?

upload succeed

姝ゆ椂锛屽湪"D:/temp"涓嬪彲浠ョ湅鍒颁綘涓婁紶鐨勪袱涓枃浠躲??

绀轰緥 3

涓婁紶涓?涓枃浠跺埌鎸囧畾鐨勭洰褰曪紝骞堕檺瀹氭枃浠跺ぇ灏忋?? demo3.html

<html> <head> 聽聽聽 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 聽聽聽 <title>File upload</title> </head> <body> 聽聽聽 <form name="myform" action="demo3.jsp" method="post" 聽聽聽聽聽聽 enctype="multipart/form-data"> 聽聽聽聽聽聽 File:<br> 聽聽聽聽聽聽 <input type="file" name="myfile"><br> 聽聽聽聽聽聽 <br> 聽聽聽聽聽聽 <input type="submit" name="submit" value="Commit"> 聽聽聽 </form> </body> </html>

demo3.jsp

<%@ page language="java" contentType="text/html; charset=GB18030" 聽聽聽 pageEncoding="GB18030"%> <%@ page import="org.apache.commons.fileupload.*"%> <%@ page import="org.apache.commons.fileupload.servlet.*"%> <%@ page import="org.apache.commons.fileupload.disk.*"%> <%@ page import="java.util.*"%> <%@ page import="java.io.*"%> <!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN"> <% 聽聽聽 File uploadPath = new File("D://temp");//涓婁紶鏂囦欢鐩綍 聽聽聽 if (!uploadPath.exists()) { 聽聽聽聽聽聽 uploadPath.mkdirs(); 聽聽聽 } 聽聽聽 // 涓存椂鏂囦欢鐩綍 聽聽聽 File tempPathFile = new File("d://temp//buffer//"); 聽聽聽 if (!tempPathFile.exists()) { 聽聽聽聽聽聽 tempPathFile.mkdirs(); 聽聽聽 } 聽聽聽 try { 聽聽聽聽聽聽 // Create a factory for disk-based file items 聽聽聽聽聽聽 DiskFileItemFactory factory = new DiskFileItemFactory(); 聽聽聽聽聽聽 // Set factory constraints 聽聽聽聽聽聽 factory.setSizeThreshold(4096); // 璁剧疆缂撳啿鍖哄ぇ灏忥紝杩欓噷鏄?b 聽聽聽聽聽聽 factory.setRepository(tempPathFile);//璁剧疆缂撳啿鍖虹洰褰? 聽聽聽聽聽聽 // Create a new file upload handler 聽聽聽聽聽聽 ServletFileUpload upload = new ServletFileUpload(factory); 聽聽聽聽聽聽 // Set overall request size constraint 聽聽聽聽聽聽 upload.setSizeMax(4194304); // 璁剧疆鏈?澶ф枃浠跺昂瀵革紝杩欓噷鏄?B 聽聽聽聽聽聽 List<FileItem> items = upload.parseRequest(request);//寰楀埌鎵?鏈夌殑鏂囦欢 聽聽聽聽聽聽 Iterator<FileItem> i = items.iterator(); 聽聽聽聽聽聽 while (i.hasNext()) { 聽聽聽聽聽聽聽聽聽聽 FileItem fi = (FileItem) i.next(); 聽聽聽聽聽聽聽聽聽聽 String fileName = fi.getName(); 聽聽聽聽聽聽聽聽聽聽 if (fileName != null ) { 聽聽聽聽聽聽 File fullFile = new File(fi.getName()); 聽聽聽聽聽聽 File savedFile = new File(uploadPath, fullFile 聽聽聽聽聽聽聽聽聽聽聽聽聽 .getName()); 聽聽聽聽聽聽 fi.write(savedFile); 聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽 } 聽聽聽聽聽聽 out.print("upload succeed"); 聽聽聽 } catch (Exception e) { 聽聽聽聽聽聽 e.printStackTrace(); 聽聽聽 } %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>File upload</title> </head> <body> </body> </html>
绀轰緥 4

鍒╃敤Servlet鏉ュ疄鐜版枃浠朵笂浼犮?? Upload.java

package com.zj.sample; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; 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.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @SuppressWarnings("serial") public class Upload extends HttpServlet { 聽聽聽 private String uploadPath = "D://temp"; // 涓婁紶鏂囦欢鐨勭洰褰? 聽聽聽 private String tempPath = "d://temp//buffer//"; // 涓存椂鏂囦欢鐩綍 聽聽聽 File tempPathFile; 聽聽聽 @SuppressWarnings("unchecked") 聽聽聽 public void doPost(HttpServletRequest request, HttpServletResponse response) 聽聽聽聽聽聽聽聽聽聽 throws IOException, ServletException { 聽聽聽聽聽聽 try { 聽聽聽聽聽聽聽聽聽聽 // Create a factory for disk-based file items 聽聽聽聽聽聽聽聽聽聽 DiskFileItemFactory factory = new DiskFileItemFactory(); 聽聽聽聽聽聽聽聽聽聽 // Set factory constraints 聽聽聽聽聽聽聽聽聽聽 factory.setSizeThreshold(4096); // 璁剧疆缂撳啿鍖哄ぇ灏忥紝杩欓噷鏄?b 聽聽聽聽聽聽聽聽聽聽 factory.setRepository(tempPathFile);// 璁剧疆缂撳啿鍖虹洰褰? 聽聽聽聽聽聽聽聽聽聽 // Create a new file upload handler 聽聽聽聽聽聽聽聽聽聽 ServletFileUpload upload = new ServletFileUpload(factory); 聽聽聽聽聽聽聽聽聽聽 // Set overall request size constraint 聽聽聽聽聽聽聽聽聽聽 upload.setSizeMax(4194304); // 璁剧疆鏈?澶ф枃浠跺昂瀵革紝杩欓噷鏄?B 聽聽聽聽聽聽聽聽聽聽 List<FileItem> items = upload.parseRequest(request);// 寰楀埌鎵?鏈夌殑鏂囦欢 聽聽聽聽聽聽聽聽聽聽 Iterator<FileItem> i = items.iterator(); 聽聽聽聽聽聽聽聽聽聽 while (i.hasNext()) { 聽聽聽聽聽聽聽聽聽聽聽聽聽 FileItem fi = (FileItem) i.next(); 聽聽聽聽聽聽聽聽聽聽聽聽聽 String fileName = fi.getName(); 聽聽聽聽聽聽聽聽聽聽聽聽聽 if (fileName != null ) { 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 File fullFile = new File(fi.getName()); 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 File savedFile = new File(uploadPath, fullFile.getName()); 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 fi.write(savedFile); 聽聽聽聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽聽聽聽 } 聽聽聽聽聽聽聽聽聽聽 System.out.print("upload succeed"); 聽聽聽聽聽聽 } catch (Exception e) { 聽聽聽聽聽聽聽聽聽聽 // 鍙互璺宠浆鍑洪敊椤甸潰 聽聽聽聽聽聽聽聽聽聽 e.printStackTrace(); 聽聽聽聽聽聽 } 聽聽聽 } 聽聽聽 public void init() throws ServletException { 聽聽聽聽聽聽 File uploadFile = new File(uploadPath); 聽聽聽聽聽聽 if (!uploadFile.exists()) { 聽聽聽聽聽聽聽聽聽聽 uploadFile.mkdirs(); 聽聽聽聽聽聽 } 聽聽聽聽聽聽 File tempPathFile = new File(tempPath); 聽聽聽 聽聽聽 if (!tempPathFile.exists()) { 聽聽聽聽聽聽聽聽聽聽 tempPathFile.mkdirs(); 聽聽聽聽聽聽 } 聽聽聽 } }

demo4.html

<html> <head> 聽聽聽 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 聽聽聽 <title>File upload</title> </head> <body> // action="fileupload"瀵瑰簲web.xml涓?lt;servlet-mapping>涓?lt;url-pattern>鐨勮缃? 聽聽聽 <form name="myform" action="fileupload" method="post" 聽聽聽聽聽聽 enctype="multipart/form-data"> 聽聽聽聽聽聽 File:<br> 聽聽聽聽聽聽 <input type="file" name="myfile"><br> 聽聽聽聽聽聽 <br> 聽聽聽聽聽聽 <input type="submit" name="submit" value="Commit"> 聽聽聽 </form> </body> </html>

web.xml

<servlet> 聽聽聽 <servlet-name>Upload</servlet-name> 聽聽聽 <servlet-class>com.zj.sample.Upload</servlet-class> </servlet> <servlet-mapping> 聽聽聽 <servlet-name>Upload</servlet-name> 聽聽聽 <url-pattern>/fileupload</url-pattern> </servlet-mapping>

鏈枃鍑鸿嚜 鈥滃瓙 瀛戔??鍗氬锛岃鍔″繀淇濈暀姝ゅ嚭澶刪ttp://zhangjunhd.blog.51cto.com/113473/18331