diff --git a/pnkx-admin/src/main/java/com/pnkx/web/controller/common/CommonController.java b/pnkx-admin/src/main/java/com/pnkx/web/controller/common/CommonController.java index 81b8d9a..2f47923 100644 --- a/pnkx-admin/src/main/java/com/pnkx/web/controller/common/CommonController.java +++ b/pnkx-admin/src/main/java/com/pnkx/web/controller/common/CommonController.java @@ -10,10 +10,12 @@ import com.pnkx.common.utils.file.MimeTypeUtils; import com.pnkx.system.domain.SysFile; import com.pnkx.system.service.ISysFileService; import net.coobird.thumbnailator.Thumbnails; +import org.apache.commons.compress.utils.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; 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.RestController; import org.springframework.web.multipart.MultipartFile; @@ -26,14 +28,12 @@ import com.pnkx.common.utils.file.FileUtils; import com.pnkx.framework.config.ServerConfig; import org.springframework.web.multipart.MultipartHttpServletRequest; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.RandomAccessFile; +import java.io.*; import java.text.NumberFormat; import java.time.LocalDate; import java.util.Arrays; import java.util.Comparator; +import java.util.List; import java.util.Objects; import static com.pnkx.web.controller.system.SysFileController.deleteFile; @@ -145,4 +145,23 @@ public class CommonController { 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(); + } + } } diff --git a/pnkx-common/src/main/java/com/pnkx/common/ftp/FtpTool.java b/pnkx-common/src/main/java/com/pnkx/common/ftp/FtpTool.java index 0b0ce4b..380bb8b 100644 --- a/pnkx-common/src/main/java/com/pnkx/common/ftp/FtpTool.java +++ b/pnkx-common/src/main/java/com/pnkx/common/ftp/FtpTool.java @@ -21,6 +21,8 @@ import java.util.Comparator; import java.util.List; import java.util.regex.Pattern; +import static cn.hutool.db.DbUtil.close; + /** * 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()) { targetFile.getParentFile().mkdirs(); } - fos = new FileOutputStream(new File(downloadPath + File.separator + fileName)); + fos = new FileOutputStream(downloadPath + File.separator + fileName); // 文件读取方式一 int i; byte[] bytes = new byte[1024]; diff --git a/pnkx-framework/src/main/java/com/pnkx/framework/config/SecurityConfig.java b/pnkx-framework/src/main/java/com/pnkx/framework/config/SecurityConfig.java index 038d362..ccdb720 100644 --- a/pnkx-framework/src/main/java/com/pnkx/framework/config/SecurityConfig.java +++ b/pnkx-framework/src/main/java/com/pnkx/framework/config/SecurityConfig.java @@ -100,6 +100,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { "/sendResetEmail/**", "/restPassword/**").anonymous() .antMatchers("/websocket/**").anonymous() .antMatchers("/open/**").anonymous() + // ftp资源请求 + .antMatchers("/ftp/**").anonymous() .antMatchers("/client/**").permitAll() .antMatchers( HttpMethod.GET, diff --git a/pnkx-system/src/main/java/com/pnkx/system/service/ISysFileService.java b/pnkx-system/src/main/java/com/pnkx/system/service/ISysFileService.java index efb27ea..892dbe2 100644 --- a/pnkx-system/src/main/java/com/pnkx/system/service/ISysFileService.java +++ b/pnkx-system/src/main/java/com/pnkx/system/service/ISysFileService.java @@ -4,6 +4,7 @@ import com.pnkx.system.domain.SysFile; import org.springframework.web.multipart.MultipartFile; import java.io.File; +import java.io.InputStream; import java.util.List; /** @@ -98,4 +99,11 @@ public interface ISysFileService { * @return 结果 */ Boolean like(Long id); + + /** + * 获取ftp文件 + * @param path 路径 + * @return 文件 + */ + InputStream getFtpFile(String path); } diff --git a/pnkx-system/src/main/java/com/pnkx/system/service/impl/SysFileServiceImpl.java b/pnkx-system/src/main/java/com/pnkx/system/service/impl/SysFileServiceImpl.java index aa51b7a..2bd7d84 100644 --- a/pnkx-system/src/main/java/com/pnkx/system/service/impl/SysFileServiceImpl.java +++ b/pnkx-system/src/main/java/com/pnkx/system/service/impl/SysFileServiceImpl.java @@ -18,6 +18,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; +import java.io.InputStream; import java.time.LocalDate; import java.util.Arrays; import java.util.List; @@ -174,6 +175,12 @@ public class SysFileServiceImpl implements ISysFileService { return sysFileMapper.like(id); } + @Override + public InputStream getFtpFile(String path) { + FTPClient ftpClient = ftpTool.connectFtp(); + return ftpTool.previewFile(ftpClient, path); + } + public void deleteFtpFilesByIds (SysFile file) { FTPClient ftpClient = ftpTool.connectFtp(); ftpTool.deleteServerFiles(ftpClient, file.getPath());