1.上传
上传文件对表单的要求:
表单要使用post提交
表单的enctype是multipart/form-data
表单中要有file类型的input文本域
Struts2上传是基于拦截器,底层还是使用commons-fileupload组件
struts上传的步骤:
(1)建立表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/upload/test" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="username"><br>
文件:<input type="file" name="upload"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
(2)创建Action
注意:
- 接收的文件的属性:和表单中文本域file类型input的name一致
- 接收的文件的名字:file的name+FileName固定写法
- 接收的文件的类型MIME类型:file的name+ContentType
package action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import javax.servlet.ServletContext;
import java.io.*;
public class UploadAction extends ActionSupport {
private String username;
// 要接收的文件,命名需要和表单中的file类型的input的name一致
private File upload;
/**
* 文件名的接收file属性名FileName:固定写法
*/
private String uploadFilename;
/**
* 获得上传文件的类型MIME,File属性nameContentType:固定写法
* 在tomcat\conf\web.xml中就可看到对应的文件类型属性了
*/
private String uploadContentType;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFilename() {
return uploadFilename;
}
public void setUploadFilename(String uploadFilename) {
this.uploadFilename = uploadFilename;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String upload() throws Exception {
// 获得servletcontext
ServletContext context = ServletActionContext.getServletContext();
// 获得服务器的绝对路径
String path = context.getRealPath("/");
path = path + "upload\\" + uploadFilename;
// 定义输入输出流
FileInputStream stream = new FileInputStream(upload);
FileOutputStream outputStream = new FileOutputStream(path);
int len = -1;
byte[] bs = new byte[1024];
while ((len = stream.read(bs)) != -1){
outputStream.write(bs, 0, len);
}
stream.close();
outputStream.close();
return super.SUCCESS;
}
}
2.文件上传的验证
再上传时要验证文件类型和大小
- 验证文件的类型
<package name="upload" extends="struts-default" namespace="/upload">
<action name="test" class="action.UploadAction" method="upload">
<!-- 主动引用默认拦截器栈-->
<interceptor-ref name="defaultStack">
<!-- 设置上传拦截器fileUpload.allowedExtensions, 不要使用allowedExtensionsSet-->
<param name="fileUpload.allowedExtensions">
.txt, .jpg
</param>
</interceptor-ref>
<result name="success">/index.jsp</result>
</action>
</package>
- 文件上传的大小设置
struts2默认上传文件的配置在default.properties这个默认配置文件中,由它来控制
struts.multipart.maxSize=2097152
文件超过自大致的提示信息在struts_message.properties中,我们只要通过国际化文件覆盖它即可。
3.struts2多文件的上传
- 多文件上传Action中把File属性以及和file属性像个的上传文件变成数组,在execute中循环上传即可。
-
package action; import com.opensymphony.xwork2.ActionSupport; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import javax.servlet.ServletContext; import java.io.File; public class UploadsAction extends ActionSupport { private String username; // 要接收的文件,命名需要和表单中的file类型的input的name一致 private File[] upload; /** * 文件名的接收file属性名FileName:固定写法 */ private String[] uploadFilename; /** * 获得上传文件的类型MIME,File属性nameContentType:固定写法 * 在tomcat\conf\web.xml中就可看到对应的文件类型属性了 */ private String[] uploadContentType; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadFilename() { return uploadFilename; } public void setUploadFilename(String[] uploadFilename) { this.uploadFilename = uploadFilename; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } public String upload() throws Exception { // 获得servletcontext ServletContext context = ServletActionContext.getServletContext(); String path = context.getRealPath("/"); for(int i = 0; i < upload.length; i++) { String filename = uploadFilename[i] +"."+ uploadContentType; // 获得服务器的绝对路径 path = path + "upload\\" + filename; // 定义输入输出流 FileUtils.copyFile(upload[i], new File(path)); } return super.SUCCESS; } }
jsp文件
-
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib uri="/struts-tags" prefix="s" %> <html> <head> <title>$Title$</title> </head> <body> <s:fielderror></s:fielderror> <form action="${pageContext.request.contextPath}/upload/test2" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="username"><br> 文件1:<input type="file" name="upload"><br> 文件2:<input type="file" name="upload"><br> 文件3:<input type="file" name="upload"><br> <input type="submit" value="提交"> </form> </body> </html>
4.文件的下载
struts文件下载对动作类Action的要求
在Action之中必须提供三个属性
-
package action; import com.opensymphony.xwork2.ActionSupport; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.struts2.ServletActionContext; import javax.servlet.ServletContext; import java.io.*; import java.net.URLEncoder; public class downAction extends ActionSupport { // 提供一个输入流的属性,名字叫inputStream:固定写法 private InputStream inputStream; // 定义文件的大小 private int filelength; // 定义文件名 private String fileName; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public int getFilelength() { return filelength; } public void setFilelength(int filelength) { this.filelength = filelength; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String down() throws Exception { ServletContext context = ServletActionContext.getServletContext(); String realPath = context.getRealPath("/"); // 实例化输入流 inputStream = new FileInputStream(realPath + "\\upload\\1557317591(1).jpg"); // 给fileName赋值 fileName = FilenameUtils.getName(realPath); // 对文件名做个编码 fileName = URLEncoder.encode(fileName, "UTF-8"); // 给文件大小赋值 filelength = inputStream.available(); return super.SUCCESS; } }
struts.xml
-
<action name="test3" class="action.downAction" method="down"> <result name="success" type="stream"> <!-- 指定Action中输入流变量--> <param name="inputStream">inputStream</param> <!-- 设置响应消息头Content-Disposition --> <param name="contentDisposition">attachment;filename=${fileName}</param> <!-- 使用下载的方式来返回结果--> <param name="contentType">application/octet-stream</param> <!-- 配置文件下载文件的大小--> <param name="contentLength">${filelength}</param> </result> </action>
直接请求test3便可下载