feat: 图片资源转为内部接口

This commit is contained in:
裴浩宇 2024-03-20 18:32:36 +08:00
parent 0a9f1dd835
commit bb1e33b8ef
5 changed files with 53 additions and 5 deletions

View File

@ -10,10 +10,12 @@ import com.pnkx.common.utils.file.MimeTypeUtils;
import com.pnkx.system.domain.SysFile; import com.pnkx.system.domain.SysFile;
import com.pnkx.system.service.ISysFileService; import com.pnkx.system.service.ISysFileService;
import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -26,14 +28,12 @@ import com.pnkx.common.utils.file.FileUtils;
import com.pnkx.framework.config.ServerConfig; import com.pnkx.framework.config.ServerConfig;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.File; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import static com.pnkx.web.controller.system.SysFileController.deleteFile; import static com.pnkx.web.controller.system.SysFileController.deleteFile;
@ -145,4 +145,23 @@ public class CommonController {
log.error("下载文件失败", e); log.error("下载文件失败", e);
} }
} }
/**
* ftp获取单个文件
*/
@GetMapping("/ftp/**")
public void getFtpFile(HttpServletRequest request, HttpServletResponse response) {
try {
// 下载文件并获取文件流
InputStream fileStream = sysFileService.getFtpFile(request.getRequestURI().substring(4));
// 设置响应头
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// 将文件流输出到响应
IOUtils.copy(fileStream, response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
// 异常处理
e.printStackTrace();
}
}
} }

View File

@ -21,6 +21,8 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static cn.hutool.db.DbUtil.close;
/** /**
* FtpConfig * FtpConfig
* *
@ -100,6 +102,16 @@ public class FtpTool {
} }
} }
public InputStream previewFile(FTPClient ftpClient, String path) {
InputStream inputStream = null;
try {
inputStream = ftpClient.retrieveFileStream(path);
} catch (Exception e) {
log.error("获取文件失败;异常信息:{}", e.toString());
}
return inputStream;
}
/** /**
* 按前后缀查询文件 * 按前后缀查询文件
* *
@ -157,7 +169,7 @@ public class FtpTool {
if (!targetFile.getParentFile().exists()) { if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs(); targetFile.getParentFile().mkdirs();
} }
fos = new FileOutputStream(new File(downloadPath + File.separator + fileName)); fos = new FileOutputStream(downloadPath + File.separator + fileName);
// 文件读取方式一 // 文件读取方式一
int i; int i;
byte[] bytes = new byte[1024]; byte[] bytes = new byte[1024];

View File

@ -100,6 +100,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
"/sendResetEmail/**", "/restPassword/**").anonymous() "/sendResetEmail/**", "/restPassword/**").anonymous()
.antMatchers("/websocket/**").anonymous() .antMatchers("/websocket/**").anonymous()
.antMatchers("/open/**").anonymous() .antMatchers("/open/**").anonymous()
// ftp资源请求
.antMatchers("/ftp/**").anonymous()
.antMatchers("/client/**").permitAll() .antMatchers("/client/**").permitAll()
.antMatchers( .antMatchers(
HttpMethod.GET, HttpMethod.GET,

View File

@ -4,6 +4,7 @@ import com.pnkx.system.domain.SysFile;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.util.List; import java.util.List;
/** /**
@ -98,4 +99,11 @@ public interface ISysFileService {
* @return 结果 * @return 结果
*/ */
Boolean like(Long id); Boolean like(Long id);
/**
* 获取ftp文件
* @param path 路径
* @return 文件
*/
InputStream getFtpFile(String path);
} }

View File

@ -18,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -174,6 +175,12 @@ public class SysFileServiceImpl implements ISysFileService {
return sysFileMapper.like(id); return sysFileMapper.like(id);
} }
@Override
public InputStream getFtpFile(String path) {
FTPClient ftpClient = ftpTool.connectFtp();
return ftpTool.previewFile(ftpClient, path);
}
public void deleteFtpFilesByIds (SysFile file) { public void deleteFtpFilesByIds (SysFile file) {
FTPClient ftpClient = ftpTool.connectFtp(); FTPClient ftpClient = ftpTool.connectFtp();
ftpTool.deleteServerFiles(ftpClient, file.getPath()); ftpTool.deleteServerFiles(ftpClient, file.getPath());