struts2 实现文件下载并解决中文文件名乱码问题

/**
 * @Title  BaseAction.java
 * @Package com.xdja
 * @Description  
 * @author 张佳
 * @date 2012-4-19 下午04:11:30
 * @version v1.0
 * @copyright Copyright 2012
 * Company : 郑州信大捷安信息技术股份有限公司
 */
package com.xdja.wap;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = -72078345049879488L;
	protected final Logger log = Logger.getLogger(BaseAction.class);
	private InputStream inputStream;
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public InputStream getInputStream()throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Content-Disposition", "attachment;fileName="+ java.net.URLEncoder.encode(fileName,"UTF-8"));//header这样设置就能解决附件中文乱码问题
		return inputStream;
	}

	private String fileName;// 要下载的附件名称

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/**
	 * 下载附件
	 * 
	 * @Title getDownLoad
	 * @Description
	 * @return
	 * @author 张佳
	 * @date 2012-4-24 上午09:43:11
	 */
	public void fileDownLoad(String fileName, String path)throws Exception {
		FileInputStream inputSteam = null;
		this.fileName = fileName;
		File file = new File(path);
		inputSteam = new FileInputStream(file);
		this.setInputStream(inputSteam);
	}
}

 2.struts.xml配置

  

<!-- 新闻下载 -->
		<action name="newsdownload" class="com.xdja.wap.news.NewsAction" method="newsDownLoad">
			<result name="success" type="stream">
				<param name="contentDisposition">attachment;filename="${fileName}"</param>
				<param name="inputName">inputStream</param>
				 <param name="bufferSize">1024</param>
			</result>
		</action>

 

3.测试类

public String noticeDownLoad(){
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			this.setFileName(new String(this.getFileName().getBytes("ISO-8859-1"), "UTF-8"));
			//this.getFileName()接收前台传过来的文件名,这里为“张佳1234.doc”
			String attachmentPath = request.getParameter("attPath");//获取附件存储的路径
			if (!HelpFunctions.isEmpty(this.getFileName()) && !HelpFunctions.isEmpty(attachmentPath)) {
				String path = SysConfigInfo.getElementAttributeValue("System/SnapShotPath", "value") + Constants.NOTICES_BASE_PATH + "/" + attachmentPath + "/" + this.getFileName();
				//这里的path路径为G:\newswork\张佳1234.doc 也就是要下载的文件路径
				this.fileDownLoad(this.getFileName(), path);//调用下载方法
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

 4.效果图

  晕 怎么上传图片? 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值