关闭

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

标签: Java处理
1063人阅读 评论(0) 收藏 举报
分类:
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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:199884次
    • 积分:3553
    • 等级:
    • 排名:第9623名
    • 原创:152篇
    • 转载:109篇
    • 译文:0篇
    • 评论:17条
    最新评论