下载并解决中文乱码问题

阅读更多
log.info("进入CmsStaffAct.v_export");
try {


CmsSite site = cmsSiteMng.findById(id);

if(site==null){
throw new Exception("企业不存在");
}

//获取站点下面所有的号码,并下载
String realPath = request.getSession().getServletContext().getRealPath("/upload")+"/";
String fileUrl = staffMng.exportDeal(realPath,site.getShortName(),id);

response.setContentType(ExportFileType.ZIP.desc);// 不同类型的文件对应不同的MIME类型
response.setCharacterEncoding("utf-8");

String zipName = site.getShortName()+".zip";

String agent = request.getHeader("User-Agent");
boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
if (isMSIE) {
zipName = URLEncoder.encode(zipName, "UTF-8");
}else{
zipName =new String(zipName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setHeader("content-disposition", "attachment;filename=" + zipName); 
log.info("退出CmsStaffAct.v_export,fileUrl:"+fileUrl);

BufferedOutputStream bos = null;
BufferedInputStream  bis = null;
try {
            bis = new BufferedInputStream(new FileInputStream(fileUrl));       
            bos = new BufferedOutputStream(response.getOutputStream());
           
            byte[] buff = new byte[2048];
            int bytesRead;


            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff,0,bytesRead);
            }
        } catch(final IOException e) {
        e.printStackTrace();
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("");
        } catch(Exception e) {
        e.printStackTrace();
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("");
        }finally {
            if (bis != null)
                bis.close();
            if (bos != null)
            {
                bos.flush();
                bos.close();
                bos=null;
            }
        }
        response.flushBuffer();
} catch(Exception e) {
        e.printStackTrace();
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("");
        response.flushBuffer();
    }

你可能感兴趣的:(下载)