struts2 文件下载及临时文件的删除

1.文件下载配置
Struts2提供了stream结果类型,该结果类型是专门用于支持文件下载功能的。配置stream类型的结果需要指定以下4个属性。

  contentType:指定被下载文件的文件类型

  inputName:指定被下载文件的入口输入流

  contentDisposition:指定下载的文件名

  bufferSize:指定下载文件时的缓冲大小

struts2文件下载示例:

1) Action类
public class DownLoadFileAction {
    //input
    private int id;
    
    //output
    private String downLoadFileName;
 
    public String execute(){
        
        return "success";
    }
    /**
      * 读取文件流
     * @return
     * @throws Exception
     */
    public InputStream getInputStream() throws Exception{
        
        RedisManagerModel model = new RedisManagerModel();
        InputStream is = null;
        try {
            
            sample = (Sample) session.get(Sample.class, id);
            //正常输出报告文件
            String downLoadFilePath = sample.getFilePath();
             File file = new File(downLoadFilePath);
             if(!file.exists()){
                 this.downLoadFileName="error.txt";
                 ByteArrayInputStream tInputStringStream = new ByteArrayInputStream("The System Cannot Find the File Specified".getBytes());
                 return tInputStringStream;
             }
             this.downLoadFileName=sample.getFileName();
             //转码
             this.downLoadFileName = new String(this.downLoadFileName.getBytes(), "ISO8859-1");
             is=new FileInputStream (file);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            HibernateSessionFactory.closeSession();
        }
        return is ; 
     }
    
 
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getDownLoadFileName() {
        return downLoadFileName;
    }
    public void setDownLoadFileName(String downLoadFileName) {
        this.downLoadFileName = downLoadFileName;
    }
    
}
2)配置struts.xml
<!-- 文件下载 -->
        <action name="downloadFile" class="xxx.xxx.action.sample.DownLoadFileAction">
            <result name="success" type="stream" >
                <param name="contentDisposition">attachment;fileName="${downLoadFileName}"</param>
                <!--指定action中返回被下载文件的InputStream的名称-->   
                 <param name="inputName">inputStream</param>  
               <!--指定下载文件的缓冲大小-->
                <param name="bufferSize">1024</param> 
            </result>
        </action>
2. 临时文件删除问题

sturts2下载是通过文件流的方式实现的,当文件正在下载的时候,因为文件流未关闭,文件句柄已被占用,所以文件无法删除.

解决问题的思路:

1).首先通过输入流/输出流将文件转成byte[]放入缓存中

2).将byte[]转成struts2要求的输入流 

3).关闭文件的输入流,并删除文件

action方法改动如下:

public class DownLoadFileAction {
    //input
    private int id;
    
    //output
    private String downLoadFileName;
 
    public String execute(){
        
        return "success";
    }
    /**
      * 读取文件流
     * @return
     * @throws Exception
     */
    public InputStream getInputStream() throws Exception{
        
        RedisManagerModel model = new RedisManagerModel();
        InputStream is = null;
        try {
            
            sample = (Sample) session.get(Sample.class, id);
            //正常输出报告文件
            String downLoadFilePath = sample.getFilePath();
             File file = new File(downLoadFilePath);
             if(!file.exists()){
                 this.downLoadFileName="error.txt";
                 ByteArrayInputStream tInputStringStream = new ByteArrayInputStream("The System Cannot Find the File Specified".getBytes());
                 return tInputStringStream;
             }
             this.downLoadFileName=sample.getFileName();
             //转码
             this.downLoadFileName = new String(this.downLoadFileName.getBytes(), "ISO8859-1");
             //is=new FileInputStream (file);
            FileInputStream fis = new FileInputStream (file);
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
            byte[] buff = new byte[1024];  
            int rc = 0;  
            while ((rc = fis.read(buff, 0, 1024)) > 0) {  
                swapStream.write(buff, 0, rc);  
            }
            is=new ByteArrayInputStream(swapStream.toByteArray());
                fis.close();
                swapStream.close();
                file.delete();//删除文件
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            HibernateSessionFactory.closeSession();
        }
        return is ; 
     }
    
 
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getDownLoadFileName() {
        return downLoadFileName;
    }
    public void setDownLoadFileName(String downLoadFileName) {
        this.downLoadFileName = downLoadFileName;
    }
    
}
 

发布了98 篇原创文章 · 获赞 68 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/jarniyy/article/details/104919248