1.单文件上传:拦截器fileUpload完成的
提供表单,表单的enctype必须是multipart/form-data
<s:form enctype="multipart/form-data">
<s:textfield name="name" label="姓名"></s:textfield>
<s:file name="photot" label="靓照"><s:file>
<s:submit value="上传"><s:submit>
</s:form>
编写动作类
public class Upload1Action extends ActionSupport {
private String name;//普通字段
private File photo;//类型 File,字段名和表单保持一致
private String photoFileName;//类型 字符串,名字为 上传字段名+"FileName" 上传的文件名
private String photoContentType;// 固定ContentType, 上传的文件的MIME类型
public String upload(){
try {
//打印普通字段
System.out.println(name);
//文件上传到应用下的files目录中
String rootStoreDirectory = ServletActionContext.getServletContext().getRealPath("/files");
FileUtils.moveFile(photo, new File(rootStoreDirectory,photoFileName));//剪切
return SUCCESS;
} catch (IOException e) {
e.printStackTrace();
return ERROR;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
}
原理:是由fileUpload拦截器完成的
拦截器能限制上传文件的MIME类型、扩展名和大小(默认2M),上传遇到问题拦截器会自动转向input视图。
限制上传大小:
更改全局参数:<constant name="struts.multipart.maxSize" value="3145728"></constant>
限制上传文件的扩展名:
<package name="p1" extends="struts-default">
<action name="upload" class="com.itheima.action.Upload1Action" method="upload">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">.txt,.jpg</param>
<param name="fileUpload.allowedTypes">image/jpeg,text/plain</param>
</interceptor-ref>
<result>/success.jsp</result>
<result name="input">/1.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
中文提示:
默认提示:struts2-core.jar包的org.apache.struts2的message中
局部国际化消息资源文件:
在动作类所在的包中建立”动作类名_zh_CN.properties”
配置全局国际化消息文件:
配置内容:
2.多文件上传
public class Upload2Action extends ActionSupport {
private String name;//普通字段
private File[] photo;//类型File,字段名和表单保持一致。用数组或者List
private String[] photoFileName;//类型字符串,名字为 上传字段名+"FileName" 上传的文件名
private String[] photoContentType;// 固定ContentType, 上传的文件的MIME类型
public String upload(){
try {
//打印普通字段
System.out.println(name);
for(int i=0;photo!=null&&i<photo.length;i++){
//文件上传到应用下的files目录中
String rootStoreDirectory = ServletActionContext.getServletContext().getRealPath("/files");
FileUtils.moveFile(photo[i], new File(rootStoreDirectory,photoFileName[i]));//剪切
}
return SUCCESS;
} catch (IOException e) {
e.printStackTrace();
return ERROR;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File[] getPhoto() {
return photo;
}
public void setPhoto(File[] photo) {
this.photo = photo;
}
public String[] getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String[] photoFileName) {
this.photoFileName = photoFileName;
}
public String[] getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String[] photoContentType) {
this.photoContentType = photoContentType;
}
}
3.文件的下载
动作类提供一个InputStream的输入流
public class DownloadAction extends ActionSupport {
private InputStream inStream;//名字不要使用in,Struts2中有一个bug
private String filename;//文件名
private int filelength;//文件长度
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public int getFilelength() {
return filelength;
}
public void setFilelength(int filelength) {
this.filelength = filelength;
}
public InputStream getInStream() {
return inStream;
}
public void setInStream(InputStream inStream) {
this.inStream = inStream;
}
public String download(){
try {
//给inStream初始化
String filepath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/美女.jpg");
//截取文件名
filename = FilenameUtils.getName(filepath);
//IE
filename = URLEncoder.encode(filename, "UTF-8");
inStream = new FileInputStream(filepath);
//文件长度赋值
filelength = inStream.available();
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
配置结果类型:
<action name="download" class="com.zzxb.action.DownloadAction" method="download">
<result name="success" type="stream">
<!-- 指定动作类中InputStream的属性名称 -->
<param name="inputName">inStream</param>
<!-- 告知客户端用下载的方式打开 . ${filename},获取当前动作的属性,用的OGNL表达式。OGNL表达式在xml文件中使用${}-->
<param name="contentDisposition">attachment;filename=${filename}</param>
<!-- 告知客户端MIME类型 -->
<param name="contentType">application/octet-stream</param>
<param name="contentLength">${filelength}</param>
</result>
</action>