程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Java開發之文件上傳,java文件上傳

Java開發之文件上傳,java文件上傳

編輯:JAVA綜合教程

Java開發之文件上傳,java文件上傳


文件上傳有SmartUpload、Apache的Commons fileupload.我們今天介紹Commons fileupload的用法。

1.commons-fileupload-1.3.1.jar

下載地址:http://pan.baidu.com/s/1kVtYMzH

 

2.web.xml

 1 <!-- 上傳文件 服務器端 -->
 2   <servlet>
 3     <servlet-name>UploadServlet</servlet-name>
 4     <servlet-class>com.servlet.UploadServlet</servlet-class>
 5     <init-param>
 6     <param-name>filePath</param-name><!-- 文件存儲路徑 -->
 7     <param-value>F:\myeclipse\Workspaces\King\WebRoot\uploadfile\</param-value>
 8     </init-param>
 9   </servlet>
10   
11   <servlet-mapping>
12     <servlet-name>UploadServlet</servlet-name>
13     <url-pattern>/servlet/UploadServlet</url-pattern>
14   </servlet-mapping>

3.UploadFile.jsp

 1 <form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">
 2         <div align="center">
 3             <fieldset >
 4                 <legend>上傳文件</legend><br/>
 5                     <div align="left">上傳文件1</div>
 6                     <div align="left">
 7                         <input type="file" name="file1"/>
 8                     </div>
 9                     <div align="left">上傳文件2</div>
10                     <div align="left">
11                         <input type="file" name="file2"/>
12                     </div>
13                     <div>
14                         <div align='left'>上傳文件說明1</div>
15                         <div align='left'><input type="text" name="description1"/></div>
16                     </div>
17                     <div>
18                         <div align='left'>上傳文件說明2</div>
19                         <div align='left'><input type="text" name="description2"/></div>
20                     </div>
21                     <div>
22                         <div align='left'>
23                             <input type='submit' value="上傳文件"/>
24                         </div>
25                     </div>
26             </fieldset>
27         </div>
28     </form>

