commons-upload使用

其中有文件大小与文件的格式处理。 

<%@ page contentType="text/html; charset=utf-8" language="java" 

org.apache.commons.fileupload.disk.DiskFileItemFactory,

java.io.File,

org.apache.commons.fileupload.servlet.ServletFileUpload,

java.io.PrintWriter,org.apache.commons.fileupload.FileUploadException,

org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException,

org.apache.commons.fileupload.FileItem"%>

<%request.setCharacterEncoding("utf-8"); %>

<%@ include file="/WEB-INF/jsp/common/common.jsp" %>

<html>

<head>

<title>文件上传处理页面</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<body>

<%

String filepathRoot =FileUploadConstants.getPropValue("BASE_PATH")+FileUploadConstants.getPropValue("EDUCATION_PATH"); // "c:/upload";

final long MAX_SIZE = 12 * 1024 * 1024;// 设置上传文件最大为 3M

// 允许上传的文件格式的列表

final String  allowedExt = "doc,txt,docx,jpg,png,gif,jpeg,pdf,zip";

response.setContentType("text/html");

// 设置字符编码为UTF-8, 这样支持汉字显示

response.setCharacterEncoding("UTF-8");

// 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload

 DiskFileItemFactory dfif = new DiskFileItemFactory();

 dfif.setSizeThreshold(4096);// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘

 dfif.setRepository(new File(request.getRealPath("/")+ "ImagesUploadTemp"));// 设置存放临时文件的目录,web根目录下的ImagesUploadTemp目录

 // 用以上工厂实例化上传组件

 ServletFileUpload sfu = new ServletFileUpload(dfif);

 // 设置最大上传尺寸

 sfu.setSizeMax(MAX_SIZE);

 //PrintWriter printWriter = response.getWriter();

 // 从request得到 所有 上传域的列表

 List fileList = null;

 try {

  fileList = sfu.parseRequest(request);

 } catch (FileUploadException e) {// 处理文件尺寸过大异常

  if (e instanceof SizeLimitExceededException) {

  out.println("文件尺寸超过规定大小:" + MAX_SIZE + "字节<p />");

  out.println("<a href=\"/aicpa/collectInfo.shtm\" target=\"_top\">返回</a>");

    return;

  }

  e.printStackTrace();

 }

 // 没有文件上传

 if (fileList == null || fileList.size() == 0) {

  out.println("请选择上传文件<p />");

  out.println("<a href=\"turnToPage.jsp\" target=\"_top\">返回</a>");

  return;

 } 

List list = new ArrayList();

Random random = new Random();

AicpaUploadInfo aicpaUploadInfo = null;

// 逐一提取上传文件信息,同时可保存文件。

for (int i=0;i<fileList.size();i++)

{

FileItem fileItem = (FileItem)fileList.get(i);

// 忽略简单form字段而不是上传域的文件域(<input type="text" />等)

   if (fileItem == null || fileItem.isFormField()) {

      continue;

   }

// 得到文件的完整路径

   String path = fileItem.getName();

   // 得到去除路径的文件名

   String t_name = path.substring(path.lastIndexOf("\\") + 1);

   // 得到文件的扩展名(无扩展名时将得到全名)

   String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);

String newFileName = System.currentTimeMillis()+random.nextInt()+"."+t_ext ;

// 得到文件的大小

   long size = fileItem.getSize();

   if ("".equals(path) || size == 0) {

   //out.println("请选择上传文件<p />");

   //out.println("<a href=\"upload.html\" target=\"_top\">返回</a>");

   return;

   }

   // 拒绝接受规定文件格式之外的文件类型

   int allowFlag = 0;

   if(allowedExt.indexOf( t_ext.toLowerCase())==-1 ){

    out.println("请上传以下类型的文件<p />");

    out.println("*." + allowedExt+ "&nbsp;&nbsp;&nbsp;");

    out.println("<p /><a href=\"/upload.html\" target=\"_top\">返回</a>");

    return ;

   }

  

   

String newFilePath = filepathRoot+"/" + newFileName;

java.io.File dir = new java.io.File( filepathRoot);

if(!dir.exists() ){

dir.mkdirs();

}

    // 保存文件

   fileItem.write(new File(newFilePath));

//处理业务逻辑

}

request.setAttribute( "list",list);

%>

<c:if test="${not empty list}">

</c:if>

 <%

response.sendRedirect("专页url?upload=success");

%>

</body>

</html>

猜你喜欢

转载自username2.iteye.com/blog/1687723