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

1 篇文章 0 订阅
1 篇文章 0 订阅

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;
	}
	
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值