apache的common-fileupload简单用法实例
1 jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 6 <title>欢迎使用</title> 7 </head> 8 9 <body> 10 <div> 11 <form action="servlet/FileUploadServlet" method="post" enctype="multipart/form-data"> 12 <input type="file" name="file"/> 13 <input type="text" name="desc"/> 14 <input type="submit" name="submit" value="上传"/> 15 </form> 16 </div> 17 </body> 18 </html>
2、Servlet
1 package com.test.upload.servlet; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.UnsupportedEncodingException; 6 import java.util.ArrayList; 7 import java.util.Iterator; 8 import java.util.List; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import org.apache.commons.fileupload.FileItem; 16 import org.apache.commons.fileupload.FileUploadException; 17 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 18 import org.apache.commons.fileupload.servlet.ServletFileUpload; 19 import org.apache.commons.lang.StringUtils; 20 21 22 public class FileUploadServlet extends HttpServlet { 23 24 private static final long serialVersionUID = 6896969016159333196L; 25 private static final int MAX_FILE_SIZE = 10*1024*1024;//10MB 26 public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ 27 doPost(request, response); 28 } 29 30 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException{ 31 try { 32 request.setCharacterEncoding("utf-8"); 33 } catch (UnsupportedEncodingException e2) { 34 e2.printStackTrace(); 35 } 36 String path= "d:"+File.separator+"temp"+File.separator; 37 File file = new File(path); 38 if(!file.exists()){ 39 file.mkdir(); 40 } 41 DiskFileItemFactory factory = new DiskFileItemFactory(); 42 factory.setSizeThreshold(4096); 43 factory.setRepository(file); 44 45 ServletFileUpload upload= new ServletFileUpload(factory); 46 upload.setSizeMax(MAX_FILE_SIZE); 47 48 List items = new ArrayList(); 49 try { 50 items = upload.parseRequest(request); 51 Iterator iterable = items.iterator(); 52 while(iterable.hasNext()){ 53 FileItem fileItem = (FileItem) iterable.next(); 54 55 if(!fileItem.isFormField()){ 56 String tempName = fileItem.getName(); 57 if(!StringUtils.isEmpty(tempName) && fileItem.getSize() > 0){ 58 System.out.println(fileItem.getName()+","+fileItem.getFieldName()); 59 File upFile = new File(path+tempName); 60 fileItem.write(upFile); 61 }else { 62 System.out.println("附件为空"); 63 } 64 } 65 } 66 } catch (FileUploadException e1) { 67 e1.printStackTrace(); 68 } catch (Exception e) { 69 e.printStackTrace(); 70 } 71 } 72 }
3 web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>上传文件</display-name> 4 5 <servlet> 6 <servlet-name>FileUploadServlet</servlet-name> 7 <servlet-class>com.test.upload.servlet.FileUploadServlet</servlet-class> 8 </servlet> 9 <servlet-mapping> 10 <servlet-name>FileUploadServlet</servlet-name> 11 <url-pattern>/servlet/FileUploadServlet</url-pattern> 12 </servlet-mapping> 13 <welcome-file-list> 14 <welcome-file>upload.jsp</welcome-file> 15 </welcome-file-list> 16 17 </web-app>
增加个用jquery.form.js上传
1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 6 <title>欢迎使用</title> 7 </head> 8 <script type="text/javascript" src="js/jquery.js"></script> 9 <script type="text/javascript" src="js/jquery.form.js"></script> 10 <script type="text/javascript"> 11 function submitform(){ 12 var options = { 13 url:'servlet/FileUploadServlet', 14 dataType:"json", 15 type:'post', 16 success: function(){ 17 $('#output1').html("提交成功!").show(); 18 } 19 }; 20 $("#form1").ajaxSubmit(options); 21 return false; 22 } 23 24 25 </script> 26 <body> 27 <div> 28 <form id="form1" action="servlet/FileUploadServlet" method="post" enctype="multipart/form-data" > 29 <input type="file" name="file"/> 30 <input type="text" name="desc"/> 31 <button type="button" name="btn1" onclick="javascript:submitform();">上传</button> 32 </form> 33 </div> 34 <div id="output1"> 35 </div> 36 </body> 37 </html>
jquery form中options参数详见:http://www.malsup.com/jquery/form/#options-object
有关文件上传的有个知识点:enctype
1 application/x-www-form-urlencoded (默认值)
2 multipart/form-data
3 text/plain
有关enctype详细见:http://wenku.baidu.com/view/7d2be4ea81c758f5f61f67fa.html