|
|
@@ -143,7 +143,7 @@ public class QrcodeAddServiceImpl implements IQrcodeAddService {
|
|
|
//主码
|
|
|
String masterCode = getMasterCode();
|
|
|
Boolean isUpload = true;
|
|
|
-
|
|
|
+ byte[]zipData = new byte[0];
|
|
|
//qrCodeID
|
|
|
String status = "";
|
|
|
//添加产码记录
|
|
|
@@ -178,12 +178,12 @@ public class QrcodeAddServiceImpl implements IQrcodeAddService {
|
|
|
sourceFilePath = getTxtPath(num, filePath, maxNum, tablename,code,
|
|
|
addNum, createTime, url ,type);
|
|
|
}else{
|
|
|
- qrcodeZipFile = generateQrcodeZip(num,filePath,maxNum,tablename,code);
|
|
|
+ zipData = generateQrcodeZipInMemory(num, maxNum, tablename, code);
|
|
|
}
|
|
|
}
|
|
|
map.remove(id);
|
|
|
if(isApply){
|
|
|
- return uploadToOSSWithInputStream(filePath,qrcodeZipFile);
|
|
|
+ return uploadToOSSWithInputStream(filePath,zipData);
|
|
|
}
|
|
|
String productName = Db.queryStr("select item_name name_ from t_jz_item where id = ?",type);
|
|
|
return getFilePath(filePath, sourceFilePath,num,productName);
|
|
|
@@ -307,11 +307,9 @@ public class QrcodeAddServiceImpl implements IQrcodeAddService {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- private File generateQrcodeZip(int count, String zipFilePath, long maxNum,
|
|
|
- String tableName, String url) {
|
|
|
- String txtFileName = DateUtils.getStringFullDate() + "-" + count+".txt";
|
|
|
- // SQL 查询
|
|
|
+ public byte[] generateQrcodeZipInMemory(int count, long maxNum,
|
|
|
+ String tableName, String url) {
|
|
|
+ String txtFileName = DateUtils.getStringFullDate() + "-" + count + ".txt";
|
|
|
String sql = "select id, num_, random_ from " + tableName + " where num_ > ? order by num_ asc";
|
|
|
List<Record> idList = Db.find(sql, maxNum);
|
|
|
|
|
|
@@ -319,12 +317,10 @@ public class QrcodeAddServiceImpl implements IQrcodeAddService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- File zipFile = new File(zipFilePath);
|
|
|
-
|
|
|
- try (FileOutputStream fos = new FileOutputStream(zipFile);
|
|
|
- ZipOutputStream zos = new ZipOutputStream(fos)) {
|
|
|
+ // 使用字节数组输出流,在内存中操作
|
|
|
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(baos)) {
|
|
|
|
|
|
- // zip里放一个txt文件
|
|
|
ZipEntry entry = new ZipEntry(txtFileName);
|
|
|
zos.putNextEntry(entry);
|
|
|
|
|
|
@@ -332,19 +328,17 @@ public class QrcodeAddServiceImpl implements IQrcodeAddService {
|
|
|
for (int j = 0; j < size; j++) {
|
|
|
String strId = idList.get(j).get("id").toString();
|
|
|
zos.write(strId.toLowerCase().getBytes(StandardCharsets.UTF_8));
|
|
|
-
|
|
|
- // 除了最后一行,都换行
|
|
|
if (j < size - 1) {
|
|
|
zos.write("\n".getBytes(StandardCharsets.UTF_8));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
zos.closeEntry();
|
|
|
- zos.flush();
|
|
|
+ // zos关闭时会自动写入baos, 无需flush
|
|
|
|
|
|
- return zipFile;
|
|
|
+ // 返回内存中的zip文件内容的字节数组
|
|
|
+ return baos.toByteArray();
|
|
|
} catch (Exception e) {
|
|
|
- logger.error("创建zip文件失败", e);
|
|
|
+ logger.error("在内存中创建zip文件失败", e);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -482,9 +476,12 @@ public class QrcodeAddServiceImpl implements IQrcodeAddService {
|
|
|
* @param fileName 文件名
|
|
|
* @return OSS访问地址
|
|
|
*/
|
|
|
- private String uploadToOSSWithInputStream(String fileName,File zipFile){
|
|
|
+ private String uploadToOSSWithInputStream(String fileName,byte[]zipData){
|
|
|
try {
|
|
|
- InputStream inputStream = Files.newInputStream(zipFile.toPath());
|
|
|
+ if (zipData == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ InputStream inputStream = new ByteArrayInputStream(zipData);
|
|
|
return OssUtil.upload(inputStream, fileName);
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|