基于表单的文件上传 2010-04-05 21:13:27
- 如果在表单中使用表单元素 <input type=“file” />,浏览器在解析表单时,会自动生成一个输入框和一个按钮,输入框可供用户填写本地文件的文件名和路径名,按钮可以让浏览器打开一个文件选择框供用户选择文件:
-
-
-
- 当表单需要上传文件时,需指定表单 enctype 的值为 multipart/form-data
-
- 在 form 元素的语法中,enctype 属性指定将数据发送到服务器时浏览器使用的编码类型。
-
- enctype 属性取值:
-
- application/x-www-form-urlencoded:表单 enctype 属性的默认值。这种编码方案使用有限的字符集,当使用了非字母和数字时,必须用”%HH”代替(H 代表十六进制数字)。对于大容量的二进制数据或包含非 ASCII 字符的文本来说,这种编码不能满足要求。
-
- multipart/form-data:form 设定了enctype=“multipart/form-data”属性后,表示表单以二进制传输数据 .
-
-
-
- Commons-fileupload 组件上传的基本原理
-
- FileUpload组件将页面提交的所有元素(普通form表单域,如text和文件域file)都看作一样的FileItem,这样上传页面提交的 request请求也就是一个FileItem的有序组合,FileUpload组件可以解析该request,并返回一个一个的FileItem。而对每一个FileItem,FileUpload组件可以判断出它是普通form表单域还是文件file域,从而根据不同的类型,采取不同的操作--如果是表单域,就读出其值,如果是文件域,就保存文件到服务器硬盘上或者内存中
-
-
-
- Commons-fileupload 组件API
-
- ServletFileUpload 负责处理上传的文件数据,并将每部分的数据封装成一到 FileItem 对象中。
-
- DiskFileItemFactory 是创建 FileItem 对象的工厂,在这个工厂类中可以配置内存缓冲区大小和存放临时文件的目录。
-
- ServletFileUpload 在接收上传文件数据时,会将内容保存到内存缓存区中,如果文件内容超过了 DiskFileItemFactory 指定的缓冲区的大小,那么文件将被保存到磁盘上,存储为 DiskFileItemFactory 指定目录中的临时文件。等文件数据都接收完毕后,ServletUpload 在从文件中将数据写入到上传文件目录下的文件中
-
-
-
-
-
- 进行文件上传和下载的具体操作:
-
- 首先,加载必要的jar包:“commons-fileupload-1.2.1.jar”,“commons-io-1.4.jar”
-
- 步骤:
-
- 0. 创建 FileItemFactory 子类 DiskFileItemFactory 的对象
-
- 1.为了得到 ServletFileUpload 对象, 先需要得到 FileItemFactory 的一个对象, 然后调用 new ServletFileUpload(FileItemFactory fif); 方法得到 ServletFileUpload 对象
-
- 2.为了得到 FileItem 的集合, 先需要得到 ServletFileUpload 对象, 然后调用该对象的 parseRequest() 方法得到 FileItem 的 List
-
- 3.从 HttpServletRequest 对象中得到 FileItem 的集合
-
- 4. 对上述集合进行遍历:判断是表单域还是文件域
-
-
-
- 下面是对文件上传相关操作的代码示例。
-
-
-
- boolean isMultipart = ServletFileUpload.isMultipartContent(request);
-
-
-
-
-
- if(!isMultipart){
-
- response.getWriter().println("没有文件域");
-
- return;
-
- }
-
-
-
-
-
-
-
-
-
- DiskFileItemFactory itemFactory = new DiskFileItemFactory();
-
-
-
-
-
- ServletFileUpload fileUpload = new ServletFileUpload();
-
- fileUpload.setFileItemFactory(itemFactory);
-
- 设置上传文件的最大大小
-
- fileUpload.setFileSizeMax(1000 * 100);
-
-
-
- String forwardPage = null;
-
-
-
- try {
-
-
-
- List<FileItem> items = fileUpload.parseRequest(request);
-
-
-
- Map<String, String> paramMap = new HashMap<String, String>();
-
- FileItem fileItem = null;
-
-
-
-
-
- Iterator<FileItem> it = items.iterator();
-
- while(it.hasNext()){
-
- FileItem item = it.next();
-
-
-
- if(item.isFormField()){
-
-
-
- 获得表单中该字段的字段名
-
- String fieldName = item.getFieldName();
-
- 获得表单中与上面字段名对应的字段值
-
- String value = item.getString("UTF-8");
-
-
-
- paramMap.put(fieldName, value);
-
- }
-
-
-
- else{
-
- 这里获得了与file对应的FileItem对象
-
- fileItem = item;
-
- }
-
-
-
- }
-
-
-
-
-
-
-
- String path = null;
-
-
-
- path = this.getServletContext().getRealPath("/photoes");
-
-
-
- File photoesDir = new File(path);
-
- if(!photoesDir.exists()){
-
- photoesDir.mkdir();
-
- }
-
-
-
- path = path + "/" + paramMap.get("username");
-
-
-
- File file = new File(path + ".jpg");
-
-
-
-
-
- fileItem.write(file);
-
-
-
- 上传文件表单的两个准备工作
-
- 1. 提交方式改为 POST
-
- 2. enctype 属性的取值改为 multipart/form-data
-
-
-
- 在获取表单值时
-
- 1. 不能使用 request.getParameter() 方法, 因为该方法仅能获取字符串, 对二进制无能为力.
-
- 2. 使用 fileUpload 组件读取上传文件及表单域字段.
-
-
-
- fileUpload 组件的核心思想
-
- fileUpload 组件认为: 所有的表单域(包含文件域和文本域)都是 FileItem 对象.
-
- 步骤:
-
- 1. 获取 FileItem 对象组成的 List
-
- 2. 对 List 进行遍历
-
- 3. 判断每一个 FileItem 对象是表单域 还是 文件域, 以进行不同的处理.
-
-
-
- 详细步骤:
-
- 1. 检查表单的 enctype 是否为 multipart/form-data, 以决定是否使用 FileUpload 组件进行解析请求
-
- 2. 若表单的 enctype 是否为 multipart/form-data, 则需要获取 封装了所有表单域 的对应的 FileItem 对象的 List: List<FileItem> list = null;
-
- 3) 获取 ServletFileUpload 对象, 调用该对象的 public java.util.List parseRequest(javax.servlet.http.HttpServletRequest request)
-
- 方法获取封装 FileItem 的List
-
- 2) 需要使用 ServletFileUpload 的构造器来创建 ServletFileUpload 对象:
-
- ^ 使用任意一个构造器都可以得到 ServletFileUpload 对象, 但都需要先得到 FileItemFactory 对象
-
- * ServletFileUpload upload = new ServletFileUpload();
-
- upload.setFileItemFactory(factory);
-
- * ServletFileUpload upload = new ServletFileUpload(factory);
-
- 1) 创建 FileItemFactory 接口是想类对象: DiskFileItemFactory, 直接创建该对象.
-
- 3. 可以调用 DiskFileItemFactory 的相关方法来对文件上传进行一些优化:
-
- 1) setSizeThreshold(int sizeThreshold):设置使用临时文件夹的大小.
-
- 2) setRepository(): 设置临时文件夹
-
- 4. 可以调用 ServletUpload 的方法进行文件上传的一些控制
-
- 1) setFileSizeMax: 设置单个文件的最大上传值
-
- 2) setSizeMax:设置总上传文件的最大值, 以 byte 为单位.
-
- 5. 关于中文乱码的问题:
-
- 1) 在解析 request 之前: request.setCharacterEncoding("UTF-8"); 可以解决文件域的乱码问题
-
- 2) 在解析表单域时需要使用: new String(value.getBytes("iso-8859-1"), "UTF-8"); 解决中文乱码问题.
-
- 但是有时候,即使我们按照上面方式设置了,可以还是中文乱码,这个时候问题多半就出在tomcat的配置上了,打开tomcat的service.xml文件,在那里面注意下面部分
-
- <Connector port="8989" protocol="HTTP/1.1"
-
- connectionTimeout="20000"
-
- redirectPort="8443" useBodyEncodingForURI="true"/>
-
- 只有加上了红色部分的设置,我们在程序中配置EncodingUrl才能起作用。
-
-
-
- 下面一段代码是文件下载的相关操作
-
-
-
-
-
-
-
- response.setContentType("application/x-msdownload");
-
-
-
-
-
-
-
- response.setHeader("Content-Disposition", "attachment; filename=haha.ppt");
-
-
-
-
-
- OutputStream os = response.getOutputStream();
-
- BufferedOutputStream bos = new BufferedOutputStream(os);
-
-
-
-
-
- InputStream is = null;
-
- is = new FileInputStream("E:\\source\\090515\\javaee\\fileupload\\fileUpload.ppt");
-
- BufferedInputStream bis = new BufferedInputStream(is);
-
-
-
- int length = 0;
-
- byte [] temp = new byte[1 * 1024 * 10];
-
- while((length = bis.read(temp)) != -1){
-
- bos.write(temp, 0, length);
-
- System.out.println("循环操作中...");
-
- }
-
-
-
- bis.close();
-
- bos.close();
-
-
-
- 但是上面的这种方式无法被迅雷之类的下载工具下载,如果希望被迅雷之类的东西下载,就直接这么写就可以了
-
- request.getRequestDispatcher("/data/fileUpload.ppt")
-
- .forward(request, response);
|