自己的理解:
第一种方法 :
对于用自己实现的文件上传或者下载,首先要通过流来获得文件的信息,然后通过操作将文件保存到一个文件夹中,再通过设置响应头和拿到输出流对象的方法来实现文件的下载
第二种方法:
strtus实现了数据的自动封装,只需要将获得文件表单的inputStream写到XML文件中,再用get或者set方法来进行一些变量的配置,就能够进行文件的上传和下载
Action部分的BaseACtion,用来获得request,session,application三个对象
package Demo;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
public Map<String,Object> request;
public Map<String,Object> session;
public Map<String,Object> application;
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
}
HelloAction,继承baseActiona用来对上传和下载的业务逻辑操作
package Demo;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class HelloAction extends BaseAction{
private File file1;
private String file1FileName;
private String file1ContentType;
public void setFile1(File file1) {
this.file1 = file1;
}
public void setFile1FileName(String file1FileName) {
String id = UUID.randomUUID().toString();
this.file1FileName = id+"#"+file1FileName;
}
public void setFile1ContentType(String file1ContentType) {
this.file1ContentType = file1ContentType;
}
public String getInfo() throws Exception{
String basePath = ServletActionContext.getServletContext().getRealPath("/upload");
File destFile = new File(basePath,file1FileName);
//处理文件上传
FileUtils.copyFile(file1, destFile);
return "success";
}
public String showList() throws Exception{
Map<String, String> fileNames = new HashMap<String, String>();
String basePath = ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(basePath);
String list[] = file.list();
if(list!=null &&list.length>0){
for(int i=0;i<list.length;i++){
String fileName = list[i];
String shortName = fileName.substring(fileName.lastIndexOf("#")+1);
fileNames.put(fileName, shortName);
}
}
request.put("fileNames", fileNames);
return "showList";
}
private String fileName;
//获取下载的文件的文件名
public void setFileName(String fileName){
try {
fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//处理好的文件名
this.fileName = fileName;
}
//2.下载提交的业务方法
public String down() throws Exception{
return "downFile";
}
public InputStream getInputStream(){
//System.out.println(ServletActionContext.getServletContext().getResourceAsStream(ServletActionContext.getServletContext().getRealPath("/upload")+fileName));
return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+fileName);
}
public String getDownFileName() throws Exception{
fileName = URLEncoder.encode(fileName, "UTF-8");
return fileName;
}
}
XML配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name = "xxxx" extends = "struts-default">
<action name="getInfo" class = "Demo.HelloAction" method = "getInfo">
<result name = "success">/success.jsp</result>
</action>
<action name="showList" class = "Demo.HelloAction" method = "showList">
<result name = "showList">/showList.jsp</result>
</action>
<action name="down" class="Demo.HelloAction" method = "down">
<result name = "downFile" type = "stream">
<!-- 调用action中返回流的方法 -->
<!-- 允许下载的文件的类型:指定为所有的二进制文件类型 -->
<param name="contentType">application/octet-stream</param>
<!-- 对应的是Action中属性;返回流的属性 -->
<param name="inputName">inputStream</param>
<!-- 下载头,浏览器显示的文件名 -->
<param name="contentDisPosition">attachment;filename=${downFileName}</param>
<param name="bufferSize"></param>
</result>
</action>
</package>
</struts>