小文件通过接口参数传递

文件通过base64转换的字符

private static void updateFileInfo(File file, DocumentInfo docInfo) throws IOException, ArchiveException {
	DateParams datePamsInfo = new DateParams();
	Date date = new Date();
	datePamsInfo.setDocumentDisbursementDate((new SimpleDateFormat("yyyyMMdd").format(date)));
	datePamsInfo.setDocumentReceivedDate(new SimpleDateFormat("yyyyMMdd").format(date));
	docInfo.setDate_params(datePamsInfo);
	String[] fileNameArry = file.getName().split("\\.");
	docInfo.setFilename(fileNameArry[0] + ".zip");
	docInfo.setFileExtension("zip");
	String fileName = file.getName().toLowerCase();
	boolean isZipFile = fileName.endsWith(".zip");
	byte[] zipByte = null;
	if (!isZipFile) {
		log.info("it is not zip file");
		try (FileInputStream in = new FileInputStream(file)) {

			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			ArchiveOutputStream archOuts = new ArchiveStreamFactory()
					.createArchiveOutputStream(ArchiveStreamFactory.ZIP, baos);
			if (archOuts instanceof ArchiveOutputStream) {
				ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;
				ZipArchiveEntry zipEntry = new ZipArchiveEntry(file, file.getName());
				zipOut.putArchiveEntry(zipEntry);
				byte[] buffer = new byte[1024];
				int bytes_read;
				while ((bytes_read = in.read(buffer)) != -1) {
					zipOut.write(buffer, 0, bytes_read);
				}
				in.close();
				zipOut.closeArchiveEntry();
				zipOut.flush();
				zipOut.close();
			}

			zipByte = baos.toByteArray();
			baos.close();
			baos = null;
		}
	} else {
		// 注意file length 最大Integer.MAX_VALUE
		zipByte = FileUtils.readFileToByteArray(file);
	}
	docInfo.setFileChecksum(DigestUtils.md5Hex(zipByte));
	docInfo.setFileContents(new String(Base64.encodeBase64(zipByte)));
	docInfo.setFileSize(String.valueOf(new Integer(
			(new BigDecimal(zipByte.length).divide(new BigDecimal(1024), 0, BigDecimal.ROUND_UP))
					.intValue())));
}

解析base64处理的字符串

String fileName;
int length;
String name;
if (batch) {
	fileName = contentStr;
	name = contentStr.substring(contentStr.lastIndexOf("/") + 1);
	length = ResultConstants.BATCH_FILE_LENGTH; // 1000
} else {
	String filePath = getAttachmentPath(marker, txnId);
	File files = new File(filePath);
	if (!files.exists()) {
		log.info("create filePath :" + filePath);
		files.mkdirs();
	}
	fileName = filePath + "/" + resultResponse.getReportNo() + ResultConstants.SUFFIX;
	name = resultResponse.getReportNo() + ResultConstants.SUFFIX;
	BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(fileName)));
	length = contentStr.length();
	int size = length / ResultConstants.STRLENGTH;
	int start = 0;
	int end = 0;
	for (int i = 0; i < size; i++) {
		start = i * ResultConstants.STRLENGTH;
		if (i == size - 1) {
			end = length;
		} else {
			end = (i + 1) * ResultConstants.STRLENGTH;
		}
		outputStream.write(Base64.getDecoder().decode(contentStr.substring(start, end).getBytes(StandardCharsets.UTF_8)));
	}
	outputStream.flush();
	outputStream.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值