From 301c64543a44ddbc29524a705f014671e39bfab2 Mon Sep 17 00:00:00 2001 From: "wh.g" Date: Mon, 16 Dec 2024 16:57:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=84=E4=BB=B6=E4=B8=8A=E4=BC=A0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysOssController.java | 110 ++++++++++++++++-- .../src/main/resources/application.yml | 3 + 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java index b3ddff3..f41d57f 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java @@ -3,6 +3,7 @@ package com.ruoyi.web.controller.system; import cn.dev33.satoken.annotation.SaCheckPermission; import cn.hutool.core.util.ObjectUtil; +import com.amazonaws.util.IOUtils; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.PageQuery; @@ -10,6 +11,8 @@ import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.core.validate.QueryGroup; import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.file.FileUtils; import com.ruoyi.official.service.ISysImageService; import com.ruoyi.system.domain.bo.SysOssBo; import com.ruoyi.system.domain.vo.SysOssVo; @@ -17,16 +20,22 @@ import com.ruoyi.system.service.ISysOssService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.util.UriUtils; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.NotEmpty; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.*; @@ -66,7 +75,7 @@ public class SysOssController extends BaseController { @SaCheckPermission("system:oss:list") @GetMapping("/listByIds/{ossIds}") public R> listByIds(@NotEmpty(message = "主键不能为空") - @PathVariable Long[] ossIds) { + @PathVariable Long[] ossIds) { List list = iSysOssService.listByIds(Arrays.asList(ossIds)); return R.ok(list); } @@ -93,6 +102,7 @@ public class SysOssController extends BaseController { /** * 上传文件到本地 + * * @param file 文件数据 * @return 文件上传地址信息 */ @@ -123,7 +133,7 @@ public class SysOssController extends BaseController { String pathFan = filePath.replace("\\", "/"); //filePath获取到的地址斜杠是“ \ ”的(单斜杠是特殊符号,得用双斜杠代替),得换成“ / ”才能访问到 //返回全路径图片地址 - String returnPath = ("http://47.121.27.78:8008/upload/" + fileName).replace("\\", "/"); + String returnPath = ("http://39.101.76.77:8008/upload/" + fileName).replace("\\", "/"); //修改数据库 Long image = iSysImageService.uploadImage(fileName, returnPath); System.err.println("替换后:" + pathFan); @@ -136,18 +146,102 @@ public class SysOssController extends BaseController { } } + /** + * 上传文件到本地 + * + * @param file 文件数据 + * @return 文件上传地址信息 + */ + @Log(title = "本地文件上传Files", businessType = BusinessType.INSERT) + @PostMapping("/addFilesUpload") + public R> addFilesUpload(@RequestPart("file") MultipartFile file) { + Map map = new HashMap<>(); + String filePath = uploadPath + "/file/files/"; + try { + if (file.isEmpty()) { + return R.warn("文件为空"); + } + String fileName = System.currentTimeMillis() + "-" + file.getOriginalFilename(); + //文件上传的路径(当前项目的根目录) - /** - * 下载OSS对象 - * - * @param ossId OSS对象ID - */ + System.err.println(filePath); + // 创建目标目录(如果不存在) + File directory = new File(filePath); + if (!directory.exists()) { + directory.mkdirs(); + } + // 保存文件到目标目录 + File uploadFile = new File(directory.getAbsolutePath() + File.separator + fileName); + file.transferTo(uploadFile); + //上传服务器地址 + String pathFan = filePath.replace("\\", "/"); + //filePath获取到的地址斜杠是“ \ ”的(单斜杠是特殊符号,得用双斜杠代替),得换成“ / ”才能访问到 + //返回全路径图片地址 + String returnPath = ("http://39.101.76.77:8008/upload/" + fileName).replace("\\", "/"); + //修改数据库 + Long image = iSysImageService.uploadImage(fileName, returnPath); + System.err.println("替换后:" + pathFan); + map.put("url", returnPath); + map.put("fileName", fileName); + map.put("ossId", image.toString()); + return R.ok(map); + } catch (IOException e) { + return R.warn("文件上传失败: " + e); + } + } + + /** + * 通用下载请求 + */ + @GetMapping("/download") + public void bianPdfDownload(HttpServletRequest request, HttpServletResponse response) throws Exception { + // 请求类型、请求参数、请求头 根据需求制定即可 + // 获取到需要下载的文件 + // 如何生成文件,根据实际业务需求改造即可 + //http://39.101.76.77:8008/upload/ + String fileName = request.getParameter("imageUrl").substring(32); + String filePath = uploadPath + "/file/files/"; + String outputFilePath = filePath + fileName; + + log.info("待下载文件:{}", outputFilePath); + // 下载文件 + // 设置响应的内容类型,让浏览器知道下载的是一个文件 + ServletContext context = request.getServletContext(); + // get MIME type of the file + String mimeType = context.getMimeType(outputFilePath); + if (mimeType == null) { + // set to binary type if MIME mapping not found + mimeType = "application/octet-stream"; + log.info("context getMimeType is null"); + } + log.info("MIME type: " + mimeType); + // 设置响应头信息,告诉浏览器文件的名称和长度 + // set content attributes for the response + response.setContentType(mimeType); + // 设置编码 + response.setCharacterEncoding("utf-8"); + // 设置请求头参数以及下载的文件名称,中文名称转义防止乱码 + // Copy the stream to the response's output stream. + try { + InputStream myStream = new FileInputStream(outputFilePath); + IOUtils.copy(myStream, response.getOutputStream()); + response.flushBuffer(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 下载OSS对象 + * + * @param ossId OSS对象ID + */ @SaCheckPermission("system:oss:download") @GetMapping("/download/{ossId}") public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException { - iSysOssService.download(ossId,response); + iSysOssService.download(ossId, response); } /** diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index b8d67ab..4900da8 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -134,6 +134,9 @@ security: - /official/slideshow/** - /official/jobInfo/** - /official/index/** + - /official/message/** + - /official/companymessage/** + - /system/oss/addFilesUpload # 公共路径 - /favicon.ico - /error