Lhaplus解压工具,乱码问题对应方案(java打包日文乱码)

在程序开发过程中,客户的需求是要将多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"):获取系统环境变量中的,文件名编码格式)

截止目前,客户确认通过,无乱码问题。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值