java框架篇---struts之文件上傳和下載


Struts2文件上傳

Struts 2框架提供了內置支持處理文件上傳使用基於HTML表單的文件上傳。上傳一個文件時,它通常會被存儲在一個臨時目錄中,他們應該由Action類進行處理或移動到一個永久的目錄,以確保數據不丟失。

請注意,服務器有一個安全策略可能會禁止寫到目錄以外的臨時目錄和屬於web應用的目錄。

在Struts中的文件上傳是通過預先定義的攔截文件上傳攔截器這是可通過org.apache.struts2.interceptor.FileUploadInterceptor類的defaultStack中的一部分。仍然可以使用在struts.xml中設置各種參數,我們將在下面看到。

視圖文件index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
 <form action="Upload.action" method="post" enctype="multipart/form-data">
      <label for="myFile">你要上傳的文件</label>
      <input type="file" name="myFile" /><br>
      
      <input type="submit" value="上傳"/>
   </form>
  </body>
</html>

在上面的例子中值得注意幾點說明。首先,表單的enctype屬性設置為multipart/ form-data。這應該是設置為使得處理文件上傳文件上傳。下一個點值得注意的是表單的 action方法上傳和文件上傳字段的名稱 - myFile。我們需要這些信息創建操作方法和struts配置。

接下來讓我們創建一個簡單的 jsp 文件的success.jsp 結果顯示我們的文件上傳的情況下成功。

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>文件上傳</title>
</head>
<body>
成功
</body>
</html>

創建action類: 處理上傳文件,

package com.oumyye.FileUpload;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{
        private File myFile;
       private String myFileContentType;
       private String myFileFileName;
       private String destPath;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
         destPath = "e:/upload/"; 
          try{
              System.out.println("Src File name: " + myFile);  
              System.out.println("我的文件名"+myFileFileName);
              System.out.println("我的文件類型"+ myFileContentType);
              File destFile  = new File(destPath, myFileFileName);
             FileUtils.copyFile(myFile, destFile);
          }catch(IOException e){
             e.printStackTrace();
             return ERROR;
          }
        return SUCCESS;
    }
    public File getMyFile() {
        return myFile;
    }
    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }
    public String getMyFileContentType() {
        return myFileContentType;
    }
    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
    }
    public String getMyFileFileName() {
        return myFileFileName;
    }
    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }
    public String getDestPath() {
        return destPath;
    }
    public void setDestPath(String destPath) {
        this.destPath = destPath;
    }
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
}

配置文件:

可以使用恆定的標簽在應用程序 struts.xml文件,像我一樣改變要上傳的文件的最大大小。讓我們有我們的在struts.xml如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.devMode" value="false" />
    <!-- 指定每次請求到達,重新加載資源文件 -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- 指定每次配置文件更改后,自動重新加載 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 把主題配置為simple -->
    <constant name="struts.ui.theme" value="simple" />

    <!-- 指定允許上傳的文件最大字節數。默認值是2097152(2M) -->
    <constant name="struts.multipart.maxSize" value="10701096" />
<!--文件上傳--> <package name="upload" namespace="/" extends="struts-default"> <action name="Upload" class="com.oumyye.FileUpload.FileUploadAction"> <result name="success">/success.jsp</result> <result name="input">/index.jsp</result> </action>

<!--文件下載--> <action name="FileDownload" class="com.oumyye.action.FileDownload"> <result name="success" type="stream"> <param name="contentType">text/plain</param> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <param name="inputName">downloadFile</param> <param name="bufferSize">1024</param> </result> </action> </package> </struts>

以下是web.xml文件中的內容,與Struts2的基本配置一樣

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Struts2_1000_FileUpload</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>

上述代碼即可完成文件上傳

文件下載

視圖文件filedownload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>文件下載</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
    <h2>文件下載內容:</h2><br/>
    Dream.jpg:<a href="FileDownload.action">點擊下載</a><br/>
  </body>
</html>

創建action類: 處理上傳文件,

package com.oumyye.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

//文件下載
public class FileDownload extends ActionSupport{
  
private String fileName; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } //返回一個輸入流,作為一個客戶端來說是一個輸入流,但對於服務器端是一個 輸出流 public InputStream getDownloadFile() throws Exception { this.fileName = "hello.jpg" ; //獲取資源路徑 return ServletActionContext.getServletContext().getResourceAsStream("upload/"+this.fileName) ; } @Override public String execute() throws Exception { return SUCCESS; } }

配置文件同上

下載時可能會出現錯誤

Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the <param name="inputName"> tag specified for this action.

可能的原因:1.文件路徑不對,根本就沒有取到文件。這種情況下,可以將獲得InputStream的那條語句放在system.out.println()中輸出一下,若為null,那就是路徑不對了,或者說得准確些就根本沒有找到文件。

2.在action中沒有寫配置文件中"<param name="inputName">"后面屬性的那個get方法.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM