Apache Commons FileUpload??????????????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache Commons FileUpload??????????????????相关的知识,希望对你有一定的参考价值。
?????????import blank commons san size ?????? page use ??????
Commons FileUpload??????
Apache Commons???????????????????????????Java??????????????? Apache ?????????Apache Commons??????????????????????????????
1???Commons Proper - ?????????Java?????????????????????
2???The Commons Sandbox - ??????Java???????????????????????????
3???The Commons Dormant - ????????????????????????????????????
Commons-FileUpload???Commons Proper???????????????????????????????????????Commons-IO ???Commons-IO?????? Commons Proper?????????????????????
Commons FileUpload ???JAR?????????
????????????????????????
????????????????????????
FileUpload???????????????html?????????????????????????????????HTTP?????? ??????????????????????????????POST????????????HTTP??????????????????????????????“multipart / form-data”??????FileUpload???????????????????????????????????????????????????
1???????????????POST?????????GET?????????????????????????????????????????????HTTP??????????????????
2???form????????????????????????type="file"???input?????????
3???form?????????enctype?????????????????????????????????????????????????????????????????????????????????????????????multipart/form-data???
enctype??????
enctype?????????????????????application/x-www-form-urlencoded | multipart/form-data | text/plain???
enctype?????????????????????application/x-www-form-urlencoded??????????????????form????????????????????????????????????????????????????????????????????????????????????name=value&name=value&name=value...???
multipart/form-data???????????????????????????????????????????????????????????????????????????????????????????????????form?????????????????????????????????
text/plain???????????????????????? "+" ???????????????????????????????????????
index.jsp????????????
<%@page contentType="text/html; charset=utf-8"%> <!doctype html> <html> <head> <title>apache commons fileupload</title> </head> <body> <form action="${pageContext.request.contextPath }/fileupload" method="post" enctype="multipart/form-data"> <%-- ??????????????? --%> username<input type="text" name="username"><br> <%-- FileItem --%> <%-- ???????????? --%> file1<input type="file" name="file1"><br> <%-- FileItem --%> file2<input type="file" name="file2"><br> <%-- FileItem --%> <input type="submit" value="submit"> </form> </body> </html>
????????????????????????
??????JAR????????????????????????
Servlet??????
package com.wb.controller; import java.io.File; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; 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.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @WebServlet(urlPatterns = {"/fileupload"}) public class FileUpController extends HttpServlet{ private static final long serialVersionUID = 5351212300627874456L; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //????????????“????????????????????????”?????? DiskFileItemFactory factory = new DiskFileItemFactory(); //?????????????????????JVM??????????????????????????????????????????????????????10KB??? factory.setSizeThreshold(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD); //?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????1GB ?????????????????????1MB??? factory.setRepository(new File("E:\TestFileUpload\files")); //?????????????????? ServletFileUpload fileUpload = new ServletFileUpload(factory); //???????????????????????????????????????10MB??? fileUpload.setFileSizeMax(1024*1024*10); //????????????????????????????????? //fileUpload.setHeaderEncoding("UTF-8"); if (ServletFileUpload.isMultipartContent(req)) { try { //???????????? List<FileItem> fileItems = fileUpload.parseRequest(req); for (FileItem fileItem : fileItems) { if(fileItem.isFormField()) {//?????????FileItem??????????????????form?????? //??????????????? String fieldName = fileItem.getFieldName(); //????????????????????????????????? String fieldValue = fileItem.getString("UTF-8"); //String fieldValue = fileItem.getString(); System.out.println(fieldName + ":" + fieldValue); }else {//?????????FileItem??????????????? //??????????????? String fileName = fileItem.getName(); System.out.println("fileName=" + fileName); //?????????????????? long fileSize = fileItem.getSize(); System.out.println("fileSize=" + fileSize); fileItem.write(new File("E:\TestFileUpload\files" + File.separator + fileName)); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } }
以上是关于Apache Commons FileUpload??????????????????的主要内容,如果未能解决你的问题,请参考以下文章
Apache Commons FileUpload实现文件上传
Spring MVC - AngularJS - 文件上传 - org.apache.commons.fileupload.FileUploadException
Apache Commons FileUpload??????????????????
The import org.apache.commons.fileupload cannot be resolved
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory