首页 > 代码库 > commons-fileupload-1.2.1.jar 插件上传与下载
commons-fileupload-1.2.1.jar 插件上传与下载
1:首先在页面上写个文本域:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘upload.jsp‘ starting page</title> </head> <body> <form action="uploadServlet" method="post" enctype="multipart/form-data"> file:<input type="file" name="file"/> 描述:<input type="text" name="desc"/> <input type="submit" value="http://www.mamicode.com/submit"/> </form> </body> </html>
<form action="uploadServlet" method="post" enctype="multipart/form-data">
file:<input type="file" name="file"/>
描述:<input type="text" name="desc"/>
<input type="submit" value="http://www.mamicode.com/submit"/>
</form>
1). 请求方式为 POST: <form action="uploadServlet" method="post" ... >
2). 使用 file 的表单域: <input type="file" name="file"/>
3). 使用 multipart/form-data 的请求编码方式: <form action="uploadServlet" method="post" enctype="multipart/form-data">
2:使用fileupload插件进行获取页面上的信息
package fileupload; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; 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.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1. 得到 FileItem 的集合 items // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints 设置工厂信息
//超过500K就写入到临时文件中
factory.setSizeThreshold(1024 * 500); File tempDirectory = new File("e:\\beforeproject"); factory.setRepository(tempDirectory); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint 设置总的大小 upload.setSizeMax(1024 * 1024 * 5); // Parse the request try { List<FileItem> /* FileItem */items = upload.parseRequest(request); // 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 in = item.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; fileName = "e:\\files\\" + fileName; System.out.println(fileName); OutputStream out = new FileOutputStream(fileName); while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.close(); in.close(); } } } catch (FileUploadException e) { e.printStackTrace(); } } }
II. 基本思想:
> commons-fileupload 可以解析请求, 得到一个 FileItem 对象组成的 List
> commons-fileupload 把所有的请求信息都解析为 FileItem 对象, 无论是一个一般的文本域还是一个文件域.
> 可以调用 FileItem 的 isFormField() 方法来判断是一个 表单域 或不是表单域(则是一个文件域)
> 再来进一步获取信息
if (item.isFormField()) {
String name = item.getFieldName();
String value = http://www.mamicode.com/item.getString();
...
}
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
InputStream uploadedStream = item.getInputStream();
...
uploadedStream.close();
}
III. 如何得到 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(yourMaxMemorySize);
//设置那个临时文件夹
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);
commons-fileupload-1.2.1.jar 插件上传与下载