| | |
| | | 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; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import com.ruoyi.common.config.RuoYiConfig; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.domain.Result; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.file.FileUploadUtils; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | |
| | | */ |
| | | @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; |
| | | |
| | | /** |
| | | * 通用下载请求 |
| | |
| | | * 通用上传请求(单个) |
| | | */ |
| | | @PostMapping("/upload") |
| | | public Result uploadFile(MultipartFile file) throws Exception |
| | | public AjaxResult uploadFile(MultipartFile file) throws Exception |
| | | { |
| | | try |
| | | { |
| | |
| | | // 上传并返回新文件名称 |
| | | String fileName = FileUploadUtils.upload(filePath, file); |
| | | String url = serverConfig.getUrl() + fileName; |
| | | Result ajax = Result.success(); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("url", url); |
| | | ajax.put("fileName", fileName); |
| | | ajax.put("newFileName", FileUtils.getName(fileName)); |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return Result.error(e.getMessage()); |
| | | return AjaxResult.error(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | * 通用上传请求(多个) |
| | | */ |
| | | @PostMapping("/uploads") |
| | | public Result uploadFiles(List<MultipartFile> files) throws Exception |
| | | public AjaxResult uploadFiles(List<MultipartFile> files) throws Exception |
| | | { |
| | | try |
| | | { |
| | |
| | | newFileNames.add(FileUtils.getName(fileName)); |
| | | originalFilenames.add(file.getOriginalFilename()); |
| | | } |
| | | Result ajax = Result.success(); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER)); |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return Result.error(e.getMessage()); |
| | | return AjaxResult.error(e.getMessage()); |
| | | } |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | } |