附件上传

2024年8月16日 13:56:42
This commit is contained in:
xiang2lin 2024-08-16 13:56:51 +08:00
parent 2a49ba7221
commit 512ccb5157
1 changed files with 46 additions and 38 deletions

View File

@ -1001,44 +1001,7 @@ public class OerDjmlServiceImpl implements IOerDjmlService {
djml.setCurshjd("999");
djml.setNextshjd("-1");
//查询协同附件
List<CollAttachmentResDTO> colAttachmentList = restUtil.getColAttachments(bodyObj.getString("summaryId"), "0", "fileApiCode", "hzya");
//下载协同附件
if (CollectionUtils.isNotEmpty(colAttachmentList)){
List<FileInfoDTO> fileList = new ArrayList<>();
for (CollAttachmentResDTO att : colAttachmentList) {
FileInfoDTO fileInfoDTO = new FileInfoDTO();
String fileName = URLDecoder.decode(att.getFilename(), "UTF-8");
byte[] bytes = restUtil.downloadFileBytes("hzya", fileApiCode, att.getFileUrl(), fileName);
String filePath = DSK+File.separator+DateUtil.today()+File.separator+att.getFileUrl()+"."+att.getExtension();
File file = new File(filePath);
// 使用 FileOutputStream 写入字节数组到文件
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(bytes);
}
if (null != file){
String zipPath = DSK+File.separator+DateUtil.today()+File.separator+att.getSubReference()+".zip";
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath), Charset.forName("GBK"));
writeZipFile(file, zos, fileName);
zos.close();
// 获取压缩文件的字节数组
File zipFile = new File(zipPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream((int) zipFile.length());
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(zipFile));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = bufferedInputStream.read(buffer, 0, buf_size))) {
byteArrayOutputStream.write(buffer, 0, len);
}
byte[] fileByte = byteArrayOutputStream.toByteArray();
fileInfoDTO.setData(fileByte);
fileInfoDTO.setFile_size(att.getSize());
fileInfoDTO.setFile_name(fileName);
fileList.add(fileInfoDTO);
}
}
}
List<FileInfoDTO> fileInfoList = fileDownload(seeyonData.getString("summaryId"),fileApiCode);
}
String djmlStr = JSONObject.toJSONString(djml);
logger.info("差旅报销单报文:{}",djmlStr);
@ -1061,6 +1024,51 @@ public class OerDjmlServiceImpl implements IOerDjmlService {
}
return null;
}
private List<FileInfoDTO> fileDownload(String summaryId,String apiCode){
try {
List<CollAttachmentResDTO> colAttachmentList = restUtil.getColAttachments(summaryId, "0", apiCode, "hzya");
//下载协同附件
if (CollectionUtils.isNotEmpty(colAttachmentList)){
List<FileInfoDTO> fileList = new ArrayList<>();
for (CollAttachmentResDTO att : colAttachmentList) {
FileInfoDTO fileInfoDTO = new FileInfoDTO();
String fileName = URLDecoder.decode(att.getFilename(), "UTF-8");
byte[] bytes = restUtil.downloadFileBytes("hzya", apiCode, att.getFileUrl(), fileName);
String filePath = DSK+File.separator+DateUtil.today()+File.separator+att.getFileUrl()+"."+att.getExtension();
File file = new File(filePath);
// 使用 FileOutputStream 写入字节数组到文件
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(bytes);
}
if (null != file){
String zipPath = DSK+File.separator+DateUtil.today()+File.separator+att.getSubReference()+".zip";
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath), Charset.forName("GBK"));
writeZipFile(file, zos, fileName);
zos.close();
// 获取压缩文件的字节数组
File zipFile = new File(zipPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream((int) zipFile.length());
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(zipFile));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = bufferedInputStream.read(buffer, 0, buf_size))) {
byteArrayOutputStream.write(buffer, 0, len);
}
byte[] fileByte = byteArrayOutputStream.toByteArray();
fileInfoDTO.setData(fileByte);
fileInfoDTO.setFile_size(att.getSize());
fileInfoDTO.setFile_name(fileName);
fileList.add(fileInfoDTO);
}
}
}
}catch (Exception e){
logger.error("下载附件出错:{}",e);
}
return null;
}
/**
* 把文件压缩成zip
* @param file 要压缩的文件