基于struts2的文件上传下载

1.struts.xml

复制代码
 1 <struts>
 2     <constant name="struts.multipart.maxSize" value="209715200" /><!-- 设置文件上传大小,2097152=2M -->
 3     <package name="action" extends="struts-default" namespace="/">
 4         <!-- 文件上传 -->
 5         <action name="upload" class="com.fileupload.action.UploadAction" method="execute">
 6             <result name="success">load_success.jsp</result><!-- 文件上传成功后跳转页面 -->
 7             <result name="message">/message.jsp</result><!-- 上传失败跳转页面 -->
 8             <interceptor-ref name="defaultStack">
 9                 <param name="fileUpload.allowedExtensions">jpg,png</param><!-- 设置上传格式 -->
10             </interceptor-ref>
11         </action>
12         <!-- 文件下载 -->
13         <action name="download" class="com.fileupload.action.DownloadAction" method="execute">
14             <result type="stream">
15                 <param name="inputName">is</param> <!-- 指定返回流的名字 -->
16                 <param name="contentType">application/pdf/jpg</param><!-- 指定返会数据的类型 -->
17                 <param name="contentDisposition">attachement;filename=${fileName}</param><!-- 指定用户下载的文件的类型和名称 -->
18             </result>
19         </action>
20     </package>
21 </struts>
复制代码

 

2.上传action

复制代码
 1 import java.io.File;
 2 import java.io.IOException;
 3 import org.apache.commons.io.FileUtils;
 4 import org.apache.struts2.ServletActionContext;
 5 
 6 public class UploadAction {
 7     private File img; //文件
 8     private String imgFileName;  //文件名:文件+FileName
 9     
10     public String execute() throws IOException{
11         
12         if(img != null){
13         String path = ServletActionContext.getServletContext().getRealPath("/images");
14         File destFile = new File(path, imgFileName);
15         FileUtils.copyFile(img, destFile);
16         return "success";
17         }
18         return "message";
19     }
20     
21     public File getImg() {
22         return img;
23     }
24     public void setImg(File img) {
25         this.img = img;
26     }
27     public String getImgFileName() {
28         return imgFileName;
29     }
30     public void setImgFileName(String imgFileName) {
31         this.imgFileName = imgFileName;
32     }
33 }
复制代码

 

3.下载action

复制代码
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;

public class DownloadAction {
    private InputStream is;  //输入流,先将文件读入服务端的内存
    private String fileName; //随意    
    
    public String execute() throws FileNotFoundException, UnsupportedEncodingException{
        fileName = "1.png";//设置路径,在项目中通过连接DB获取文件名
        
        is = ServletActionContext.getServletContext().getResourceAsStream("/images/"+fileName);//获得文件流
    
byte[] bytes = fileName.getBytes("utf-8");
    fileName = new String(bytes, "iso-8859-1");
        return "success";
    }

    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;
    }
}
复制代码

[注意]:

1. 下载时出现500:Can not find a java.io.InputStream with the name [is] in the invocation stack. Check the <param nam

  解决思路:1)查看is输入流是否为null,若为空则没有获取到正确路径,检查文件名是否正确

         2)在action中没有写配置文件中"<param name="inputName">"后面属性的那个get方法

         3)当采用 return ServletActionContext.getServletContext().getResourceAsStream("...") 这种方法获得输入流的时候,要保证文件位置在 ServletContext 当中,就是说要在当前的应用上下文中,如果想要获得外部文件 譬如 D盘中的某个文件,那么就要自己创建输入流才可以,如:

File file = new File("D://spring.doc");  
InputStream is = new FileInputStream(file);  
return is; 

 2.编码问题

  tomcat服务器端的编码是utf-8,通常情况下不用fileName = new String(fileName.getBytes("iso-8859-1"),"UTF-8");设置编码,而在输出端使用

byte[] bytes = fileName.getBytes("utf-8");
fileName = new String(bytes, "iso-8859-1");

设置编码,编码类型和浏览器端编码类型有关

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值