Struts2 default.properites属性文件相关说明
struts.i18n.encoding=UTF-8 国际化默认编码格式为UTF-8
struts.objectFactory = spring spring整合时需要使用
### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data
# struts.multipart.parser=cos
# struts.multipart.parser=pell
struts.multipart.parser=Jakarta 使用apache提供的文件上传也下载
struts.action.extension=action,, 这时就是我们提交表单后缀名为.action的原因
struts.devMode = false 是否设置为开发者模式
struts.i18n.reload=false 设置资源文件是否每次缓存
struts.configuration.xml.reload=false 设置struts.xml修改是否每次自动加载
Struts2 文件上传
进行文件上传时,必须将表单的method属性设为post,将enctype属性设为multipart/form-data。
Struts2在进行文件上传操作时,实际上是通过两个步骤实现的:
1) 首先将客户端上传的文件保存到struts.multipart.saveDir键所指定的目录中,如果该键所对应的目录不存在,那么就保存到javax.servlet.context.tempdir环境变量所指定的目录中。
2) Action中所定义的File类型的成员变量file实际上指向的是临时目录中的临时文件,然后在服务器端通过IO的方式将临时文件写入到指定的服务器端目录中。
OGNL(Object Graph Navigation Language):对象图导航语言。
Struts2的文件上传实际上是通过FileUploadInterceptor拦截器来处理的。
源码展示:
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
文件上传8
9
10
11 username:
12 file:
13
14
15
16
文件上传username:file:
file2:
file3:Insert title here Insert title here Insert title here
file:
Insert title herestruts_021
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
strtus2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
strtus2
/*
/p>
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
/result.jsp
/Listresult.jsp
attachment;filename="test.txt"
downloadFile
attachment;filename=${filename}
downloadFile
struts.multipart.saveDir=c:/test
struts.multipart.maxSize=193439403840844
packagecom.oracle.action;importjava.io.InputStream;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;public class DownloadAction extendsActionSupport {//DownloadFile这里的名字需要与我们的struts.xml中的名字一样
publicInputStream getDownloadFile() {//将资源以流的形式返回
returnServletActionContext.getServletContext().getResourceAsStream("/upload/test.txt");
}
@Overridepublic String execute() throwsException {returnSUCCESS;
}
}
packagecom.oracle.action;importjava.io.InputStream;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;public class DownloadAction1 extendsActionSupport {private intnumber;privateString filename;publicString getFilename() {returnfilename;
}public voidsetFilename(String filename) {this.filename =filename;
}public intgetNumber() {returnnumber;
}public void setNumber(intnumber) {this.number =number;
}publicInputStream getDownloadFile() {try{if (1 ==number) {this.filename = "你好.txt";this.filename = new String(this.filename.getBytes("gbk"), "8859_1");return ServletActionContext.getServletContext().getResourceAsStream("/upload/你好.txt");
}else{this.filename = "test.txt";return ServletActionContext.getServletContext().getResourceAsStream("/upload/test.txt");
}
}catch(Exception ex) {
}return null;
}
@Overridepublic String execute() throwsException {returnSUCCESS;
}
}
packagecom.oracle.action;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;/***
*@authorMountainpeople
**/
public class UploadAction extendsActionSupport {//表单的名字
privateString username;//临时文件
private File file;//file必须要与我们的页面中的一样//得到文件的名字[FileName]
private String fileFileName;//file必须与页面中的一样 FileName必须这样的写法//得到文件的内容类型[ContentType]
privateString fileContentType;publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicFile getFile() {returnfile;
}public voidsetFile(File file) {this.file =file;
}publicString getFileFileName() {returnfileFileName;
}public voidsetFileFileName(String fileFileName) {this.fileFileName =fileFileName;
}publicString getFileContentType() {returnfileContentType;
}public voidsetFileContentType(String fileContentType) {this.fileContentType =fileContentType;
}
@Overridepublic String execute() throwsException {//完成文件的上传//存放文件的路径
String root = ServletActionContext.getRequest().getRealPath("/upload");//写入文件的路径//得到输入流对象
InputStream is = newFileInputStream(file);//创建File对象
File destFile = newFile(root, fileFileName);//得到输出流对象
OutputStream os = newFileOutputStream(destFile);//创建字节流对象
byte[] buffer = new byte[500];int length = 0;while ((length = is.read(buffer)) != -1) {
os.write(buffer,0, length);
}//操作完成关闭对象
is.close();
os.close();returnSUCCESS;
}
}
packagecom.oracle.action;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.List;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;/***
*@authorMountainpeople
**/
public class UploadListAction extendsActionSupport {privateString username;private Listfile;//得到文件的名字[FileName]
private ListfileFileName;//得到文件的内容类型[ContentType]
private ListfileContentType;publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}public ListgetFile() {returnfile;
}public void setFile(Listfile) {this.file =file;
}public ListgetFileFileName() {returnfileFileName;
}public void setFileFileName(ListfileFileName) {this.fileFileName =fileFileName;
}public ListgetFileContentType() {returnfileContentType;
}public void setFileContentType(ListfileContentType) {this.fileContentType =fileContentType;
}
@Overridepublic String execute() throwsException {for (int i = 0; i < file.size(); i++) {//完成文件的上传//存放文件的路径
String root =ServletActionContext.getRequest().getRealPath("/upload");//写入文件的路径//得到输入流对象
InputStream is = newFileInputStream(file.get(i));//创建File对象
File destFile = newFile(root, fileFileName.get(i));//得到输出流对象
OutputStream os = newFileOutputStream(destFile);//创建字节流对象
byte[] buffer = new byte[500];int length = 0;while ((length = is.read(buffer)) != -1) {
os.write(buffer,0, length);
}//操作完成关闭对象
is.close();
os.close();
}returnSUCCESS;
}
}