java組件commons-fileupload完成文件上傳。本站提示廣大學習愛好者:(java組件commons-fileupload完成文件上傳)文章只能為提供參考,不一定能成為您想要的結果。以下是java組件commons-fileupload完成文件上傳正文
1、所須要的包:
1、commons-fileupload-1.2.1.jar:
下載地址
http://commons.apache.org/downloads/download_fileupload.cgi
2、commons-io-1.4.jar:
下載地址
http://commons.apache.org/downloads/download_io.cgi
2、留意事項:
form表單外面要加上enctype="multipart/form-data"
3、代碼示例
1、jsp代碼:
<%@ 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>upload</title> </head> <body> <form action="uploadServlet" method="post" enctype="multipart/form-data"> <table> <caption>上傳實例</caption> <tr> <td>姓名</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>年紀</td> <td> <input type="text" name="age"> </td> </tr> <tr> <td>照片</td> <td> <input type="file" name="image"> </td> </tr> <tr> <td></td> <td> <input type="submit" value="提交"> </td> </tr> </table> </form> </body> </html>
2、UploadServlet代碼
package servlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Writer; 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.FileUploadException; import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; /** * 上傳servlet * @author lisanlai * */ public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public UploadServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @SuppressWarnings("unchecked") protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // 設置字符編碼為UTF-8, 如許支撐漢字顯示 response.setCharacterEncoding("UTF-8"); Writer o = response.getWriter(); /** * 起首斷定form的enctype是否是multipart/form-data * 同時也斷定了form的提交方法是否是post * 辦法:isMultipartContent(request) */ if(ServletFileUpload.isMultipartContent(request)){ request.setCharacterEncoding("utf-8"); // 實例化一個硬盤文件工場,用來設置裝備擺設上傳組件ServletFileUpload DiskFileItemFactory factory = new DiskFileItemFactory(); //設置文件寄存的暫時文件夾,這個文件夾要真實存在 File fileDir = new File("../webapps/fileupload/tmp/"); if(fileDir.isDirectory() && fileDir.exists()==false){ fileDir.mkdir(); } factory.setRepository(fileDir); //設置最年夜占用的內存 factory.setSizeThreshold(1024000); //創立ServletFileUpload對象 ServletFileUpload sfu = new ServletFileUpload(factory); sfu.setHeaderEncoding("utf-8"); //設置單個文件最年夜值byte sfu.setFileSizeMax(102400000); //一切上傳文件的總和最年夜值byte sfu.setSizeMax(204800000); List<FileItem> items = null; try { items = sfu.parseRequest(request); }catch (SizeLimitExceededException e) { System.out.println("文件年夜小跨越了最年夜值"); } catch(FileUploadException e) { e.printStackTrace(); } //獲得items的迭代器 Iterator<FileItem> iter = items==null?null:items.iterator(); //圖片上傳後寄存的途徑目次 File images = new File("D:/upload/images/"); if(images.exists()==false){ images.mkdirs(); } //迭代items while(iter!=null && iter.hasNext()){ FileItem item = (FileItem) iter.next(); //假如傳過去的是通俗的表單域 if(item.isFormField()){ System.out.print("通俗的表單域:"); System.out.print(new String(item.getFieldName()) + " "); System.out.println(new String(item.getString("UTF-8"))); } //文件域 else if(!item.isFormField()){ System.out.println("源圖片:" + item.getName()); String fileName = item.getName().substring(item.getName().lastIndexOf("\\")); BufferedInputStream in = new BufferedInputStream(item.getInputStream()); //文件存儲在D:/upload/images目次下,這個目次也得存在 BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File(images.getAbsolutePath()+ fileName))); Streams.copy(in, out, true); o.write("文件上傳勝利"); } } }else { System.out.println("表單的enctype 類型毛病"); } } }
3、web.xml
<?xml version="1.0" encoding="UTF-8"?> <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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>UploadTest</display-name> <welcome-file-list> <welcome-file>upload.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>UploadServlet</display-name> <servlet-name>UploadServlet</servlet-name> <servlet-class>servlet.UploadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UploadServlet</servlet-name> <url-pattern>/uploadServlet</url-pattern> </servlet-mapping> </web-app>
本文已被整頓到了《Java上傳操作技能匯總》,迎接年夜家進修浏覽。
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。