| | |
| | | package com.ruoyi.web.controller.common; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import com.ruoyi.common.utils.file.MinioUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/common") |
| | | public class CommonController |
| | | { |
| | | public class CommonController { |
| | | private static final Logger log = LoggerFactory.getLogger(CommonController.class); |
| | | |
| | | @Autowired |
| | | private ServerConfig serverConfig; |
| | | |
| | | private static final String FILE_DELIMETER = ","; |
| | | |
| | | @Resource |
| | | private MinioUtils minioUtils; |
| | | |
| | | /** |
| | | * 通用下载请求 |
| | |
| | | log.error("下载文件失败", e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 通用文件下载方法 |
| | | * |
| | | * @param fileUrl Minio中的文件路径(如:images/2023/report.pdf) |
| | | * @param fileName 下载时显示的文件名(如:年度报告.pdf) |
| | | * @param response HttpServletResponse对象 |
| | | */ |
| | | @GetMapping("/downloadMinio") |
| | | public void downloadFile(String fileUrl, String fileName, HttpServletResponse response) { |
| | | Logger logger = LoggerFactory.getLogger(this.getClass()); |
| | | if (StringUtils.isBlank(fileUrl) || StringUtils.isBlank(fileName)) { |
| | | response.setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| | | try (PrintWriter writer = response.getWriter()) { |
| | | writer.write("文件路径和文件名不能为空"); |
| | | } catch (IOException e) { |
| | | logger.error("设置响应信息出错", e); |
| | | } |
| | | return; |
| | | } |
| | | String bucketName = "radio-frequency"; |
| | | try { |
| | | minioUtils.download(bucketName, fileUrl, fileName, response); |
| | | } catch (Exception e) { |
| | | logger.error("文件下载失败", e); |
| | | response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
| | | try (PrintWriter writer = response.getWriter()) { |
| | | writer.write("文件下载失败: " + e.getMessage()); |
| | | } catch (IOException ioException) { |
| | | logger.error("设置响应信息出错", ioException); |
| | | } |
| | | } |
| | | } |
| | | } |