单个文件上传:
web.xml文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<display-name>Struts Blank</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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="default" namespace="/" extends="struts-default">
<!--文件上传的action,注意name值,测试时使用"upload"一直报"there is no mapped action ......."-->
<action name="myTestUpload" class="cn.bdx.action.UploadAction">
<result name="success">/main/index.jsp</result>
</action>
</package>
</struts>
接下来是action类的配置:
package cn.bdx.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private String username;
private File file;
private String fileFileName;
private String fileContentType;
@Override
public String execute() throws Exception {
System.out.println(ServletActionContext.getServletContext());
String root = ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(root);
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(root,fileFileName));
System.out.println("fileName = " + fileFileName);
System.out.println("file=" +file.getName());
System.out.println("filePath = " + file.getPath());
byte[] buffer = new byte[1024];
int length = 0;
while(-1!=(length = is.read(buffer, 0, buffer.length))) {
os.write(buffer);
}
os.close();
is.close();
return SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
}
jsp页面:
<form action="myTestUpload" method="post" enctype="multipart/form-data">
username:<input type="text" name="username" />
file:<input type="file" name="file"/>
<input type="submit" value="submit">
</form>
注意:1.struts.xml文件的名称,不能写成其他的名字(如果非要写成其他名字,可以在struts配置文件中进行修改,这里就不赘言了)。
2.struts.xml文件action中的name值的问题,我在做测试的时候将这个值写成"upload",就一直出现there is no mapped action namespace[/]......
3.这里的file并不是真正指代jsp上传过来的文件,当文件上传过来时,struts2首先会寻找struts.multipart.saveDir(这个是在default.properties里面有)这个name所指定的存放位置,我们可以新建一个struts.properties属性文件来指定这个临时文件存放位置,如果没有指定,那么文件会存放在tomcat的apache-tomcat-7.0.29\work\Catalina\localhost\目录下,然后我们可以指定文件上传后的存放位置,通过输出流将其写到流里面就行了,这时我们就可以在文件夹里看到我们上传的文件了。
struts单个文件下载:
其实struts2的文件下载原理很简单,就是定义一个输入流,然后将文件写到输入流里面就行,关键配置还是在struts.xml这个配置文件里配置。
那么来看看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="default" namespace="/" extends="struts-default"> <action name="fileDownLoad" class="cn.bdx.action.DownLoadAction"> <result name="success" type="stream"> <param name="contentDisposition">attachment;filename="DiagTools.exe"</param> <param name="inputName">downloadFile</param> </result> </action> </package> </struts>
有几个地方我们要注意,首先是result的类型,以前我们定义一个action,result那里我们基本上都不写type属性,因为其默认是请求转发(dispatcher)的方式,除了这个属性一般还有redirect(重定向)等这些值,在这里因为我们用的是文件下载,所以type一定要定义成stream类型,告诉action这是文件下载的result,result元素里面一般还有param子元素,这个是用来设定文件下载时的参数,inputName这个属性就是得到action中的文件输入流,名字一定要和action中的输入流属性名字相同,然后就是contentDisposition属性,这个属性一般用来指定我们希望通过怎么样的方式来处理下载的文件,如果值是attachment,则会弹出一个下载框,让用户选择是否下载,如果不设定这个值,那么浏览器会首先查看自己能否打开下载的文件,如果能,就会直接打开所下载的文件,(这当然不是我们所需要的),另外一个值就是filename这个就是文件在下载时所提示的文件下载名字。在配置完这些信息后,我们就能过实现文件的下载功能了。
接下来是action类的代码
package cn.bdx.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownLoadAction extends ActionSupport {
private InputStream downloadFile;
public InputStream getDownloadFile() {
return ServletActionContext.getServletContext().getResourceAsStream(
"upload/DiagTools.exe");
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
注意:downloadFile属性必须跟struts.xml文件中配置的inputstream要相同
最后是jsp页面代码:
wenjianmingcheng<a href="fileDownLoad">下载</a>
ok,struts2单文件上传下载就完毕了。
本文介绍如何使用Struts2框架实现文件的上传与下载功能。详细解释了web.xml及struts.xml配置文件的设置方法,并提供了action类的示例代码。
249

被折叠的 条评论
为什么被折叠?



