关于生成文档后使用http下载方式打开

 
只需新建一个类继承抽象类HttpDownloadAction ,实现抽象方法getRootDirectory();同时将filename传进来即可。
HttpDownloadAction :
package com.---.downloadManager;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.xwork.ActionSupport;
import com.---.DownloadConfig;

public abstract class HttpDownloadAction extends ActionSupport
implements ServletResponseAware, ServletRequestAware {
	
	public abstract String getRootDirectory();
	
	private static final long serialVersionUID = 1L;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private String prefix;  
	private DownloadConfig downloadConfig; 
	private String filename; 
	private String message;
	private String fileDir;
	public String execute() throws Exception {
		fileDir = this.getRootDirectory();
		
		if (filename == null || filename.equals("")) {
			message = "文件下载路径为空!";
			return SUCCESS;	
		}
		filename =  new String(filename.getBytes("ISO-8859-1"),"UTF-8");
		String fileDownLoadPath = getFileDownloadPath() ;
		File file = new File(fileDownLoadPath);
		if (!file.exists()) { 
			message = "文件" + filename + "不存在!";
			return SUCCESS;	
        }
		
		FileInputStream fis = null;
		ServletOutputStream os = null;
		try {
			
			prefix = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
		    Map<String, String> str = downloadConfig.getResponseContentType();
			response.setContentType(downloadConfig.getResponseContentType().get(prefix));
			String userAgent = request.getHeader("User-Agent");
			if(userAgent.indexOf("MSIE") != -1) 
				filename = "filename=" + java.net.URLEncoder.encode(filename,"UTF-8");
			else
				filename = "filename*=utf8''"  + java.net.URLEncoder.encode(filename,"UTF-8");
			response.setHeader("content-Disposition", "attachment;" + filename);  
			os = response.getOutputStream();
	        fis = new FileInputStream(fileDownLoadPath);
	        byte[] buff = new byte[1024];
	        int n = 0;    
            while ((n = fis.read(buff)) != -1) {    
                 os.write(buff, 0, n);    
             }    
			return NONE;
		} catch (Exception e) {    
			System.out.println(e+"...");
			message = "文件" + filename + "不存在!";
        } finally {        	
        	if(fis != null)
        		fis.close();
        	if(os != null)
        		os.flush();
		}
		return SUCCESS;	
	}
	private String getFileDownloadPath() {
		if(fileDir == null || fileDir.equals("")){
			return filename;
		}else{
			return fileDir + "\\" + filename;
		}
		
	}
	public void setServletResponse(HttpServletResponse reponse) {
		response = reponse;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public DownloadConfig getDownloadConfig() {
		return downloadConfig;
	}
	public void setDownloadConfig(DownloadConfig downloadConfig) {
		this.downloadConfig = downloadConfig;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
}
 

    DownloadConfig:

package com.---.config;

import java.util.Map;

public class DownloadConfig {
	private Map<String,String> responseContentType;

	public Map<String, String> getResponseContentType() {
		return responseContentType;
	}

	public void setResponseContentType(Map<String, String> responseContentType) {
		this.responseContentType = responseContentType;
	}
}

 

 

 

  downloadConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http//www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName">
	<bean id="downloadConfig" class="com.viathink.lims.center.config.DownloadConfig" singleton="true">
		<!-- 下载文件时不同文件类型对应的response contentType配置 -->
		<property name="responseContentType">
			<map>
					<entry key="doc"><value>application/msword</value></entry>
					<entry key="xls"><value>application/vnd.ms-excel</value></entry>
					<entry key="txt"><value>text/plain</value></entry>
			</map>
		</property>
	</bean>
</beans>

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值