private void outPutFile(HttpServletRequest request,
HttpServletResponse response,String type){
response.setCharacterEncoding("UTF-8"); //Servlet的输出设定
String fileContent = "";
ArrayList<Integer> handleNoList = new ArrayList<Integer>();
try{
String fileContent = getFileContent();
if (fileContent.length() > 0) {
response.setContentType("text/plain; charset=UTF-8"); //客户端输出设定
PrintWriter out = response.getWriter(); // PrintWriter 的定义一定要放在 response.setContentType之后才可以防止乱码地出现。
String currTime = getCurrentTime();
//文件名乱码解决方案(这里的编码格式是日语系统的)
if(request.getHeader("User-Agent").indexOf("MSIE") == -1)
response.setHeader("Content-Disposition", "attachment; filename=" + MimeUtility.encodeWord("テスト"+currTime+"."+type, "ISO-2022-JP", "B"));
else
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("テスト"+currTime+"."+type, "UTF-8"));
out.append(fileContent);
} else {
response.setContentType("text/html; charset=UTF-8");
}
}catch(Exception ex){
System.out.printl(ex.toString);
}
}
setContentType的设定: 种类 扩展名 Content-Type
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HTML文件 .html .htm text/html
文本文件 txt,csv,tsv.. text/plain
GIF .gif image/gif
PNG .png image/png
Windows执行程序 .exe application/octet-stream
posted on 2009-02-07 12:05
TRE-China R&D 阅读(316)
评论(0) 编辑 收藏 引用 所属分类:
JAVA