JSP Servlet学习笔记——使用fileupload上传文件

关键代码如下:

index.jsp

 

  1. <body>  
  2.   <center>  
  3.     <h3>文件上传</h3>  
  4.     <font color="red"><%=request.getAttribute("msg")==null?"":request.getAttribute("msg") %</font>  
  5.     <form enctype="multipart/form-data" method="post" action="servlet/FileUploadServlet">  
  6.         文件描述:<input type="text" name="text" /><br/>  
  7.         选择文件:<input type="file" name="file1" /><br/>  
  8.         选择文件:<input type="file" name="file2" /><br/>  
  9.         选择文件:<input type="file" name="file3" /><br/>  
  10.         选择文件:<input type="file" name="file4" /><br/>  
  11.         选择文件:<input type="file" name="file5" /><br/>  
  12.         <input type="submit" value="上传" />  
  13.     </form>  
  14.   </center>  
  15. </body>  


FileUploadServlet.jsp

 

 

    1. package com.accp.fileupload.servlet;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import java.util.List;  
    6.   
    7. import javax.servlet.ServletException;  
    8. import javax.servlet.http.HttpServlet;  
    9. import javax.servlet.http.HttpServletRequest;  
    10. import javax.servlet.http.HttpServletResponse;  
    11.   
    12. import org.apache.commons.fileupload.FileItem;  
    13. import org.apache.commons.fileupload.FileUploadException;  
    14. import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;  
    15. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
    16. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
    17.   
    18. public class FileUploadServlet extends HttpServlet {  
    19.   
    20.     private static final long serialVersionUID = 2827297299439162553L;  
    21.   
    22.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
    23.             throws ServletException, IOException {  
    24.         //使用同一种处理方法  
    25.         doPost(request, response);  
    26.     }  
    27.   
    28.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
    29.             throws ServletException, IOException {  
    30.         request.setCharacterEncoding("UTF-8");  
    31.         response.setContentType("text/html; charset=UTF-8");  
    32.           
    33.         //保存路径  
    34.         String savePath = getServletContext().getRealPath("/upload");  
    35.         File saveDir = new File(savePath);  
    36.         // 如果目录不存在,就创建目录  
    37.         if(!saveDir.exists()){  
    38.             saveDir.mkdir();  
    39.         }  
    40.           
    41.         // 创建文件上传核心类  
    42.         DiskFileItemFactory factory = new DiskFileItemFactory();  
    43.         ServletFileUpload sfu = new ServletFileUpload(factory);  
    44.         //设置编码  
    45.         sfu.setHeaderEncoding("UTF-8");  
    46.         // 设置上传的单个文件的最大字节数为2M  
    47.         sfu.setFileSizeMax(1024*1024*2);  
    48.         //设置整个表单的最大字节数为10M  
    49.         sfu.setSizeMax(1024*1024*10);  
    50.           
    51.         try{  
    52.             // 处理表单请求  
    53.             List<FileItem> itemList = sfu.parseRequest(request);  
    54.             for (FileItem fileItem : itemList) {  
    55.                 // 对应表单中的控件的name  
    56.                 String fieldName = fileItem.getFieldName();  
    57.                 System.out.println("控件名称:" + fieldName);  
    58.                 // 如果是普通表单控件  
    59.                 if(fileItem.isFormField()){  
    60.                     String value = fileItem.getString();  
    61.                     //重新编码,解决乱码  
    62.                     value = new String(value.getBytes("ISO-8859-1"),"UTF-8");  
    63.                     System.out.println("普通内容:" + fieldName + "=" + value);  
    64.                 // 上传文件  
    65.                 }else{  
    66.                     // 获得文件大小  
    67.                     Long size = fileItem.getSize();  
    68.                     // 获得文件名  
    69.                     String fileName = fileItem.getName();  
    70.                     System.out.println("文件名:"+fileName+"\t大小:" + size + "byte");  
    71.                       
    72.                     //设置不允许上传的文件格式  
    73.                     if(fileName.endsWith(".exe")){  
    74.                         request.setAttribute("msg", "不允许上传的类型!");  
    75.                     }else{  
    76.                         //将文件保存到指定的路径  
    77.                         File file = new File(savePath,fileName);  
    78.                         fileItem.write(file);  
    79.                         request.setAttribute("msg", "上传成功!");  
    80.                     }  
    81.                 }  
    82.             }  
    83.         }catch(FileSizeLimitExceededException e){  
    84.             request.setAttribute("msg", "文件太大");  
    85.         }catch(FileUploadException e){  
    86.             e.printStackTrace();  
    87.         }catch(Exception e){  
    88.             e.printStackTrace();  
    89.         }  
    90.         //上传完毕后  转发到首页  
    91.         request.getRequestDispatcher("/index.jsp").forward(request, response);  
    92.     }  
    93.   
    94. }  
posted @ 2015-09-21 13:46  郑文亮  阅读(495)  评论(0编辑  收藏  举报