|
@@ -5,6 +5,7 @@ import com.aliyun.oss.model.OSSObject;
|
|
|
import com.sckw.core.utils.IdWorker;
|
|
import com.sckw.core.utils.IdWorker;
|
|
|
import com.sckw.core.utils.StringUtils;
|
|
import com.sckw.core.utils.StringUtils;
|
|
|
import com.sckw.core.web.constant.HttpStatus;
|
|
import com.sckw.core.web.constant.HttpStatus;
|
|
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
import com.sckw.core.web.response.HttpResult;
|
|
import com.sckw.core.web.response.HttpResult;
|
|
|
import com.sckw.excel.common.NumberConstant;
|
|
import com.sckw.excel.common.NumberConstant;
|
|
|
import com.sckw.excel.common.StringConstant;
|
|
import com.sckw.excel.common.StringConstant;
|
|
@@ -17,9 +18,9 @@ import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FilenameUtils;
|
|
import org.apache.commons.io.FilenameUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
@@ -47,18 +48,6 @@ import java.util.zip.ZipOutputStream;
|
|
|
@Service
|
|
@Service
|
|
|
public class FileService {
|
|
public class FileService {
|
|
|
|
|
|
|
|
- @Value("${aliyun.oss.endpoint}")
|
|
|
|
|
- private String oss_endpoint;
|
|
|
|
|
-
|
|
|
|
|
- @Value("${aliyun.oss.accessKeyId}")
|
|
|
|
|
- private String oss_accessKeyId;
|
|
|
|
|
-
|
|
|
|
|
- @Value("${aliyun.oss.secret}")
|
|
|
|
|
- private String oss_accessKeySecret;
|
|
|
|
|
-
|
|
|
|
|
- @Value("${aliyun.oss.bucket}")
|
|
|
|
|
- public String oss_bucketName;
|
|
|
|
|
-
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
KwsFileInfoDao fileInfoDao;
|
|
KwsFileInfoDao fileInfoDao;
|
|
|
|
|
|
|
@@ -68,6 +57,7 @@ public class FileService {
|
|
|
* @param file
|
|
* @param file
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public HttpResult uploadFile(MultipartFile file) {
|
|
public HttpResult uploadFile(MultipartFile file) {
|
|
|
HttpResult result = new HttpResult();
|
|
HttpResult result = new HttpResult();
|
|
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
|
@@ -78,23 +68,13 @@ public class FileService {
|
|
|
result.setMsg("上传请不要选择空文件");
|
|
result.setMsg("上传请不要选择空文件");
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
- //获取文件类型
|
|
|
|
|
- String contentType = file.getContentType();
|
|
|
|
|
- //获取上传文件的原始文件名
|
|
|
|
|
String oFileName = file.getOriginalFilename();
|
|
String oFileName = file.getOriginalFilename();
|
|
|
- //文件大小
|
|
|
|
|
BigDecimal fileSize = FileUtils.getFileSize(file, StringConstant.KB);
|
|
BigDecimal fileSize = FileUtils.getFileSize(file, StringConstant.KB);
|
|
|
FileInfo fileInfo = new FileInfo();
|
|
FileInfo fileInfo = new FileInfo();
|
|
|
fileInfo.setOriginalName(oFileName);
|
|
fileInfo.setOriginalName(oFileName);
|
|
|
- //获取文件后缀
|
|
|
|
|
fileInfo.setFileSuffix(FilenameUtils.getExtension(oFileName));
|
|
fileInfo.setFileSuffix(FilenameUtils.getExtension(oFileName));
|
|
|
- fileInfo.setFileSize(StringUtils.isBlank(fileSize) ? new BigDecimal("0") : fileSize);
|
|
|
|
|
|
|
+ fileInfo.setFileSize(StringUtils.isBlank(fileSize) ? new BigDecimal(NumberConstant.ZERO) : fileSize);
|
|
|
Map<String, String> infoMap = FileUtils.uploadFileByInfo(file, FileEnum.DOCUMENT_ADDRESS);
|
|
Map<String, String> infoMap = FileUtils.uploadFileByInfo(file, FileEnum.DOCUMENT_ADDRESS);
|
|
|
-// Map<String, String> infoMap=new HashMap<>();
|
|
|
|
|
-// infoMap.put("fileName","fileName");
|
|
|
|
|
-// infoMap.put("fileMd5","fileMd5");
|
|
|
|
|
-// infoMap.put("filePath","filePath");
|
|
|
|
|
-// infoMap.put("fileKey","fileKey");
|
|
|
|
|
fileInfo.setId(new IdWorker(1).nextId());
|
|
fileInfo.setId(new IdWorker(1).nextId());
|
|
|
fileInfo.setType(FileEnum.FILE_STORE_TYPE_OSS.getFileType());
|
|
fileInfo.setType(FileEnum.FILE_STORE_TYPE_OSS.getFileType());
|
|
|
fileInfo.setFileName(infoMap.get("fileName"));
|
|
fileInfo.setFileName(infoMap.get("fileName"));
|
|
@@ -104,12 +84,10 @@ public class FileService {
|
|
|
fileInfo.setUpdateTime(new Date());
|
|
fileInfo.setUpdateTime(new Date());
|
|
|
fileInfo.setStatus(0);
|
|
fileInfo.setStatus(0);
|
|
|
fileInfo.setDelFlag(0);
|
|
fileInfo.setDelFlag(0);
|
|
|
- fileInfo.setCreateBy(1L);
|
|
|
|
|
- fileInfo.setUpdateBy(1L);
|
|
|
|
|
|
|
+ fileInfo.setCreateBy(LoginUserHolder.getUserId());
|
|
|
|
|
+ fileInfo.setUpdateBy(LoginUserHolder.getUserId());
|
|
|
fileInfoDao.insert(fileInfo);
|
|
fileInfoDao.insert(fileInfo);
|
|
|
- //上传至oss文件地址
|
|
|
|
|
if (StringUtils.isNotBlank(infoMap.get("filePath"))) {
|
|
if (StringUtils.isNotBlank(infoMap.get("filePath"))) {
|
|
|
- result.setCode(HttpStatus.SUCCESS_CODE);
|
|
|
|
|
result.setMsg("上传成功");
|
|
result.setMsg("上传成功");
|
|
|
Map<String, Object> map = new HashMap<>(NumberConstant.SIXTEEN);
|
|
Map<String, Object> map = new HashMap<>(NumberConstant.SIXTEEN);
|
|
|
map.put("fileKey", infoMap.get("fileMd5"));
|
|
map.put("fileKey", infoMap.get("fileMd5"));
|
|
@@ -137,7 +115,7 @@ public class FileService {
|
|
|
if (!ObjectUtils.isEmpty(file) && file.length > 0) {
|
|
if (!ObjectUtils.isEmpty(file) && file.length > 0) {
|
|
|
List<MultipartFile> multipartFiles = Arrays.asList(file);
|
|
List<MultipartFile> multipartFiles = Arrays.asList(file);
|
|
|
for (MultipartFile multipartFile : multipartFiles) {
|
|
for (MultipartFile multipartFile : multipartFiles) {
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
|
|
|
|
+ Map<String, Object> map = new HashMap<>(NumberConstant.SIXTEEN);
|
|
|
//文件大小
|
|
//文件大小
|
|
|
String fileSize = FileUtils.getFileSize(multipartFile);
|
|
String fileSize = FileUtils.getFileSize(multipartFile);
|
|
|
//文件名称
|
|
//文件名称
|
|
@@ -184,13 +162,7 @@ public class FileService {
|
|
|
|
|
|
|
|
|
|
|
|
|
public void remove(String fileName) {
|
|
public void remove(String fileName) {
|
|
|
- String str = StringConstant.HTTP_STRING
|
|
|
|
|
- + StringConstant.COLON
|
|
|
|
|
- + StringConstant.LEFT_SEPARATORS
|
|
|
|
|
- + oss_bucketName
|
|
|
|
|
- + StringConstant.POINT
|
|
|
|
|
- + oss_endpoint
|
|
|
|
|
- + StringConstant.LEFT_SEPARATOR;
|
|
|
|
|
|
|
+ String str = FileUtils.getOSSAddressPrefix();
|
|
|
String fileNameSubstring = StringUtils.substring(fileName, str.length() + 1);
|
|
String fileNameSubstring = StringUtils.substring(fileName, str.length() + 1);
|
|
|
FileUtils.remove(fileName);
|
|
FileUtils.remove(fileName);
|
|
|
}
|
|
}
|
|
@@ -232,10 +204,11 @@ public class FileService {
|
|
|
zos.close();
|
|
zos.close();
|
|
|
String header = request.getHeader("User-Agent").toUpperCase();
|
|
String header = request.getHeader("User-Agent").toUpperCase();
|
|
|
if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
|
|
if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
|
|
|
- momentFileName = URLEncoder.encode(momentFileName, "utf-8");
|
|
|
|
|
- momentFileName = momentFileName.replace("+", "%20"); //IE下载文件名空格变+号问题
|
|
|
|
|
|
|
+ momentFileName = URLEncoder.encode(momentFileName, StringConstant.ENCODING_UTF_EIGHT);
|
|
|
|
|
+ //IE下载文件名空格变+号问题
|
|
|
|
|
+ momentFileName = momentFileName.replace("+", "%20");
|
|
|
} else {
|
|
} else {
|
|
|
- momentFileName = new String(momentFileName.getBytes(), "ISO8859-1");
|
|
|
|
|
|
|
+ momentFileName = new String(momentFileName.getBytes(), StringConstant.ENCODING_ISO);
|
|
|
}
|
|
}
|
|
|
response.reset();
|
|
response.reset();
|
|
|
response.setContentType("text/plain");
|
|
response.setContentType("text/plain");
|