下载并解决中文乱码问题
程序员文章站
2022-06-18 16:21:08
...
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("<script type=\"text/javascript\">alert(\""+e.getMessage()+"\")</script>");
} catch(Exception e) {
e.printStackTrace();
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("<script type=\"text/javascript\">alert(\""+e.getMessage()+"\")</script>");
}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("<script type=\"text/javascript\">alert(\""+e.getMessage()+"\")</script>");
response.flushBuffer();
}
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("<script type=\"text/javascript\">alert(\""+e.getMessage()+"\")</script>");
} catch(Exception e) {
e.printStackTrace();
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("<script type=\"text/javascript\">alert(\""+e.getMessage()+"\")</script>");
}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("<script type=\"text/javascript\">alert(\""+e.getMessage()+"\")</script>");
response.flushBuffer();
}
上一篇: 基于Java的一个简单的文件上传下载功能
下一篇: wps页眉下面的横线去掉方法