Java strtus2文件下载不同浏览器文件名中文乱码处理

在IE,FF,chrome中文件名都不会乱码,在其他浏览器中,请参考《不同浏览器上中文文件名的下载乱码问题

jsp:

<a href="<%=path %>/downloadProFile.action?fileName=${data.app_name }&fileUrl=${data.app_url }">下载</a>

strtus:

		<action name="downloadProFile" class="com.actions.DownloadAction">
		    <result name="success" type="stream">
		    <!--设置文件类型-->
		    <param name="contentType">application/octet-stream;charset=ISO8859-1</param> 
		    <!--下载文件流-->
		    <param name="inputName">inputStream</param>
		    <!--添加参数,即就是下载的名称-->
		    <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
		    <!--缓存-->
		    <param name="bufferSize">4096</param>
		    </result>
		 </action>

DownloadAction.java

package com.actions;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.actions.common.BaseAction;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;

/**
 * 功能:下载
 * @author Adam
 *
 */
public class DownloadAction extends BaseAction {
	//兼容linux
	String fs = System.getProperties().getProperty("file.separator");
	protected String configPath = "attached";
	protected String dirTemp = "attached"+fs+"temp"; 
	public String downloadFileName;
	public InputStream inputStream;
	
	/**
	 * 功能:设置返回下载文件名
	 * @return
	 */
	public String getDownloadFileName() {
		String fileName = getRequest().getParameter("fileName").trim();
		String agent = getRequest().getHeader("USER-AGENT");
		try {
			fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
    		if(null != agent && -1 != agent.indexOf("MSIE")){
    			fileName = URLEncoder.encode(fileName,"UTF8");
    			//将加号还原为空格(URLEncoder.encode()会将空格转成加号)
    			fileName = fileName.replaceAll("\\+", "%20");
    		}else if(null != agent && -1 != agent.indexOf("Mozilla")){
    			fileName = MimeUtility.encodeText(fileName,"UTF8","B");
		    }
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return fileName;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

	/**
	 * 功能:下载文件,设置文件流
	 * File.separator
	 * @return
	 */
	public InputStream getInputStream() {
		String fileUrl = getRequest().getParameter("fileUrl").trim();
		fileUrl = fileUrl.replace("/", fs);
        String savePath = getSession().getServletContext().getRealPath(fs) + configPath; 
        String realPath = fs + configPath + fs + fileUrl;
        File file = new File(savePath + fs + fileUrl);
		if (file.isFile() && file.exists()) {
			getLog().logger.error("realPath:" + realPath);
			System.out.println(realPath);
			return ServletActionContext.getServletContext().getResourceAsStream(realPath);
		}else{
	        String tempPath = getSession().getServletContext().getRealPath(fs) + dirTemp;
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");  
	        String ymd = sdf.format(new Date());
	        tempPath += fs + ymd + fs;
	        File dirTempFile = new File(tempPath);
	        if (!dirTempFile.exists()) {
	            dirTempFile.mkdirs();  
	        }
	        File f = new File(tempPath + this.getDownloadFileName());
	        if (!f.exists()) {
	        	try {
					f.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
	        }
			String inExistPath = fs + dirTemp + fs + ymd + fs + this.getDownloadFileName();
			return ServletActionContext.getServletContext().getResourceAsStream(inExistPath);
		}
	}
	
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String execute() {
		return "success";
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上富贵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值