在程序开发过程中,客户的需求是要将多Excel打包下载。对此,我们采取的实现形式为:
1.将Excel下载到临时目录下。
2.将临时目录下的多Excel文件,进行打包,再输出到临时目录下
3.请求调用浏览器下载,将Zip文件下载到客户端,删除临时目录下所有文件
部分代码整理:
/**
* ファイルを圧縮する
* @param srcfile
* @param zipName
* @param encode
* @return
*/
public boolean zipFiles(List<String> srcfile, String zipName, EncodeMode encode) {
logger.info("【開始】ファイル圧縮処理");
boolean result = false;
byte[] buf = new byte[2048];
org.apache.tools.zip.ZipOutputStream out = null;
try {
out = new org.apache.tools.zip.ZipOutputStream(new FileOutputStream(zipName + Extension.ZIP.value));
if (encode != null) {
out.setEncoding(encode.value());
}
for (int i = 0; i < srcfile.size(); i++) {
String filePath = srcfile.get(i);
File file = new File(filePath);
if (file.exists()) {
logger.info("【ファイルパス】" + this.getPath(filePath));
logger.info("【ファイル名】" + file.getName());
FileInputStream in = new FileInputStream(filePath);
out.putNextEntry(new org.apache.tools.zip.ZipEntry(file.getName()));
int bytesRead;
while ((bytesRead = in.read(buf)) > 0) {
out.write(buf, 0, bytesRead);
}
out.flush();
out.closeEntry();
in.close();
} else {
logger.warn("ファイルが見つかりません。(" + filePath + ")");
}
}
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
①在这段代码中,我们首先将
out.setEncoding("UTF-8");
使用WinRAR解压工具,本地测试通过,日文中文均无乱码问题。
②但在客户验收过程中,Zip包解压缩后,Excel文件名,出现日文乱码,客户在日本,使用Lhaplus解压工具。
对于客户抛来的问题,进行各种调查,修正,第一次修正结果:
out.setEncoding("Shift-JIS");
再次确认过程中,依旧有部分日文出现乱码
③由于Lhaplus解压工具在客户公司的使用范围之广,再次调查,修正,最终解决方案:
out.setEncoding(System.getProperty("sun.jnu.encoding"));
(System.getProperty("sun.jnu.encoding"):获取系统环境变量中的,文件名编码格式)
截止目前,客户确认通过,无乱码问题。