开发指南文件上传修改

This commit is contained in:
dinggang 2022-04-26 15:09:51 +08:00
parent 299d0cc19b
commit 98c5978c7a
2 changed files with 23 additions and 7 deletions

View File

@ -42,7 +42,9 @@ public class DevelopmentGuideController {
@PostMapping("/uploadDevelopmentFile") @PostMapping("/uploadDevelopmentFile")
@ApiOperation("开发指南文件上传") @ApiOperation("开发指南文件上传")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "file", value = "开发指南文件", paramType = "file", dataType = "file", required = true) @ApiImplicitParam(name = "file", value = "开发指南文件", paramType = "file", dataType = "file", required = true),
@ApiImplicitParam(name = "fileName", value = "文件名称", paramType = "query", dataType = "String", required = true),
@ApiImplicitParam(name = "type", value = "能力类型", paramType = "query", dataType = "String", required = true),
}) })
public Result uploadDevelopmentFile(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) { public Result uploadDevelopmentFile(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request) {
try { try {

View File

@ -27,19 +27,28 @@ public class DevelopmentGuideServiceImpl implements DevelopmentGuideService {
@Override @Override
public void getDevelopmentFile(HttpServletRequest request, HttpServletResponse response) throws Exception { public void getDevelopmentFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.info("----------------------------------开始上传开发指南--------------------------------------------------");
String type = request.getParameter("type").toString(); String type = request.getParameter("type").toString();
Long resourceId = Long.parseLong(request.getParameter("resourceId")); Long resourceId = Long.parseLong(request.getParameter("resourceId"));
logger.info("读取文件类型及能力ID" + type + ";" + resourceId);
String url = devModelFilePath + File.separator + type + File.separator + resourceId + ".md"; String url = devModelFilePath + File.separator + type + File.separator + resourceId + ".md";
String filePath = devModelFilePath + File.separator + type;
File file = new File(url); File file = new File(url);
//开发指南未保存过获取默认模板文件 //开发指南未保存过获取默认模板文件
if (! file.exists()) { if (new File(filePath).exists()) {
file = new File(devModelFilePath + File.separator + type + File.separator + type + ".md"); if (! file.exists()) {
file = new File(devModelFilePath + File.separator + type + File.separator + type + ".md");
}
} else {
logger.info("----------------------------------文件路径不存在--------------------------------------");
throw new IOException("文件路径不存在!");
} }
response.setContentType("text/html"); response.setContentType("text/html");
try ( try (
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream(); OutputStream outputStream = response.getOutputStream();
) { ) {
logger.info("----------------------------------开始获取文件--------------------------------------");
//将读取流拷贝到输出流中 //将读取流拷贝到输出流中
IOUtils.copy(fis, outputStream); IOUtils.copy(fis, outputStream);
//清空缓存的读取流保证数据完整性 //清空缓存的读取流保证数据完整性
@ -52,22 +61,27 @@ public class DevelopmentGuideServiceImpl implements DevelopmentGuideService {
@Override @Override
public void uploadDevelopmentFile(MultipartFile uploadFile, HttpServletRequest request) throws Exception{ public void uploadDevelopmentFile(MultipartFile uploadFile, HttpServletRequest request) throws Exception{
logger.info("----------------------------------开始上传开发指南--------------------------------------------------");
String type = request.getParameter("type").toString(); String type = request.getParameter("type").toString();
File folder = new File(devModelFilePath + File.pathSeparator + type + File.separator + uploadFile.getName()); String fileName = request.getParameter("fileName").toString();
File path = new File(devModelFilePath + File.pathSeparator + type); logger.info("读取文件类型及文件名:" + type + ";" + fileName);
File folder = new File(devModelFilePath + File.pathSeparator + type + File.separator + fileName);
File path = new File(devModelFilePath + File.pathSeparator + type + File.separator);
//文件保存过删除后重新保存 //文件保存过删除后重新保存
if (folder.exists()) { if (folder.exists()) {
if (folder.isFile()) { if (folder.isFile()) {
if (!folder.delete()) { if (!folder.delete()) {
logger.info("----------------------------------删除已存在文件失败--------------------------------------");
throw new IOException("保存文件失败!"); throw new IOException("保存文件失败!");
} }
} }
} }
try { try {
// 文件保存 // 文件保存
logger.info("读取文件名:" + uploadFile.getName()); uploadFile.transferTo(new File(path, fileName));
uploadFile.transferTo(new File(path, uploadFile.getName())); logger.info("----------------------------------文件上传成功--------------------------------------");
} catch (IOException e) { } catch (IOException e) {
logger.info("----------------------------------文件上传失败--------------------------------------");
e.printStackTrace(); e.printStackTrace();
} }
} }