diff --git a/renren-admin/src/main/java/io/renren/modules/category/controller/CategoryController.java b/renren-admin/src/main/java/io/renren/modules/category/controller/CategoryController.java index 1d629b4a..2776e800 100644 --- a/renren-admin/src/main/java/io/renren/modules/category/controller/CategoryController.java +++ b/renren-admin/src/main/java/io/renren/modules/category/controller/CategoryController.java @@ -2,6 +2,7 @@ package io.renren.modules.category.controller; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import io.renren.common.exception.ErrorCode; import io.renren.common.utils.MessageUtils; import io.renren.common.utils.Result; @@ -20,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.io.File; +import java.io.IOException; import java.util.List; @RestController @@ -64,10 +66,18 @@ public class CategoryController { ValidatorUtils.validateEntity(categoryDTO, AddGroup.class, DefaultGroup.class); categoryDTO.setDelFlag(0); //若为一级分类,增加文件夹 - if (ObjectUtil.isNull(categoryDTO.getPid())) { - File file = new File(devModelFilePath + categoryDTO.getName()); - if (file.mkdirs()) { - throw new RuntimeException("创建文件夹失败!"); + if (ObjectUtils.isEmpty(categoryDTO.getPid())) { + File file = new File(devModelFilePath + File.pathSeparator + categoryDTO.getName()); + //文件路径不存在,创建各种能力的子文件夹 + if (! new File(devModelFilePath).exists()) { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + return new Result<>().error("开发指南文件夹创建失败!"); + } + } else { + return new Result<>().error("开发指南文件夹创建失败!"); } } categoryService.save(categoryDTO); diff --git a/renren-admin/src/main/java/io/renren/modules/developmentGuide/controller/DevelopmentGuideController.java b/renren-admin/src/main/java/io/renren/modules/developmentGuide/controller/DevelopmentGuideController.java index 690dd608..3ef4f4db 100644 --- a/renren-admin/src/main/java/io/renren/modules/developmentGuide/controller/DevelopmentGuideController.java +++ b/renren-admin/src/main/java/io/renren/modules/developmentGuide/controller/DevelopmentGuideController.java @@ -52,34 +52,17 @@ public class DevelopmentGuideController { } } - @PostMapping("/upload") + @PostMapping("/uploadDevelopmentFile") @ApiOperation("开发指南文件上传") @ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "开发指南文件", paramType = "file", dataType = "file", required = true) }) - public Result upload(@RequestParam("file") MultipartFile uploadFile, - HttpServletRequest request) { - logger.info("上传文件:" + uploadFile.getOriginalFilename()); - File folder = new File(devModelFilePath); - if (!folder.isDirectory()) { - folder.mkdirs(); - } - if (folder.exists()) { - if (folder.isFile()) { - if (!folder.delete()) { - return new Result<>().error("保存开发指南失败!!!"); - } - } - } - // 对上传的文件重命名,避免文件重名 - String fileName = "devModelFile.md"; + public Result uploadDevelopmentFile(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) { try { - // 文件保存 - uploadFile.transferTo(new File(folder, fileName)); - - return new Result<>(); - } catch (IOException e) { - return new Result().error(e.getMessage()); + developmentGuideService.uploadDevelopmentFile(uploadFile, request); + return new Result(); + }catch (Exception e) { + return new Result().error(e.getMessage()); } } diff --git a/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/DevelopmentGuideService.java b/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/DevelopmentGuideService.java index e2a0cc39..d0c2c473 100644 --- a/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/DevelopmentGuideService.java +++ b/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/DevelopmentGuideService.java @@ -1,5 +1,7 @@ package io.renren.modules.developmentGuide.service; +import org.springframework.web.multipart.MultipartFile; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -7,5 +9,5 @@ public interface DevelopmentGuideService { void getDevelopmentFile(HttpServletRequest request, HttpServletResponse response) throws Exception; - + void uploadDevelopmentFile(MultipartFile uploadFile, HttpServletRequest request) throws Exception; } diff --git a/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/impl/DevelopmentGuideServiceImpl.java b/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/impl/DevelopmentGuideServiceImpl.java index 3dd7b2f8..cd481189 100644 --- a/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/impl/DevelopmentGuideServiceImpl.java +++ b/renren-admin/src/main/java/io/renren/modules/developmentGuide/service/impl/DevelopmentGuideServiceImpl.java @@ -4,6 +4,7 @@ import io.renren.modules.developmentGuide.service.DevelopmentGuideService; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -16,46 +17,52 @@ import java.io.OutputStream; public class DevelopmentGuideServiceImpl implements DevelopmentGuideService { @Value("${resource.devModelFilePath}") - private String delModelFilePath; + private String devModelFilePath; @Override public void getDevelopmentFile(HttpServletRequest request, HttpServletResponse response) throws Exception { - //String type = request.getParameter("type").toString(); - //Long resourceId = Long.parseLong(request.getParameter("resourceId")); - //String url = type + "/devModelFile.txt"; - String url = delModelFilePath + File.separator + "devModelFile.md"; + String type = request.getParameter("type").toString(); + Long resourceId = Long.parseLong(request.getParameter("resourceId")); + String url = devModelFilePath + File.separator + type + File.separator + resourceId + ".md"; File file = new File(url); - if (!file.exists()) { - Exception e = new Exception("文件不存在"); - e.printStackTrace(); - throw e;//抛出文件不存在的 + //开发指南未保存过获取默认模板文件 + if (! file.exists()) { + file = new File(devModelFilePath + File.separator + type + File.separator + type + ".md"); } response.setContentType("text/html"); - FileInputStream fis = null; - OutputStream outputStream = response.getOutputStream(); - try { - fis = new FileInputStream(file); + try ( + FileInputStream fis = new FileInputStream(file); + OutputStream outputStream = response.getOutputStream(); + ) { //将读取流拷贝到输出流中 IOUtils.copy(fis, outputStream); //清空缓存的读取流,保证数据完整性 response.flushBuffer(); } catch (IOException e) { - System.err.println("解析失败!!!"); e.printStackTrace(); - throw new Exception("解析失败"); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - e.printStackTrace(); + throw e; + } + } + + @Override + public void uploadDevelopmentFile(MultipartFile uploadFile, HttpServletRequest request) throws Exception{ + String type = request.getParameter("type").toString(); + File folder = new File(devModelFilePath + File.pathSeparator + type + File.separator + uploadFile.getName()); + File path = new File(devModelFilePath + File.pathSeparator + type); + //文件保存过删除后重新保存 + if (folder.exists()) { + if (folder.isFile()) { + if (!folder.delete()) { + throw new IOException("保存文件失败!"); } } - if (outputStream != null) { - outputStream.close();//输出流关闭 - } } - System.err.println("获取开发指南成功!!!"); + try { + // 文件保存 + uploadFile.transferTo(new File(path, uploadFile.getName())); + } catch (IOException e) { + e.printStackTrace(); + } } } diff --git a/renren-admin/src/main/java/io/renren/modules/resource/service/impl/ResourceServiceImpl.java b/renren-admin/src/main/java/io/renren/modules/resource/service/impl/ResourceServiceImpl.java index cc499e3d..15522159 100644 --- a/renren-admin/src/main/java/io/renren/modules/resource/service/impl/ResourceServiceImpl.java +++ b/renren-admin/src/main/java/io/renren/modules/resource/service/impl/ResourceServiceImpl.java @@ -134,6 +134,7 @@ public class ResourceServiceImpl extends CrudServiceImpl resourceDTOS = resourceDao.selectWithAttrs(resourceDTO); - int j = pageNum * pageSize > resourceDTOS.size() ? resourceDTOS.size() : pageNum * pageSize; + int j = Math.min(pageNum * pageSize, resourceDTOS.size()); if (resourceDTOS.isEmpty()) { resultPage.setRecords(null); resultPage.setTotal(0); diff --git a/renren-admin/src/main/java/io/renren/modules/resourceCollection/service/impl/ResourceCollectionServiceImpl.java b/renren-admin/src/main/java/io/renren/modules/resourceCollection/service/impl/ResourceCollectionServiceImpl.java index d7617708..455dbb3e 100644 --- a/renren-admin/src/main/java/io/renren/modules/resourceCollection/service/impl/ResourceCollectionServiceImpl.java +++ b/renren-admin/src/main/java/io/renren/modules/resourceCollection/service/impl/ResourceCollectionServiceImpl.java @@ -41,8 +41,6 @@ public class ResourceCollectionServiceImpl extends CrudServiceImpl getWrapper(Map params){ QueryWrapper wrapper = new QueryWrapper<>(); - - return wrapper; }