java吧 关注:1,236,339贴子:12,704,743
  • 2回复贴,共1

java web文件下载,如何让下载的文件自动保存为UTF-8+BOM格式?

只看楼主收藏回复

用setCharacterEncoding("UTF-8")只能保存为UTF-8格式。
难道要在程序中显式写入那头三个字节?


IP属地:北京1楼2014-11-19 15:56回复
    好吧,问题已解决,用那个“难道要”还真解决了。
    String videoIDStr = videoID.toString();
    videoIDStr = "0000" + videoID;
    String fileName = "kcjy" + videoIDStr.substring(videoIDStr.length() - 4) + ".html";
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    try {
    if (out != null) {
    byte[] bomStrByteArr = new byte[]{(byte)0xef,(byte)0xbb,(byte)0xbf};
    String bomStr = new String(bomStrByteArr, "UTF-8");
    out.write(bomStr);
    out.write(sb.toString());
    }
    手动写入BOM的头三个字节,下载下来的文件还真就成UTF-8+BOM格式了


    IP属地:北京2楼2014-11-19 16:20
    收起回复