文件的和上传和下载:
(1)文件的上传:
Struts是通过拦截器实现文件上传的,而默认拦截器栈中包含了文件上传拦截器,故表单通过Struts2可直接将文件上传,其底层是通过apache的commons-fileupload完成的。
我们要做的,就是将上传好的文件放到指定的位置或者其他的一些处理。
前端表单提交的代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html>
<head>
<title>index page</title>
</head> <body>
<form action="test/upload.action" method="POST" enctype="multipart/form-data">
文件:<input type="file" name="img"/><br>
<input type="submit" value="上传"/>
</form>
</body>
</html>
Action代码:
package com.tongji.actions; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; public class UploadAction {
private File img;
private String imgFileName; //文件名 = 文件 + FileName , 必须是这样 public File getImg() {
return img;
} public void setImg(File img) {
this.img = img;
} public String getImgFileName() {
return imgFileName;
} public void setImgFileName(String imgFileName) {
this.imgFileName = imgFileName;
} public String execute() throws IOException {
if (img != null) {
//String path = "d:/images";
String path = ServletActionContext.getServletContext().getRealPath("/images");
File destFile = new File(path,imgFileName);
FileUtils.copyFile(img, destFile);
return "success";
}
return "message";
} }
注意:第32行,不能像31行那样直接写文件要被拷贝到的路径。因为,不能让前端代码操纵后端服务器的盘符,前端代码能够操作的只能是对应项目的所在空间(tomcat目录下的项目的空间)。
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.multipart.maxSize" value="20971520"/> <!-- 改变可上传文件的大小 -->
<package name="demo" namespace="/test" extends="struts-default">
<action name="upload" class="com.tongji.actions.UploadAction">
<result>/welcome.jsp</result>
<result name="message">/messge.jsp</result> <!-- 处理上传失败异常 -->
<interceptor-ref name="defaultStack"> <!-- 设置可上传文件的拓展名 -->
<param name="fileUpload.allowedExtensions">jpg,png</param>
</interceptor-ref>
</action>
</package>
</struts>
注意:以上的注释内容。
(2)多文件的上传:
前端表单提交的代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html>
<head>
<title>index page</title>
</head> <body>
<form action="test/upload.action" method="POST" enctype="multipart/form-data">
文件1:<input type="file" name="imgs"/><br>
文件2:<input type="file" name="imgs"/><br>
文件3:<input type="file" name="imgs"/><br>
<input type="submit" value="上传"/>
</form>
</body>
</html>
Action代码:
package com.tongji.actions; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; public class UploadAction {
private File[] imgs;
private String[] imgsFileName; //文件名 = 文件 + FileName , 必须是这样 public File[] getImgs() {
return imgs;
} public void setImgs(File[] imgs) {
this.imgs = imgs;
} public String[] getImgsFileName() {
return imgsFileName;
} public void setImgsFileName(String[] imgsFileName) {
this.imgsFileName = imgsFileName;
} public String execute() throws IOException {
//若没有选择要上传的文件,则该数组不进行创建,而非创建了长度为0的数组对象
if (imgs != null) {
String path = ServletActionContext.getServletContext().getRealPath("/images");
for (int i = 0; i < imgs.length; i++) {
File destFile = new File(path, imgsFileName[i]);
FileUtils.copyFile(imgs[i], destFile);
}
return "success";
}
return "message";
} }
补充:<constant name="struts.multipart.maxSize" value="20971520"/> <!-- 改变可上传文件的大小 -->,在多文件上传时,指的是多文件上传的总大小
(3)文件的下载:
服务端向客户端浏览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt、jpg等,会直接在浏览器中显示;如果需要用户以附件的形式保存,则称为文件下载。
如果需要向浏览器提供文件下载功能,则需要设置HTTP响应头的Content-Disposition属性,即内容配置属性值为attachment(附件)。
Action类中需要提供两个属性,一个为文件输入流,用于指定服务器向客户端所提供下载的文件资源;一个为文件名,即用户要下载的资源文件名。
前端页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html>
<head>
<title>index page</title>
</head> <body>
<a href="test/download.action">美图</a>
</body>
</html>
Action代码:
package com.tongji.actions; import java.io.InputStream; import org.apache.struts2.ServletActionContext; public class DownloadAction {
private InputStream is;
private String fileName; //随意 public InputStream getIs() {
return is;
} public void setIs(InputStream is) {
this.is = is;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public String execute() throws Exception {
fileName = "5.jpg"; fileName = "书言.jpg";
is = ServletActionContext.getServletContext().getResourceAsStream("/images/" + fileName); byte[] bytes = fileName.getBytes("utf-8");
fileName = new String(bytes, "iso-8859-1"); return "success";
} }
解释:第33行是为了解决中文乱码的问题,因为代码是utf-8格式的,而前端页面是iso-8859-1格式的。
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>
<package name="demo" namespace="/test" extends="struts-default">
<action name="download" class="com.tongji.actions.DownloadAction">
<result type="stream">
<param name="inputName">is</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>
</package>
</struts>
解释:返回类型是stream,并且要指定inputName(用于指定服务器向客户端所提供下载的文件资源),以及需要设置HTTP响应头的Content-Disposition属性,即配置attachment和文件名。
如果Action中的InputStream属性值为inputStream,则不需要配置<param name="inputName">is</param>,因为InputStream默认值就为inputStream。