4.servlet

  1 package com.servlet;
  2 
  3 import java.io.File;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.InputStreamReader;
  8 import java.io.OutputStream;
  9 import java.io.PrintWriter;
 10 import java.util.List;
 11 
 12 import javax.servlet.ServletException;
 13 import javax.servlet.http.HttpServlet;
 14 import javax.servlet.http.HttpServletRequest;
 15 import javax.servlet.http.HttpServletResponse;
 16 
 17 import org.apache.commons.fileupload.DiskFileUpload;
 18 import org.apache.commons.fileupload.FileItem;
 19 import org.apache.commons.fileupload.FileUploadException;
 20 
 21 import sun.swing.FilePane;
 22 
 23 public class UploadServlet extends HttpServlet {
 24 
 25     private String filePath;
 26 
 27 
 28     /**
 29      * Destruction of the servlet. <br>
 30      */
 31     public void destroy() {
 32         super.destroy(); // Just puts "destroy" string in log
 33         // Put your code here
 34     }
 35 
 36     
 37     public void doGet(HttpServletRequest request, HttpServletResponse response)
 38             throws ServletException, IOException {
 39 
 40         response.setCharacterEncoding("UTF-8");
 41         response.getWriter().println("請以POST方式上傳文件");
 42     }
 43 
 44     
 45     public void doPost(HttpServletRequest request, HttpServletResponse response)
 46             throws ServletException, IOException {
 47         File file1 = null, file2 = null;
 48         String description1 = null, description2 = null;
 49         response.setCharacterEncoding("UTF-8");
 50         request.setCharacterEncoding("UTF-8");
 51         response.setContentType("text/html");
 52         PrintWriter out = response.getWriter();
 53         
 54         DiskFileUpload diskFileUpload = new DiskFileUpload();//解析request
 55         
 56         try {
 57             List<FileItem> fileList = diskFileUpload.parseRequest(request);//將結果放置到List中
 58             out.println("遍歷所有的FileItem...</br>");
 59             this.log("遍歷所有的FileItem...");
 60             for(FileItem fileItem : fileList){
 61                 if(fileItem.isFormField()){//如果是文本域
 62                     if("description1".equals(fileItem.getFieldName())){
 63                         out.println("遍歷到description1...</br>");
 64                         description1 = new String(fileItem.getString().getBytes(),"UTF-8");
 65                     }
 66                     if("description2".equals(fileItem.getFieldName())){
 67                         out.println("遍歷到description2...</br>");
 68                         description2 = new String(fileItem.getString().getBytes(),"UTF-8");
 69                     }
 70                 }else{//否則為文件域
 71                     if("file1".equals(fileItem.getFieldName())){
 72                         File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
 73                         out.println("遍歷到file1...</br>");
 74                         out.println("客戶端文件位置:"+remoteFile.getAbsolutePath()+"</br>");
 75                         //服務器端文件,放在uploadfile文件夾下
 76                         file1 = new File(filePath,remoteFile.getName());
 77                         file1.getParentFile().mkdirs();//創建文件夾路徑
 78                         file1.createNewFile();//創建新文件
 79                         InputStream ins = fileItem.getInputStream();//FileItem的內容
 80                         OutputStream ous = new FileOutputStream(file1);//輸出到文件中
 81                         try{
 82                             byte[] buffer = new byte[1024];//字節緩存
 83                             int len = 0;//實際緩存長度
 84                             while((len=ins.read(buffer)) > -1){//循環讀入緩存
 85                                 ous.write(buffer,0,len);
 86                             }
 87                             out.println("已保存文件"+file1.getAbsolutePath()+"</br>");
 88                         }
 89                         finally{
 90                             ous.close();
 91                             ins.close();
 92                         }
 93                     }
 94                     if("file2".equals(fileItem.getFieldName())){
 95                         File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
 96                         out.println("遍歷到file2...</br>");
 97                         out.println("客戶端文件位置:"+remoteFile.getAbsolutePath()+"</br>");
 98                         //服務器端文件,放在uploadfile文件夾下
 99                         file2 = new File(filePath,remoteFile.getName());
100                         file2.getParentFile().mkdirs();//創建文件夾路徑
101                         file2.createNewFile();//創建新文件
102                         InputStream ins = fileItem.getInputStream();//FileItem的內容
103                         OutputStream ous = new FileOutputStream(file2);//輸出到文件中
104                         try{
105                             byte[] buffer = new byte[1024];//字節緩存
106                             int len = 0;//實際緩存長度
107                             while((len=ins.read(buffer)) > -1){//循環讀入緩存
108                                 ous.write(buffer,0,len);
109                             }
110                             out.println("已保存文件"+file2.getAbsolutePath()+"</br>");
111                         }
112                         finally{
113                             ous.close();
114                             ins.close();
115                         }
116                     }
117                 }
118             }
119             out.println("Request 解析完畢");
120         } catch (FileUploadException e) {
121             // TODO 自動生成的 catch 塊
122             out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
123             out.println("<HTML>");
124             out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
125             out.println("  <BODY>");
126             
127             if(file1 != null){
128                 out.println("<div>");
129                 out.println(" <div align='left'>file1;</div>");
130                 out.println(" <div align='left'><a href='"+request.getContextPath()+
131                         file1.getName()+"'target=_blank>"+file1.getName()+"</a>");
132                 out.println("</div>");
133                 out.println("</div>");
134             }
135             if(file2 != null){
136                 out.println("<div>");
137                 out.println(" <div align='left'>file2;</div>");
138                 out.println(" <div align='left'><a href='"+request.getContextPath()+
139                         file2.getName()+"'target=_blank>"+file2.getName()+"</a>");
140                 out.println("</div>");
141                 out.println("</div>");
142             }
143             out.println("<div>");
144             out.println(" <div align='left'>description1:</div>");
145             out.println(" <div align='left'>");
146             out.println(description1);
147             out.println("</div>");
148             out.println("</div>");
149             
150             out.println("<div>");
151             out.println(" <div align='left'>description2:</div>");
152             out.println(" <div align='left'>");
153             out.println(description2);
154             out.println("</div>");
155             out.println("</div>");
156             
157             out.println("  </BODY>");
158             out.println("</HTML>");
159             out.flush();
160             out.close();
161         }
162         
163     }
164 
165     public String getServletInfo() {
166         return "This is my default servlet created by Eclipse";
167     }
168 
169     
170     public void init() throws ServletException {
171         // Put your code here
172         filePath = getServletConfig().getInitParameter("filePath");
173     }
174 
175 }

6.

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved