文件上傳的幾種方式

文件上傳與文件上傳一樣重要。在Java中,要實現文件上傳,可以有兩種方式:

1、通過Servlet類上傳

2、通過Struts框架實現上傳

這兩種方式的根本還是通過Servlet進行IO流的操作。

一、通過Servlet類上傳

1、編寫Sevlet類

package com.chanshuyi.upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet {

  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    
    InputStream in = request.getInputStream();
    
    /* 設置文件保存地址 */
    File saveFile = new File(this.getServletContext().getRealPath("/uploaded"), "hello.txt");	
    System.out.println("[文件保存地址]:" + saveFile.getAbsolutePath());
    
    /* 保存 */
    FileOutputStream out = new FileOutputStream(saveFile);
    byte[] buf = new byte[4096];
    int readLength = -1;
    while((readLength = in.read(buf)) != -1)
    {
      out.write(buf);
    }
    out.flush();
    out.close();
    in.close();
    response.getWriter().write("<html><script>alert('Uploaded Succeed!')</script></html>");
  }
}


這裏用純Servlet實現的時候,無法獲取文件的文件名以及一些其他信息。 還不知道怎麼解決(MARK)。

2、配置web.xml文件

添加以下代碼:

1 <!-- 文件上傳(通過Servlet實現)  -->
2     <servlet>
3         <servlet-name>fileUploadServlet</servlet-name>
4         <servlet-class>com.chanshuyi.upload.FileUploadServlet</servlet-class>
5     </servlet>
6     <servlet-mapping>
7         <servlet-name>fileUploadServlet</servlet-name>
8         <url-pattern>/fileUploadServlet</url-pattern>
9     </servlet-mapping>


3、前臺代碼

1 
<p>通過Servlet實現上傳</p>
2 <form action="fileUploadServlet" method="post" enctype="multipart/form-data"><input type="file" name="file" id="file"/><input type="submit" value="提交"/></form>

二、通過Struts框架實現上傳

1、配置struts.xml文件

添加如下Action:

<!-- 文件上傳(通過Struts實現) -->
<action name="fileUpload" class="com.chanshuyi.upload.FileUploadAction"></action>


2、編寫Action類

 1 package com.chanshuyi.upload;
 2 
 3 import java.io.File;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.http.HttpServletResponse;
 7 
 8 import org.apache.commons.io.FileUtils;
 9 import org.apache.struts2.ServletActionContext;
10 
11 import com.opensymphony.xwork2.ActionSupport;
12 
13 @SuppressWarnings("serial")
14 public class FileUploadAction extends ActionSupport {
15     
16     /** ActionContext對象 **/
17     ServletContext servletContext = ServletActionContext.getServletContext();
18     
19     HttpServletResponse response = ServletActionContext.getResponse();
20     
21     /* 特定的命名規則,不能改變 */
22     /** 上傳的文件,名字要與前臺name屬性相同 **/
23     private File file; 
24     
25     /** 上傳文件名稱 **/
26     private String fileFileName; 
27     
28     /** 上傳文件類型 **/
29     private String fileContentType;
30     
31     public String execute()throws Exception
32     {
33         if(file == null)
34         {
35             return null;
36         }
37         /* 設置文件保存地址 */
38         File saveFile = new File(servletContext.getRealPath("/uploaded"), fileFileName);
39         System.out.println("[文件保存地址]:" + saveFile.getAbsolutePath());
40         if(!saveFile.getParentFile().exists())
41         {
42             saveFile.getParentFile().mkdir();
43         }
44         
45         FileUtils.copyFile(file, saveFile);
46         System.out.println("[系統消息]:文件已經保存,保存路徑爲->" + saveFile.getAbsolutePath());
47         
48         response.getWriter().write("<html><script>alert('uploaded Succeed!')</script></html>");
49         
50         return null;
51     }
52     /* 省略GET/SET方法 */
53 }


3、前臺頁面

<p>通過Struts實現上傳</p>
<form action="fileUpload.action" method="post" enctype="multipart/form-data"><input type="file" name="file" id="file"/><input type="submit" value="提交"/></form>

本例寫的Action處理後不返回result,直接向response對象寫入數據,彈出上傳成功的提示。 


發佈了41 篇原創文章 · 獲贊 5 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章