jsp中文件上传的实现

<%@ page import="java.util.*"%>
<%@ page import="java.io.File"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="org.apache.commons.fileupload.FileUploadBase"%>
<%@ page import="org.apache.commons.fileupload.FileUploadException"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="com.khnt.cdsp.xzxk.Import"%>
<%@ page import="com.khnt.pub.MemsUser" %>
<%@ page import="com.khnt.frame.foundation.user.User" %>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
 String resultMsg = "";
 String filePath = "";
 MemsUser user = (MemsUser) session.getAttribute(User.NAME);
 String aCode = user.getAreacode();
 String paths = request.getParameter("path");
 String path = pageContext.getServletContext().getRealPath("/") + "files/" + paths + "/";
 
 try
 {
        // 1. 上传文件
        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(4*1024);
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(1000*1024*1024);
        List itemList = null;
        try {
           itemList = upload.parseRequest(request);
        }catch (FileUploadBase.SizeLimitExceededException e){  
           // 请求数据的size超出了规定的大小.
           e.printStackTrace();
           response.getWriter().print("{success:false,errors:'请求数据的size超出了规定的大小'}");
           return;
        }catch (FileUploadBase.InvalidContentTypeException e){  
     // 无效的请求类型,即请求类型enctype != "multipart/form-data"  
           response.getWriter().print("{success:false,errors:'请求类型enctype != multipart/form-data'}");
           return;
        }catch (FileUploadException e){  
        // 如果都不是以上子异常,则抛出此总的异常,出现此异常原因无法说明. 
           response.getWriter().print("{success:false,errors:'上传过程异常,导致其原因可能是网速过慢或者其它原因'}");
           return;
        }
  
        if(itemList != null){
           Iterator it = itemList.iterator();
           while(it.hasNext())
           {
             FileItem item =(FileItem) it.next();
             if(!item.isFormField())
             {
                  String totalName = item.getName();
                  String name = "temp";
                  if(totalName != ""){
                     int index = totalName.lastIndexOf("\\");
                     name = totalName.substring(index+1);
                     //System.out.println(name);
                  }else{
                     name = "temp";
                  }
                  try {
                     filePath = path + name;
                     //System.out.println("-------------------" + path +name);
                     item.write(new File(filePath));
                  } catch (Exception e) {
                     e.printStackTrace();
                     response.getWriter().print("{success:false,errors:'写入磁盘错误'}");
                     return;
                 }
             }
            }
           }
           else{
             response.getWriter().print("{success:false,errors:'上传数据空'}");
             return;
          }
         
        // 2. 导入数据(操作数据库)
  resultMsg = Import.impExcel(filePath,aCode,user.getUserId());
  
  // 3. 返回结果
  out.print(resultMsg);
  //response.getWriter().print("{success:true,msg:'导入成功!'}");
 }
 catch (Exception e)
 {
  e.printStackTrace();
  out.print(resultMsg);
  //response.getWriter().print("{success:false,errors:'导入失败,请检查文件格式是否正确'}");
 }
%>

你可能感兴趣的:(java,jsp,文件下载)