修改通用上传,支持单图片和批量上传
This commit is contained in:
parent
3eea2b04a8
commit
5070e42c09
|
@ -25,7 +25,7 @@ public interface ImageService extends IService<ImageEntity> {
|
|||
|
||||
List<ImageReq> batchUploadDefectImage(String partId, String imageSource, MultipartFile[] files);
|
||||
|
||||
String uploadCommonImage(String imageSource, ImageWorkReq imageWorkReq, MultipartFile file) throws IOException;
|
||||
List<String> batchUploadCommonImage(String imageSource, ImageWorkReq imageWorkReq, MultipartFile[] file) throws IOException;
|
||||
|
||||
void delete(String imageId);
|
||||
}
|
||||
|
|
|
@ -147,8 +147,9 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
|
|||
}
|
||||
|
||||
@Override
|
||||
public String uploadCommonImage(String imageSource, ImageWorkReq imageWorkReq, MultipartFile file) throws IOException {
|
||||
if(null == file){
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public List<String> batchUploadCommonImage(String imageSource, ImageWorkReq imageWorkReq, MultipartFile[] files) throws IOException {
|
||||
if(null == files || files.length == 0){
|
||||
throw new ServiceException(Message.IMAGE_IS_EMPTY);
|
||||
}
|
||||
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
|
@ -167,10 +168,14 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
|
|||
if (!FileUtil.exist(path_prefix)) {
|
||||
FileUtil.mkdir(path_prefix);
|
||||
}
|
||||
String path = path_prefix + file.getOriginalFilename();
|
||||
FileUtil.writeBytes(file.getBytes(),path);
|
||||
String url = FileUtil.FILE_SEPARATOR.concat("static").concat(FileUtil.FILE_SEPARATOR).concat("image").concat(StrUtil.SLASH).concat(StrUtil.removePrefix(path, permPath));
|
||||
return StrUtil.replace(url, FileUtil.FILE_SEPARATOR, StrUtil.SLASH);
|
||||
List<String> result = new ArrayList<>(files.length);
|
||||
for (MultipartFile multipartFile : files) {
|
||||
String path = path_prefix + multipartFile.getOriginalFilename();
|
||||
FileUtil.writeBytes(multipartFile.getBytes(),path);
|
||||
String url = FileUtil.FILE_SEPARATOR.concat("static").concat(FileUtil.FILE_SEPARATOR).concat("image").concat(StrUtil.SLASH).concat(StrUtil.removePrefix(path, permPath));
|
||||
result.add(StrUtil.replace(url, FileUtil.FILE_SEPARATOR, StrUtil.SLASH));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package com.dite.znpt.web.controller;
|
||||
|
||||
import com.dite.znpt.constant.Message;
|
||||
import com.dite.znpt.domain.Result;
|
||||
import com.dite.znpt.domain.vo.ImageWorkReq;
|
||||
import com.dite.znpt.enums.*;
|
||||
import com.dite.znpt.exception.ServiceException;
|
||||
import com.dite.znpt.service.ImageService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
@ -109,12 +111,31 @@ public class CommonController {
|
|||
@Parameter(description = "纬度")@RequestParam(required = false) String latitude,
|
||||
@Parameter(description = "海拔")@RequestParam(required = false) String altitude,
|
||||
MultipartFile file) throws IOException {
|
||||
if(null == file){
|
||||
throw new ServiceException(Message.IMAGE_IS_EMPTY);
|
||||
}
|
||||
ImageWorkReq workReq = new ImageWorkReq();
|
||||
workReq.setUploadUser(uploadUser);
|
||||
workReq.setLongitude(longitude);
|
||||
workReq.setLatitude(latitude);
|
||||
workReq.setAltitude(altitude);
|
||||
return Result.ok(imageService.uploadCommonImage(imageSource, workReq, file));
|
||||
return Result.ok(imageService.batchUploadCommonImage(imageSource, workReq, new MultipartFile[]{file}).get(0));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "批量上传图片", httpMethod = "POST")
|
||||
@PostMapping("/batch-upload-image/{imageSource}")
|
||||
public Result batchUploadImage(@PathVariable String imageSource,
|
||||
@Parameter(description = "上传用户") @RequestParam(required = false) String uploadUser,
|
||||
@Parameter(description = "经度") @RequestParam(required = false) String longitude,
|
||||
@Parameter(description = "纬度") @RequestParam(required = false) String latitude,
|
||||
@Parameter(description = "海拔") @RequestParam(required = false) String altitude,
|
||||
@RequestParam("file") MultipartFile[] files) throws IOException {
|
||||
ImageWorkReq workReq = new ImageWorkReq();
|
||||
workReq.setUploadUser(uploadUser);
|
||||
workReq.setLongitude(longitude);
|
||||
workReq.setLatitude(latitude);
|
||||
workReq.setAltitude(altitude);
|
||||
return Result.ok(imageService.batchUploadCommonImage(imageSource, workReq, files));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue