Fileupload使用方法!其他实例

按步骤来,不然会出错。。。。。。

1.下载两个文件commons-io和commons-fileupload(直接在Goole搜索这两个关键字,然后直接在apache官方网站下载就行了),然后解压导入他们的jar文件到你的项目中

 

2.随便写一个上传文件的JSP页面,如下:

  

[xhtml]  view plain copy
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ page import="java.util.*" %>  
  3. <html>  
  4.   <head>  
  5.     <title>文件上传测试title>  
  6.   head>  
  7.   <body>  
  8.    
  9.     <form method="post" action="<%=request.getContextPath() %>/servlet/FileUpload" enctype="multipart/form-data" >  
  10.             <p><input type="file" name="myfile1" value="浏览文件" />p>  
  11.             <p><input type="submit" value="上传"/>p>  
  12.     form>  
  13.   body>  
  14. html>  

 

3.文件上传页面好了以后,就是见项目中建一个servlet文件,我这里建的servlet是FileUpload,在web.xml的配置文件是

  

[xhtml]  view plain copy
  1.  <servlet>  
  2.     <servlet-name>FileUploadservlet-name>  
  3.     <servlet-class>com.cneric.blog.FileUploadservlet-class>  
  4.   servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>FileUploadservlet-name>  
  7.     <url-pattern>/servlet/FileUploadurl-pattern>  
  8.   servlet-mapping>  

servlet的代码如下:

[java]  view plain copy
  1. package com.cneric.blog;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.io.PrintWriter;  
  6. import java.util.ArrayList;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9.   
  10. import javax.servlet.RequestDispatcher;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.   
  16. import org.apache.commons.fileupload.DiskFileUpload;  
  17. import org.apache.commons.fileupload.FileItem;  
  18. import org.apache.commons.fileupload.FileItemFactory;  
  19. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  20. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  21.   
  22. public class FileUpload extends HttpServlet {  
  23.   
  24.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  25.             throws ServletException, IOException {  
  26.         doPost(request, response);  
  27.     }  
  28.   
  29.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  30.             throws ServletException, IOException {  
  31.         request.setCharacterEncoding("UTF-8");  
  32.         // 构建上传路径  
  33.         String basePath = getServletContext().getRealPath("/Upload");  
  34.         System.out.println("path=" + basePath);  
  35.   
  36.         // 文件上傳部分  
  37.         boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
  38.   
  39.         if (isMultipart == true) {  
  40.             try {  
  41.                 FileItemFactory factory = new DiskFileItemFactory();  
  42.                 ServletFileUpload upload = new ServletFileUpload(factory);  
  43.   
  44.                 // 得到所有的表单域,它们目前都被当作FileItem  
  45.                 List fileItems = upload.parseRequest(request);  
  46.                 Iterator iter = fileItems.iterator();  
  47.   
  48.                 // 依次处理每个表单域  
  49.                 while (iter.hasNext()) {  
  50.                     FileItem item = (FileItem) iter.next();  
  51.   
  52.                     if (item.isFormField()) {  
  53.                         // 如果item是正常的表单域  
  54.                         String name = item.getFieldName();  
  55.                         String value = item.getString();  
  56.                         System.out.print("表单域名为:" + name + "表单域值为:" + value);  
  57.                     } else {  
  58.                         // 如果item是文件上传表单域  
  59.   
  60.                         // 获得文件名及路径  
  61.                         String fileName = item.getName();  
  62.                         System.out.println(fileName);  
  63.                         if (fileName != null) {  
  64.                             File fullFile = new File(item.getName());  
  65.                             // 如果文件存在则上传  
  66.                             if (fullFile.exists()) {  
  67.                                 File fileOnServer = new File(basePath, fullFile  
  68.                                         .getName());  
  69.                                 item.write(fileOnServer);  
  70.   
  71.                                 System.out.println("文件"  
  72.                                         + fileOnServer.getName() + "上传成功");  
  73.                             }  
  74.                         }  
  75.                     }  
  76.                 }  
  77.             } catch (Exception e) {  
  78.                 e.printStackTrace();  
  79.             }  
  80.         } else {  
  81.             System.out.println("the enctype must be multipart/form-data");  
  82.         }  
  83.   
  84.         // 取得服务器中已有文件的下載列表  
  85.         List fileListInServer = new ArrayList();  
  86.   
  87.         File dir = new File(basePath);  
  88.         String[] children = dir.list();  
  89.         if (children != null) {  
  90.             for (int i = 0; i < children.length; i++) {  
  91.                 fileListInServer.add(children[i]);  
  92.             }  
  93.         }  
  94.         request.setAttribute("downloadList", fileListInServer);  
  95.   
  96.         // 跳回原頁面  
  97.         RequestDispatcher dispatcher = request  
  98.                 .getRequestDispatcher("/fileupload/test.jsp");  
  99.         dispatcher.forward(request, response);  
  100.         return;  
  101.   
  102.     }  
  103.   
  104. }  

 

里要注意上传路径的问题,在本例子中,我是将文件上传到项目的一个目录中,在WebRoot目录下

建立一个Upload目录,用于存放上传的文件,则在代码中构建上传的路径为

String basePath = getServletContext().getRealPath("/Upload");

你可能感兴趣的:(xhtml,servlet,string,upload,null,apache)