①搭建环境
commons-fileupload.jar
commons-io.jar
<!-- 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:
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
<form action="/UploadServlet" method="post" enctype="multipart/form-data"> File: <input type="file" name="file"> <input type="submit" value="Submit"> </form>
效果: