开发指南文件获取,保存

This commit is contained in:
DingGang 2022-04-25 22:21:56 +08:00
parent 94cf76e322
commit 416b27074a
6 changed files with 57 additions and 56 deletions

View File

@ -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);

View File

@ -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<String>().error(e.getMessage());
developmentGuideService.uploadDevelopmentFile(uploadFile, request);
return new Result();
}catch (Exception e) {
return new Result().error(e.getMessage());
}
}

View File

@ -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;
}

View File

@ -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;//抛出文件不存在的
file = new File(devModelFilePath + File.separator + type + File.separator + type + ".md");
}
response.setContentType("text/html");
FileInputStream fis = null;
try (
FileInputStream fis = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
try {
fis = new FileInputStream(file);
) {
//将读取流拷贝到输出流中
IOUtils.copy(fis, outputStream);
//清空缓存的读取流保证数据完整性
response.flushBuffer();
} catch (IOException e) {
System.err.println("解析失败!!!");
e.printStackTrace();
throw new Exception("解析失败");
} finally {
if (fis != null) {
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("保存文件失败!");
}
}
}
try {
fis.close();
// 文件保存
uploadFile.transferTo(new File(path, uploadFile.getName()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
outputStream.close();//输出流关闭
}
}
System.err.println("获取开发指南成功!!!");
}
}

View File

@ -134,6 +134,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
@Override
public Object pageWithAttrs(JSONObject jsonObject) {
ResourceDTO resourceDTO = JSON.toJavaObject(jsonObject, ResourceDTO.class);
resourceDTO.setCreator(SecurityUser.getUser().getId());
Integer pageNum = jsonObject.getInteger("pageNum");
Integer pageSize = jsonObject.getInteger("pageSize");
//默认按上架时间降序排列
@ -163,7 +164,7 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
} else {
List<ResourceDTO> 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);

View File

@ -41,8 +41,6 @@ public class ResourceCollectionServiceImpl extends CrudServiceImpl<ResourceColle
@Override
public QueryWrapper<ResourceCollectionEntity> getWrapper(Map<String, Object> params){
QueryWrapper<ResourceCollectionEntity> wrapper = new QueryWrapper<>();
return wrapper;
}