设备图片上传更新与编辑功能提示
This commit is contained in:
@@ -5,13 +5,17 @@ import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.annotation.Log;
|
||||
import org.nl.domain.LocalStorage;
|
||||
import org.nl.exception.BadRequestException;
|
||||
import org.nl.logicflow.service.StageImageService;
|
||||
import org.nl.logicflow.service.dto.StageImageDto;
|
||||
import org.nl.utils.FileUtil;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -70,4 +74,16 @@ public class StageImageController {
|
||||
public ResponseEntity<Object> selectList() {
|
||||
return new ResponseEntity<>(stageImageService.selectList(), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping({"/pictures"})
|
||||
@ApiOperation("上传图片")
|
||||
public ResponseEntity<Object> upload(@RequestParam MultipartFile file) {
|
||||
String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
|
||||
if (!"图片".equals(FileUtil.getFileType(suffix))) {
|
||||
throw new BadRequestException("只能上传图片");
|
||||
} else {
|
||||
LocalStorage localStorage = stageImageService.upload((String)null, file);
|
||||
return new ResponseEntity(localStorage, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package org.nl.logicflow.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import org.nl.domain.LocalStorage;
|
||||
import org.nl.logicflow.service.dto.StageImageDto;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -56,4 +58,12 @@ public interface StageImageService {
|
||||
* @return
|
||||
*/
|
||||
JSONArray selectList();
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param name
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
LocalStorage upload(String name, MultipartFile file);
|
||||
}
|
||||
|
||||
@@ -2,13 +2,20 @@ package org.nl.logicflow.service.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.config.FileProperties;
|
||||
import org.nl.domain.LocalStorage;
|
||||
import org.nl.exception.BadRequestException;
|
||||
import org.nl.logicflow.service.StageImageService;
|
||||
import org.nl.logicflow.service.dto.StageImageDto;
|
||||
import org.nl.logicflow.utils.StageImageUtil;
|
||||
import org.nl.repository.LocalStorageRepository;
|
||||
import org.nl.utils.FileUtil;
|
||||
import org.nl.utils.SecurityUtils;
|
||||
import org.nl.wql.core.bean.ResultBean;
|
||||
import org.nl.wql.core.bean.WQLObject;
|
||||
@@ -16,7 +23,9 @@ import org.nl.wql.util.WqlUtil;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -28,6 +37,8 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class StageImageServiceImpl implements StageImageService {
|
||||
private final FileProperties properties;
|
||||
private final LocalStorageRepository localStorageRepository;
|
||||
@Override
|
||||
public Map<String, Object> queryAll(Map whereJson, Pageable page) {
|
||||
String where = "1=1";
|
||||
@@ -102,10 +113,30 @@ public class StageImageServiceImpl implements StageImageService {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("image_uuid", obj.getString("image_uuid"));
|
||||
json.put("image_code", obj.getString("image_code"));
|
||||
json.put("image_name", obj.getString("image_code").toString().split("-")[0]);
|
||||
json.put("image_name", obj.getString("image_code").toString().split("\\.")[0]);
|
||||
result.add(json);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalStorage upload(String name, MultipartFile multipartFile) {
|
||||
FileUtil.checkSize(this.properties.getMaxSize(), multipartFile.getSize());
|
||||
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
||||
String type = FileUtil.getFileType(suffix);
|
||||
File file = StageImageUtil.upload(multipartFile, this.properties.getPath().getPath() + type + File.separator);
|
||||
if (ObjectUtil.isNull(file)) {
|
||||
throw new BadRequestException("上传失败");
|
||||
} else {
|
||||
try {
|
||||
name = StrUtil.isEmpty(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
|
||||
LocalStorage localStorage = new LocalStorage(file.getName(), name, suffix, file.getPath(), type, FileUtil.getSize(multipartFile.getSize()));
|
||||
return (LocalStorage)this.localStorageRepository.save(localStorage);
|
||||
} catch (Exception var7) {
|
||||
FileUtil.del(file);
|
||||
throw var7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.nl.logicflow.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.utils.FileUtil;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @Author: lyd
|
||||
* @Description: 图片上传的工具类
|
||||
* @Date: 2022-08-18
|
||||
*/
|
||||
@Slf4j
|
||||
public class StageImageUtil extends cn.hutool.core.io.FileUtil {
|
||||
public static File upload(MultipartFile file, String filePath) {
|
||||
FileUtil fileUtil = new FileUtil();
|
||||
String name = fileUtil.getFileNameNoEx(file.getOriginalFilename());
|
||||
String suffix = fileUtil.getExtensionName(file.getOriginalFilename());
|
||||
|
||||
try {
|
||||
String fileName = name + "." + suffix;
|
||||
String path = filePath + fileName;
|
||||
File dest = (new File(path)).getCanonicalFile();
|
||||
if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) {
|
||||
System.out.println("was not successful.");
|
||||
}
|
||||
|
||||
file.transferTo(dest);
|
||||
return dest;
|
||||
} catch (Exception var10) {
|
||||
log.error(var10.getMessage(), var10);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user