From cef67d95db23105fe57a98854e9957ef47eb7d80 Mon Sep 17 00:00:00 2001 From: liuxiao <1732399289qq.com> Date: Tue, 30 Jul 2024 16:04:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysOssController.java | 59 ++++++++----------- .../controller/GwIndexController.java | 2 +- .../service/impl/GwSlideshowServiceImpl.java | 1 + 3 files changed, 25 insertions(+), 37 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 41ff6fa..efc4adf 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 @@ -22,14 +22,13 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.NotEmpty; import java.io.File; import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.text.SimpleDateFormat; +import java.util.*; /** * 文件上传 控制层 @@ -45,6 +44,7 @@ public class SysOssController extends BaseController { @Value("${file.BASE_FILE_SAVE_PATH}") private String uploadPath; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); private final ISysOssService iSysOssService; private final ISysImageService iSysImageService; @@ -98,43 +98,30 @@ public class SysOssController extends BaseController { */ @Log(title = "本地文件上传", businessType = BusinessType.INSERT) @PostMapping("/addPartsUpload") - public R> addPartsUpload(@RequestPart("file") MultipartFile file) { + public R> addPartsUpload(@RequestPart("file") MultipartFile file, HttpServletRequest req) { Map map=new HashMap<>(); - - String filePath=uploadPath+"/file/upload/"; + String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/"); + String format = sdf.format(new Date()); + ///www/wwwroot/guanwang/web/image/file/upload/ + // String filePath=uploadPath+"/file/upload/"; + File folder = new File(realPath + format); + if (!folder.isDirectory()){ + folder.mkdirs(); + } + String oldName = file.getOriginalFilename(); + String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length()); try { - if (file.isEmpty()) { - return R.warn("文件为空"); - } - - String fileName = System.currentTimeMillis()+"-"+file.getOriginalFilename(); - //文件上传的路径(当前项目的根目录) - - 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://47.121.27.78:8008/upload/" + fileName).replace("\\","/"); - //修改数据库 - Long image = iSysImageService.uploadImage(fileName,returnPath); - System.err.println("替换后:"+pathFan); - map.put("url",returnPath); - map.put("fileName",fileName); + file.transferTo(new File(realPath,newName)); + String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile" + format + newName; + Long image = iSysImageService.uploadImage(newName,filePath); + map.put("url",filePath); + map.put("fileName",newName); map.put("ossId",image.toString()); return R.ok(map); - } catch (IOException e) { - return R.warn("文件上传失败: "+e); + } catch (IOException ex) { + ex.printStackTrace(); } - + return R.warn("文件上传失败!"); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/official/controller/GwIndexController.java b/ruoyi-system/src/main/java/com/ruoyi/official/controller/GwIndexController.java index d9f375d..bea9a83 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/official/controller/GwIndexController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/official/controller/GwIndexController.java @@ -41,7 +41,7 @@ public class GwIndexController extends BaseController { /** * 查询首页封面信息列表 */ - @SaCheckPermission("official:index:list") + /* @SaCheckPermission("official:index:list")*/ @GetMapping("/list") public TableDataInfo list(GwIndexBo bo, PageQuery pageQuery) { return iGwIndexService.queryPageList(bo, pageQuery); diff --git a/ruoyi-system/src/main/java/com/ruoyi/official/service/impl/GwSlideshowServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/official/service/impl/GwSlideshowServiceImpl.java index a5c8434..2baac51 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/official/service/impl/GwSlideshowServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/official/service/impl/GwSlideshowServiceImpl.java @@ -44,6 +44,7 @@ public class GwSlideshowServiceImpl implements IGwSlideshowService { GwSlideshowVo gwSlideshowVo = baseMapper.selectVoById(id); //查询详情时返回上一篇,下一篇处理 List list = baseMapper.selectList(Wrappers.lambdaQuery().eq(GwSlideshow::getType,gwSlideshowVo.getType()).orderByDesc(GwSlideshow::getIsTop).orderByDesc(GwSlideshow::getCreateTime)); + //获取文章推荐内容 getPrevNextArticles(id, gwSlideshowVo, list); return gwSlideshowVo; }