日期:2014-05-16  浏览次数:20988 次

Apache-FileUpload1.2.2在JSP中使用实例
1.先下载Apache的commons FileUpload和IO的jar包,并将起引入到WebRoot\WEB-INF\lib目录内

2.建立一个html文件或着jsp文件,代码如下:
<html>
   <head>
      <title>FileUpload</title>
   </head>
   <body>
      <form name="upform" action="./servlet/FileUpload" method="post"
			enctype="multipart/form-data">
         <input type="file" name="file1" id="file1" /><br />
         <input type="submit" value="Submit" /><br />
         <input type="reset" value="重置"/>
      </form>
   </body>
</html>


3.建立一个Servlet,我在这里将servlet命名为FileUpload,代码如下:
【PS:本代码将上传的文件存放在D:\lmis目录下】
package com.howe.lmis.servlet;


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
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.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload extends HttpServlet {

 private static final long serialVersionUID = 1L;

 public FileUpload() {
  super();
 }
 
 public void init() throws ServletException {
  
 }

 public void destroy() {
  super.destroy();  
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  // 设置上传文件最大为 3M
  final long MAX_SIZE = 3 * 1024 * 1024;
  
  
  // 允许上传的文件格式的列表
  final String[] allowedExt = new String[] { "jpg", "jpeg", "gif", "txt",
               "doc", "docx", "mp3", "wma", "m4a" }; 
  
  response.setContentType("text/html;charset=GB18030");
  
  PrintWriter out = response.getWriter();
  
  System.out.println(request.getContentLength());
  System.out.println(request.getContentType()); 

  // 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
  DiskFileItemFactory dfif = new DiskFileItemFactory();
  
  // 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘
  dfif.setSizeThreshold(4096);
  
  // 设置存放临时文件的目录,web根目录下的ImagesUploadTemp目录
  dfif.setRepository(new File("d:\\lmis\\temp"));

  // 用以上工厂实例化上传组件
  ServletFileUpload sfu = new ServletFileUpload(dfif);
  
  // 设置最大上传尺寸
  sfu.setSizeMax(MAX_SIZE);
  
  // 从request得到 所有 上传域的列表
  Iterator<FileItem> fileItr = null;
  
  try {
   List<FileItem> fileList = sfu.parseRequest(request);
   
   // 没有文件上传
   if (fileList == null || fileList.size() == 0) {
    out.println("请选择上传文件<p />");
    out.println("<a href=\"/LMIS011/testFileUpload.jsp\">返回</a>");
    return;
   }
   
   // 得到所有上传的文件
   fileItr = fileList.iterator();
   
   
  } catch (FileUploadException e) {// 处理文件尺寸过大异常
   if (e instanceof SizeLimitExceededException) {
    out.println("文件尺寸超过规定大小:" + MAX_SIZE + "字节<p />");
    out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
    return;
   }
   e.printStackTrace();
  }
  
  // 循环处理所有文件
  while (fileItr.hasNext()) {
   FileItem fileItem = null;
   String path = null;
   long size = 0;
   // 得到当前文件
   fileItem = (FileItem) fileItr.next();
   // 忽略简单form字段而不是上传域的文件域(<input type="text" />等)
   if (fileItem == null || fileItem.isFormField()) {
    continue;
   }
   // 得到文件的完整路径
   path = fileItem.getName();
   // 得到文件的大小
   size = fileItem.getSize();
   if ("".equals(path) || size == 0) {
    out.println("请选择上传文件<p />");
    out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");
    return;
   }