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

Struts2文件上傳

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

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

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

視圖文件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配置。web

接下來讓咱們建立一個簡單的 jsp 文件的success.jsp 結果顯示咱們的文件上傳的狀況下成功。apache

<%@ 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; } }

配置文件同上app

下載時可能會出現錯誤框架

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,那就是路徑不對了,或者說得準確些就根本沒有找到文件。jsp

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

相關文章
相關標籤/搜索