文件通过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();
}