JavaWeb-使用fileload组件
①搭建环境
commons-fileupload.jar
commons-io.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!-- https: //mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- https: //mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> |
②基本思想
> commons-fileupload可以解析请求,得到一个FileItem对象组成的List
> commons-fileupload把所有的请求信息都解析为FileItem对象。无论是一个一般的文本域还是一个文件域。
> 可以调用FileItem的isFormField()方法来判断是一个表单域或不是表单域(则是一个文件域)
> 再来进一步获取信息
if(item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
...
}
if(!item.isFormField()){
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isTnMemory = item.isInMemory();
long sizeInBytes = items.getSize();
InputStream uploadedStream = item.getInputStream();
...
uploadedStream.close();
}
③如何得到List<FileItem>对象
> 简单的方式
//Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
//Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//Parse the request
List /* FileItem */ items = upload.parseRequest(request);
> 复杂的方式:可以为文件的上传加入一些限制条件和其他的属性
//Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置内存中最多可以存放的上传文件的大小,若超出则把文件写到一个临时文件夹中,以byte为单位
factory.setSizeThreshold(yourTempDirectory);
//设置临时文件夹
factory.setRepository(yourTempDirectory);
//create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//设置上传文件的总的大小,也可以设置单个文件的大小
upload.setSizeMax(yourMaxRequestSize);
//Parse the request
List /* FileItem */ items = upload.parseRequest(request);
Servlet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | public class UploadServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.得到FileItem的集合items //Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); //设置内存中最多可以存放的上传文件的大小,若超出则把文件写到一个临时文件夹中,以byte为单位 factory.setSizeThreshold(1024 * 500); //设置临时文件夹 File yourTempDirectory = new File( "d:\\tempDirectory" ); factory.setRepository(yourTempDirectory); //create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); //设置上传文件的总的大小,也可以设置单个文件的大小 upload.setSizeMax(1024 * 1024 * 5); //Parse the request try { List<FileItem> /* FileItem */ items = upload.parseRequest(req); //2.遍历items for (FileItem item :items ) { // 若是一个一般额表单域,打印信息 if (item.isFormField()){ String name = item.getFieldName(); String value = item.getString(); System. out .println(name + ": " + value); } // 若是文件域则把文件保存到d:\\files目录下 else { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); long sizeInBytes = item.getSize(); System. out .println(fieldName); System. out .println(fileName); System. out .println(contentType); System. out .println(sizeInBytes); InputStream is = item.getInputStream(); byte [] buffer = new byte [1024]; int len = 0; fileName = "d:\\files\\" + fieldName; OutputStream out = new FileOutputStream(fileName); while ((len = is .read(buffer))!= -1){ out .write(buffer,0,len); } is .close(); out .close(); } } } catch (FileUploadException e) { e.printStackTrace(); } } } |
jsp
1 2 3 4 | <form action= "/UploadServlet" method= "post" enctype= "multipart/form-data" > File: <input type= "file" name= "file" > <input type= "submit" value= "Submit" > </form> |
效果:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步