Struts2文件上傳下載和表單重複提交問題

Struts2文件上傳下載和表單重複提交問題

文件上傳:
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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 多文件上傳除了基本的 enctype="multipart/form-data" 配置以外 file 的name屬性要相同 -->
<s:form action="upload" method="post" namespace="/test3" enctype="multipart/form-data">
<s:file name="file" label="文件1"></s:file>
<s:file name="file" label="文件2"></s:file>
<s:file name="file" label="文件3"></s:file>
<s:file name="file" label="文件4"></s:file>
<s:submit value="上傳"></s:submit>
</s:form>
</body>
</html>

action:
package com.hz.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

//Struts2 的文件上傳實際上使用的是 Commons FileUpload 組件, 所以需要導入
//commons-fileupload-1.3.jar
//commons-io-2.0.1.jar

public class UploadAction extends ActionSupport{
//獲取上傳的文件File對象
private List<File> file;
//獲取上傳的文件名:格式時上傳的文件名(file)+FileName
private List<String> fileFileName;
//獲取上傳的文件類型:格式時上傳的文件名(file)+ContentType
private List<String> fileContentType;

public List<File> getFile() {
return file;
}

public void setFile(List<File> file) {
this.file = file;
}

public List<String> getFileFileName() {
return fileFileName;
}

public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}

public List<String> getFileContentType() {
return fileContentType;
}

public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}

public String execute() throws IOException{
ServletContext servletContext=ServletActionContext.getServletContext();
for(int i=0;i<file.size();i++){
String dir=servletContext.getRealPath("/files/" + fileFileName.get(i));
FileOutputStream out = new FileOutputStream(dir);
FileInputStream in = new FileInputStream(file.get(i));
byte [] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
out.close();
in.close();
}
return "success";
}

}

struts.xml:
<action name="upload" class="com.hz.action.UploadAction">
<result name="success">/success.jsp</result>
</action>
-------------------------------------------------------------------------------------------------------

文件下載:

jsp:
<a href="download">下載</a>

action:
package com.hz.action;

import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{
private String contentType;
private long contentLength;
private String contentDisposition;
private InputStream inputStream;
public String getContentType() {
return contentType;
}

public long getContentLength() {
return contentLength;
}

public String getContentDisposition() {
return contentDisposition;
}
public InputStream getInputStream() {
return inputStream;
}

public String execute() throws Exception {
// contentType: 結果類型
// contentLength: 下載的文件的長度
// contentDisposition: 設定 Content-Dispositoin 響應頭. 該響應頭指定接應是一個文件下載類型, 一般取值爲 attachment;filename="document.pdf".
//
// inputName: 指定文件輸入流的 getter 定義的那個屬性的名字. 默認爲 inputStream
//
// bufferSize: 緩存的大小. 默認爲 1024
// allowCaching: 是否允許使用緩存
// contentCharSet: 指定下載的字符集

//確定各個成員變量的值
contentType = "text/html";
contentDisposition = "attachment;filename=hidden.html";
ServletContext servletContext = ServletActionContext.getServletContext();
String fileName = servletContext.getRealPath("/files/hidden.html");
inputStream = new FileInputStream(fileName);
contentLength = inputStream.available();
return SUCCESS;
}
}

struts.xml

<action name="download" class="com.hz.action.DownloadAction">
<result type="stream">
<param name="bufferSize">2048</param>
</result>
</action>

-------------------------------------------------------------------------------------------------------

表單重複提交問題:

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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="form" method="post">
<s:token></s:token>
<s:textfield name="name" label="name"></s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>


ACTION:
package com.hz.action;

import com.opensymphony.xwork2.ActionSupport;

public class TokenAction extends ActionSupport{
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public String execute() throws InterruptedException{
//Thread.sleep(2000);
System.out.println(name);
return "success";
}
}


STRUTS.XML:

1.
<action name="form" class="com.hz.action.TokenAction">
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="invalid.token">/success.jsp</result>
<result name="success">/success.jsp</result>
</action>

2.(不跳轉頁面)
<action name="form" class="com.hz.action.TokenAction">
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/success.jsp</result>
</action>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章