提交漏交的文件
This commit is contained in:
parent
f430688bc4
commit
dc7a51eba6
|
|
@ -0,0 +1,102 @@
|
|||
package com.bonus.file.service.impl;
|
||||
|
||||
import com.bonus.common.core.utils.StringUtils;
|
||||
import com.bonus.common.core.utils.file.FileUtils;
|
||||
import com.bonus.file.service.ISysFileService;
|
||||
import com.bonus.file.utils.FileDownloadUtils;
|
||||
import com.bonus.system.api.domain.SysFile;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.bonus.file.utils.FileUploadUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 本地文件存储
|
||||
*
|
||||
* @author bonus
|
||||
*/
|
||||
@Service
|
||||
@ConditionalOnProperty(name = "storage.type", havingValue = "file", matchIfMissing = true)
|
||||
public class LocalSysFileServiceImpl implements ISysFileService
|
||||
{
|
||||
/**
|
||||
* 资源映射路径 前缀
|
||||
*/
|
||||
@Value("${file.prefix}")
|
||||
public String localFilePrefix;
|
||||
|
||||
/**
|
||||
* 域名或本机访问地址
|
||||
*/
|
||||
@Value("${file.domain}")
|
||||
public String domain;
|
||||
|
||||
/**
|
||||
* 上传文件存储在本地的根路径
|
||||
*/
|
||||
@Value("${file.path}")
|
||||
private String localFilePath;
|
||||
|
||||
/**
|
||||
* 本地文件上传接口
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @return 访问地址
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public SysFile uploadFile(MultipartFile file) throws Exception
|
||||
{
|
||||
String name = FileUploadUtils.upload(localFilePath, file);
|
||||
String url = domain + localFilePrefix + name;
|
||||
return SysFile.builder().url(url).name(name).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysFile> uploadFiles(MultipartFile[] files) throws Exception {
|
||||
List<SysFile> sysFiles = new ArrayList<>();
|
||||
for (MultipartFile file : files) {
|
||||
SysFile sysFile = uploadFile(file);
|
||||
sysFiles.add(sysFile);
|
||||
}
|
||||
return sysFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadFile(HttpServletResponse response, String urlStr) throws Exception
|
||||
{
|
||||
FileDownloadUtils.downloadFile(response, urlStr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFile(String urlStr) throws Exception
|
||||
{
|
||||
String regex = String.format("^(.*?%s)", localFilePrefix);
|
||||
String updatePath = urlStr.replaceFirst(regex, localFilePath);
|
||||
Path path = Paths.get(updatePath);
|
||||
if (Files.exists(path)){
|
||||
try {
|
||||
Files.deleteIfExists(path);
|
||||
}catch (IOException e){
|
||||
throw new Exception(e.getMessage(), e);
|
||||
}
|
||||
}else {
|
||||
throw new Exception("删除文件时文件不存在");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue