下载excel文件名字乱码问题处理

package com.dreamwin.cclib.naga.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

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

import com.dreamwin.statis.StartUp;

/**
 * 输出文件视图
 *
 *
 * @author niuxx
 *
 */
public class FILEView extends BaseView implements View {

    /** 需要输出的文件路径 */
    private String filePath;

    /**
     * 带模板路径的构造器
     *
     * @param templateName
     *            模板路径
     */
    public FILEView(String filePath) {
        this.filePath = filePath;
    }

    public void render(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        super.render(request, response);
        // response.setCharacterEncoding("UTF-8");
        String cStr1 = filePath;
        // cStr1 = URLDecoder.decode(filePath,"UTF-8");
        String view_name = filePath.substring(filePath.lastIndexOf("/") + 1);
        String cFilePath;
        cFilePath = StartUp.deployDirectory + cStr1;
        File file = new File(cFilePath);
        if (!file.exists()) {
            return;
        }
        File downFile = new File(cFilePath);
        response.setContentType("binary/octet-stream;charset=UTF-8");
        // response.setContentType("application/x-msdownload");
        // 判断浏览器版本,设置相应编码格式
        String userAgent = request.getHeader("User-Agent");
        System.out.println(userAgent);
        userAgent = userAgent.toLowerCase();
        if (userAgent.indexOf("msie") != -1) {
            //response.setHeader("Content-Disposition", "attachment; filename=\""
            //    + URLEncoder.encode(view_name, "UTF-8") +"\"");
        response.setHeader( "Content-Disposition", "attachment;filename="  + new String(view_name.getBytes("gb2312"), "ISO8859-1" ));
        } else {
            String fileName = new String(view_name.getBytes("UTF-8"),
                    "ISO8859-1");
            response.setHeader("Content-Disposition", "attachment; filename=\""
                    + fileName + "\"");
        }
        ServletOutputStream out = response.getOutputStream();
        InputStream is = new FileInputStream(downFile);
        byte[] bytes = new byte[1024];
        while (true) {
            int chunk = is.read(bytes);
            if (chunk == -1) {
                break;
            }
            out.write(bytes, 0, chunk);
        }
        is.close();
        is = null;
        out.flush();
        out.close();

    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值