在项目开发中经常会遇到文件上传下载问题,下面我分别来介绍一下文件的上传和下载。
一、文件上传
使用Struts2的文件上传功能的时候,只需要使用普通的Action类即可,但是为了获取一些上传文件的信息,如上传文件名和文件类型等,需要按照一定的规则在Action中增加一些getter和setter方法。
在Struts2中提供了文件上传拦截器(fileUpload),该拦截器能够实现对上传文件的过滤功能。fileUpload拦截器常用属性如下所示:
(1)、maximumSize:设置上传文件的最大长度,默认值为2MB。
(2)、allowedTypes:设置上传文件的类型,以“,”为分隔符,可以上传多种类型的文件。如text/html,如果不设置该属性就是允许任何类型的文件。
如果使用了拦截器对上传文件进行过滤,一旦上传文件格式不符合要求,将在页面中提示异常信息,提示的异常信息是Struts2框架中格式为“键=值”的信息,常用的“键”如下所示:
(1)、sturts.messages.error.type.not.allowed:设置上传文件的类型不匹配的提示信息。
(2)、sturts.messages.error.file.too.large:设置上传文件太大时的提示信息。
(3)、sturts.messages.error.uploading:设置文件不能上传时的通用信息。
下面我举一个文件上传的项目:
项目文件框架
fileUp.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
文件上传
<hr>
<s:form action="upLoad" enctype="multipart/form-data" method="post">
<s:textfield name="title" label="文件标题" />
<br>
<s:file name="upload" label="选择文件" />
<br>
<s:submit value="上传" />
</s:form>
</body>
</html>
fileUpSuccess.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>文件上传成功</title>
</head>
<body>
<h3>文件上传成功</h3>
<hr>
文件标题:<s:property value="+title" />
<br>
<s:property value="uploadFileName" />
<br>
<img src="<s:property value="'../save/'+uploadFileName" />" />
<br>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaeee/web-app_3_1.xsd"
id="WebApp_ID"
version="3.1" >
<filter>
<!-- 配置Struts2核心控制器的名字 -->
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- Struts2控制器的名字 -->
<filter-name>struts2</filter-name>
**加粗样式** <!-- 拦截所有的URL请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>fileUp.jsp</welcome-file>
</welcome-file-list>
</web-app>
UploadAction.java
package fileUpDown;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
public class UploadAction extends ActionSupport{
//文件标题
private String title;
//上传文件对象
private String upload;
//上传文件名
private String uploadFileName;
//获取在struts.xml文件中配置的文件保存路径
private String savePath;
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUpload() {
return this.upload;
}
public void setUpload(String upload) {
this.upload = upload;
}
public String getUploadFileName() {
return this.uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
private String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String execute() throws Exception{
//以服务器的文件保存地址和原文件名建立上传文件输入流
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
//定义输出流对象
FileInputStream fis=new FileInputStream(getUpload());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0) {
fos.write(buffer,0,len);
}
fos.close();
return SUCCESS;
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="upload" extends="struts-default">
<action name="upLoad" class="fileUpDown.UploadAction">
<!-- fileUpload拦截器设置 -->
<interceptor-ref name="fileUpload">
<!-- 设置上传文件的最大字节数 -->
<param name="maximumSize">10000000</param>
<!-- 设置上传文件的类型 -->
<param name="allowedTypes">
image/gif,image/png,image/jpeg,image/jpg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<!-- 设置上传文件保存的文件夹 -->
<param name="savePath">/save</param>
<result name="input">/fileUpload/fileUp.jsp</result>
<result name="success">/fileUpload/fileUpSuccess.jsp</result>
</action>
</package>
</struts>
运行结果
上传成功页面