1、项目中文件上传相关文件:
- FileUploadAction.java
- FileUpload.jsp
- i18n.properties/i18n_en_US.properties/i18n_zh_CN.properties
- commons-fileupload.x.x.jar
- commons-io-x.x.x.jar
2、项目中文件下载相关文件:
- FileDownloadActionjava
- FileDownload.jsp
3、上传文件action类需要有的参数
public File FieldName 文件对象名(File类型的参数,参数名要跟页面上文件标签的对象名一致)
public String FieldName+ContentType 文件类型(String类型的参数,需要对象名加ContentType,注意ContentType这个单词的大小写,写错了会出错)
public String FieldName+FileName 文件名(String类型的参数,需要对象名加FileName,注意FileName这个单词的大小写)
4、需要一次性上传多个文件时,action类里面可以用List数据类型来定义第三点中的三个参数 如:List<File>、List<String>
5、对上传文件做限制的注意实现:
(1)到struts.xml中配置interceptors标签,并且使用default-interceptor-ref标签调用
(2)在struts2-core-x.x.xx.x.jar/org.apache.struts2 下的 default.properties有规定最大值为struts.multipart.maxSize=2097152,就是maximumSize不能大于2097152
(3)可配置参数有:
- maximumSize (optional) - 上传的文件的总大小的最大值,默认2M
- allowedTypes (optional) - 允许的上传文件的类型. 多个类型时,用逗号分割
- allowedExtensions (optional) - 允许的上传文件的扩展名. 多个拓展名时,用逗号分割
6、语言错计划设置中的错误信息:
可以参照这个properties struts2-core-x.x.xx.x.jar/org.apache.struts2/struts-messages.properties
7、文件下载的struts.xml配置:
(1)action标签下的result标签使用 type=”stream”
(2)stream下可配置的参数:
contentType: 结果类型 如:text/html
contentLength: 下载的文件的长度
contentDisposition: 设定响应头,响应头指定文件下载类型,一般取值为attachment;filename=”xx.后缀”
inputName: 指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream,action类里面只需要写getter方法
bufferSize: 缓存的大小. 默认为 1024
allowCaching: 是否允许使用缓存 ,默认true
contentCharSet: 指定下载的字符集 如:UTF-8
8、文件上传步骤:
(1)文件上传页面的s:form标签中,新建一个s:file标签并且定义其name
(2)编写对应的interceptor-stack,并且用default-interceptor-ref标签调用
(3)struts.xml中定义其action,注意如果出错的时候struts2调用的result是name为input的(不定义name为input的result标签的话,会报错)
(4)新建action类配置对应参数
9、文件下载步骤:
(1)文件下载页面调用对应action
(2)struts.xml中配置对应的action
(3)新建action类,配置对应的参数名
(4)在action类里面多新建一个InputStream参数并对其赋值(要生成文件的数据就在这里),因为调用action的时候,返回出去给struts2的是一个InputStream类型的,用于生成一个可供下载的文件
10、struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="i18n"/>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor-stack name="uploadInterceptors">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">7200</param>
<param name="fileUpload.allowedTypes">text/html,text/xml,text/plain</param>
<param name="fileUpload.allowedExtensions">html,xml,txt</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="uploadInterceptors"></default-interceptor-ref>
<action name="fileUpload" class="com.demo.sshtest.FileUploadAction">
<result>/FileUpload.jsp</result>
<result name="input">FileUpload.jsp</result>
</action>
<action name="fileDownload" class="com.demo.sshtest.FileDownloadAction">
<result type="stream">
<param name="bufferSize">2048</param>
</result>
</action>
</package>
</struts>
文件上传需要配置interceptor-stack,配置之后一定要用default-interceptor-ref调用,不然不会执行,文件下载的result一定要用stream类型的
11、FileUpload.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>fileupload</title>
</head>
<body>
<!--
1、上传文件所需jar包:
commons-fileupload-1.3.jar/commons-io-2.0.1.jar
2、文件上传 Action类:
//文件对应的 File 对象名
private File FieldName;
//文件类型
private String FieldName+ContentType;
//文件名
private String FieldName+FileName;
3、一次上传多个文件:
Action类里面需要把参数类型改为List数组类型,需要注意的是页面上文件域的名字要相同
4、对上传文件做限制:
(1)需要到struts.xml里面配置interceptors标签并且使用default-interceptor-ref标签调用
maximumSize (optional) - 上传的文件的总大小的最大值,默认2M
allowedTypes (optional) - 允许的上传文件的类型. 多个类型时,用逗号分割
allowedExtensions (optional) - 允许的上传文件的扩展名. 多个拓展名时,用逗号分割
(2)在struts2-core-x.x.xx.x.jar/org.apache.struts2 下的 default.properties有规定最大值为struts.multipart.maxSize=2097152,就是maximumSize不能大于2097152
5、语言国际化中设置的错误信息:
可以参照这个properties struts2-core-x.x.xx.x.jar/org.apache.struts2/struts-messages.properties
-->
<a href="fileUpload?request_locale=en_US">English</a><br>
<a href="fileUpload?request_locale=zh_CN">中文</a><br>
<s:debug></s:debug>
<s:form action="fileUpload" method="post" enctype="multipart/form-data" theme="simple">
<s:fielderror name="file"></s:fielderror><br>
<s:actionerror/><br>
File[0]:<s:file name="file" label="File[0]"></s:file><br>
FileDesc[0]:<s:textfield name="filedesc[0]" label="FileDesc[0]"></s:textfield><br>
File[1]:<s:file name="file" label="File[1]"></s:file><br>
FileDesc[1]:<s:textfield name="filedesc[1]" label="FileDesc[1]"></s:textfield><br>
File[2]:<s:file name="file" label="File[2]"></s:file><br>
FileDesc[2]:<s:textfield name="filedesc[2]" label="FileDesc[2]"></s:textfield><br>
<s:submit></s:submit><br>
</s:form>
</body>
</html>
文件上传错误的错误信息会在fieldErrors里面存着
12、FileUploadAction.java文件上传action类
package com.demo.sshtest;
import java.io.File;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public List<File> file;
public List<String> fileContentType;
public List<String> fileFileName;
public List<String> filedesc;
public List<File> getFile() {
return file;
}
public void setFile(List<File> file) {
this.file = file;
}
public List<String> getFileContentType() {
return fileContentType;
}
public void setFileContentType(List<String> fileContentType) {
this.fileContentType = fileContentType;
}
public List<String> getFileFileName() {
return fileFileName;
}
public void setFileFileName(List<String> fileFileName) {
this.fileFileName = fileFileName;
}
public List<String> getFiledesc() {
return filedesc;
}
public void setFiledesc(List<String> filedesc) {
this.filedesc = filedesc;
}
@Override
public String execute() throws Exception {
System.out.println("File:" + file);
System.out.println("contentType:" + fileContentType);
System.out.println("fileName:" + fileFileName);
System.out.println("filedesc:" + filedesc);
return super.execute();
}
}
13、i18n.properties/i18n_en_US.properties/i18n_zh_CN.properties文件上传会用到的国际化资源文件
struts.messages.error.uploading=File upload error
struts.messages.error.file.too.large=File over maximum Size
struts.messages.error.content.type.not.allowed=File data type not allowed
struts.messages.error.file.extension.not.allowed=Extensions not allowed
14、文件上传运行效果:
(1)成功时:这个不需要图片了,如果删除成功就会重定向到指定页面
(2)失败时:失败的时候,我指定它重定向回提交页面,并且会在fieldErrors里面找到对应错误信息
15、FildDownload.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>filedownload</title>
</head>
<body>
<!--
1、struts.xml设置
(1)action标签下的result标签 type="stream"
(2)stream下可以设置的参数:
contentType: 结果类型 如:text/html
contentLength: 下载的文件的长度
contentDisposition: 设定响应头. 响应头指定接应是一个文件下载类型, 一般取值为 attachment;filename="xxx.后缀".
inputName: 指定文件输入流的 getter 定义的那个属性的名字. 默认为 inputStream,action类里面只需要写getter方法
bufferSize: 缓存的大小. 默认为 1024
allowCaching: 是否允许使用缓存 ,默认true
contentCharSet: 指定下载的字符集 如:UTF-8
-->
<a href="fileDownload">fileDownload</a>
</body>
</html>
16、FileDownloadAction.java文件下载action类
package com.demo.sshtest;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownloadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String contentType;
public int contentLength;
public String contentDisposition;
public InputStream inputStream;
public String getContentType() {
return contentType;
}
public int getContentLength() {
return contentLength;
}
public String getContentDisposition() {
return contentDisposition;
}
public InputStream getInputStream() {
return inputStream;
}
@Override
public String execute() throws Exception {
// contentType = "text/html";//这句可写可不写
contentDisposition = "attachment;filename=newfile.txt";
String str = "sdfasdfadsfadsfadsfadfa";
inputStream = new ByteArrayInputStream(str.getBytes());
contentLength = inputStream.available();
return SUCCESS;
}
}
17、文件下载运行效果
18、项目目录
19、demo:
https://download.youkuaiyun.com/download/qq_22778717/10320991