This commit is contained in:
yurui 2023-05-06 16:02:40 +08:00
父節點 6f0a0ce3c1
當前提交 18415e3fa6
共有 3 個檔案被更改,包括 1 行新增126 行删除

查看文件

@ -9,23 +9,19 @@ import com.weilab.biology.core.data.dto.AppConfigDto;
import com.weilab.biology.core.data.dto.JobDto; import com.weilab.biology.core.data.dto.JobDto;
import com.weilab.biology.core.data.dto.JobLessDto; import com.weilab.biology.core.data.dto.JobLessDto;
import com.weilab.biology.core.data.enums.JobStatusEnum; import com.weilab.biology.core.data.enums.JobStatusEnum;
import com.weilab.biology.core.data.po.AppConfig;
import com.weilab.biology.core.data.po.Job; import com.weilab.biology.core.data.po.Job;
import com.weilab.biology.core.data.vo.result.CommonError; import com.weilab.biology.core.data.vo.result.CommonError;
import com.weilab.biology.core.data.vo.result.Result; import com.weilab.biology.core.data.vo.result.Result;
import com.weilab.biology.core.data.vo.result.error.JobError; import com.weilab.biology.core.data.vo.result.error.JobError;
import com.weilab.biology.mapper.JobMapper; import com.weilab.biology.mapper.JobMapper;
import com.weilab.biology.util.FileUtils;
import com.weilab.biology.util.MailUtil; import com.weilab.biology.util.MailUtil;
import com.weilab.biology.util.TaskExecutorUtil; import com.weilab.biology.util.TaskExecutorUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
@ -66,7 +62,7 @@ public class JobService extends ServiceImpl<JobMapper, Job> {
if (dataFile != null) { if (dataFile != null) {
FileUtil.writeFromStream(dataFile.getInputStream(), dataPath); FileUtil.writeFromStream(dataFile.getInputStream(), dataPath);
} else if (!StrUtil.isBlank(dataStr)) { } else if (!StrUtil.isBlank(dataStr)) {
FileUtils.writeStringToFile(dataPath, dataStr); FileUtil.writeBytes(dataStr.getBytes(), dataPath);
} }
//将jobId和dataPath参数添加至param中 //将jobId和dataPath参数添加至param中

查看文件

@ -1,118 +0,0 @@
package com.weilab.biology.util;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* 文件读取工具类
*/
public class FileUtils {
/**
* MultipartFile 转换成File
*
* @param multfile 原文件类型
* @return File
*/
public static File multipartToFile(MultipartFile multfile) {
File file = null;
try {
file = File.createTempFile("prefix", "_" + multfile.getOriginalFilename());
multfile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 读取文件内容作为字符串返回
*/
public static String readFileAsString(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
return readFileAsString(file);
}
/**
* 读取文件内容作为字符串返回
*/
public static String readFileAsString(File file) throws IOException {
StringBuilder sb = new StringBuilder((int) (file.length()));
// 创建字节输入流
FileInputStream fis = new FileInputStream(file);
// 创建一个长度为10240的Buffer
byte[] bbuf = new byte[10240];
// 用于保存实际读取的字节数
int hasRead = 0;
while ((hasRead = fis.read(bbuf)) > 0) {
sb.append(new String(bbuf, 0, hasRead));
}
fis.close();
return sb.toString();
}
/**
* 根据文件路径读取byte[] 数组
*/
public static byte[] readFileAsBytes(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
} else {
return readFileAsBytes(file);
}
}
/**
* 根据文件读取byte[]数组
*/
public static byte[] readFileAsBytes(File file) throws IOException {
if (file == null) {
throw new FileNotFoundException();
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
short bufSize = 1024;
byte[] buffer = new byte[bufSize];
int len1;
while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
bos.write(buffer, 0, len1);
}
return bos.toByteArray();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException var14) {
var14.printStackTrace();
}
bos.close();
}
}
}
public static void writeStringToFile(String filePath, String content) throws IOException {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
bufferedOutputStream.write(content.getBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
public static void writeByteToFile(String filePath, byte[] content) throws IOException {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(filePath));
bufferedOutputStream.write(content);
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}

查看文件

@ -7,9 +7,6 @@ import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component @Component
@Slf4j @Slf4j
public class MailUtil { public class MailUtil {