您的位置:首页 > 编程语言 > Java开发

Struts2文件上传下载和表单重复提交问题

2017-07-24 21:03 453 查看
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>

事例代码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息