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>
事例代码下载
文件上传:
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>
事例代码下载
相关文章推荐
- Struts2学习之3(文件的上传下载,OGNL表达式,常用标签,表单重复提交)
- 文件的上传+下载+表单的重复提交问题 +自定义拦截器:
- JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传
- Struts2 Day 05 : 运行流程、自定义拦截器、上传下载、表单重复提交、输入验证
- struts2.x表单重复提交,文件上传下载,转换器
- struts2中的表单提交与文件上传下载
- 传智播客--struts2.x表单重复提交,文件上传下载,转换器
- JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传
- iframe标签实现form表单提交(如文件上传下载)不刷新
- 研究了三天的文件上传(jspsmart实现)file和text表单同时提交的问题
- 文件上传(jspsmart实现)file和text表单同时提交的问题
- strutsmvc ajaxForm提交表单ie出现json文件下载问题解决
- struts2文件上传下载(含中文编码问题)
- struts2文件上传后找不到文件 与 文件下载的问题
- struts2文件上传下载及乱码问题解决
- 一个IE7下,用form表单跳转iframe提交,然后下载文件。遇到的很有意思的问题
- struts2中form只提交数据不跳转引发的刷新后重复提交表单中数据的问题
- struts2上传下载文件中遇到的乱码问题
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- 关于Struts2文件下载时,下载方法被重复执行的问题