增加本地服务器文件上传和下载接口

This commit is contained in:
weiweiw 2024-06-17 18:04:38 +08:00
parent fa932cf25e
commit ff14f6f573
6 changed files with 107 additions and 1 deletions

View File

@ -66,6 +66,11 @@
<artifactId>bonus-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
<build>

View File

@ -1,9 +1,13 @@
package com.bonus.file.controller;
import com.bonus.common.core.utils.Base64Utils;
import com.bonus.file.utils.FileDownloadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.bonus.common.core.domain.R;
@ -11,6 +15,8 @@ import com.bonus.common.core.utils.file.FileUtils;
import com.bonus.file.service.ISysFileService;
import com.bonus.system.api.domain.SysFile;
import java.io.IOException;
/**
* 文件请求处理
*
@ -45,4 +51,35 @@ public class SysFileController
return R.fail(e.getMessage());
}
}
@GetMapping("/download")
public R<Boolean> downloadFile(String url, String destination) {
try {
String fileUrl = Base64Utils.decodeUrl(url);
if (fileUrl != null) {
String fileName = Base64Utils.getFileNameFromURL(fileUrl);
sysFileService.downloadFile(fileUrl, destination + "/" + fileName);
return R.ok();
}
} catch (Exception e) {
log.error("下载文件失败", e);
return R.fail(e.getMessage());
}
return R.fail("下载文件失败");
}
@GetMapping("/delete")
public R<Boolean> deleteFile(String url) {
try {
String fileUrl = Base64Utils.decodeUrl(url);
if (fileUrl != null) {
sysFileService.deleteFile(fileUrl);
return R.ok();
}
} catch (Exception e) {
log.error("delete文件失败", e);
return R.fail(e.getMessage());
}
return R.fail("传入参数不满足要求");
}
}

View File

@ -43,4 +43,17 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
IoUtils.closeQuietly(inputStream);
return domain + "/" + storePath.getFullPath();
}
@Override
public boolean downloadFile(String urlStr, String destination) throws Exception
{
return false;
}
@Override
public boolean deleteFile(String urlStr) throws Exception
{
return false;
}
}

View File

@ -17,4 +17,11 @@ public interface ISysFileService
* @throws Exception
*/
public String uploadFile(MultipartFile file) throws Exception;
public boolean downloadFile(String urlStr, String destination) throws Exception;
public boolean deleteFile(String urlStr) throws Exception;
}

View File

@ -1,11 +1,17 @@
package com.bonus.file.service;
import com.bonus.file.utils.FileDownloadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.bonus.file.utils.FileUploadUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* 本地文件存储
*
@ -47,4 +53,29 @@ public class LocalSysFileServiceImpl implements ISysFileService
String url = domain + localFilePrefix + name;
return url;
}
@Override
public boolean downloadFile(String urlStr, String destination) throws Exception
{
return FileDownloadUtils.downloadFile(urlStr, destination);
}
@Override
public boolean 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("删除文件时文件不存在");
}
return true;
}
}

View File

@ -46,4 +46,17 @@ public class MinioSysFileServiceImpl implements ISysFileService
IoUtils.closeQuietly(inputStream);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
}
@Override
public boolean downloadFile(String urlStr, String destination) throws Exception
{
return false;
}
@Override
public boolean deleteFile(String urlStr) throws Exception
{
return false;
}
}