修改上传图像,增加图像预览功能

This commit is contained in:
gaoxiong 2025-05-19 23:31:27 +08:00
parent fbddea1c30
commit 3eea2b04a8
8 changed files with 29 additions and 19 deletions

View File

@ -20,14 +20,13 @@ public class WebMvcConfig extends WebMvcConfigurationSupport {
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/temp/**").addResourceLocations("file:" + uploadTempPath);
registry.addResourceHandler("/image/**").addResourceLocations("file:" + uploadPermPath);
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
registry.addResourceHandler("/static/image/**").addResourceLocations("file:" + uploadPermPath);
registry.addResourceHandler("/static/image/temp/**").addResourceLocations("file:" + uploadTempPath);
registry.addResourceHandler("/upload/**").addResourceLocations("file:D:\\Upload\\Image\\Temp\\");
super.addResourceHandlers(registry);
}
}

View File

@ -27,7 +27,7 @@ public class ProjectReq implements Serializable {
@ApiModelProperty("项目名称")
private String projectName;
@Size(groups = {ValidationGroup.Insert.class, ValidationGroup.Update.class}, max = 100, message = "项目封面述长度不能超过100字符")
@Size(groups = {ValidationGroup.Insert.class, ValidationGroup.Update.class}, max = 200, message = "项目封面述长度不能超过200字符")
@ApiModelProperty("项目封面")
private String coverUrl;

View File

@ -49,7 +49,7 @@ public class TurbineReq implements Serializable {
@ApiModelProperty("机组型号")
private String turbineModel;
@Size(groups = {ValidationGroup.Insert.class, ValidationGroup.Update.class}, max = 100, message = "机组封面图长度不能超过100字符")
@Size(groups = {ValidationGroup.Insert.class, ValidationGroup.Update.class}, max = 200, message = "机组封面图长度不能超过200字符")
@ApiModelProperty("机组封面图")
private String turbineCoverUrl;

View File

@ -27,7 +27,7 @@ public class UserReq implements Serializable {
@ApiModelProperty("账号")
private String account;
@Size(groups = {ValidationGroup.Insert.class, ValidationGroup.Update.class}, max = 100, message = "头像地址长度不能超过100字符")
@Size(groups = {ValidationGroup.Insert.class, ValidationGroup.Update.class}, max = 200, message = "头像地址长度不能超过200字符")
@ApiModelProperty("头像地址")
private String avatar;

View File

@ -15,7 +15,7 @@ import java.util.List;
public enum ImageSourceEnum {
COLLECT("collect", "图像采集", Boolean.TRUE),
OUT_WORK("out-work", "外部工作", Boolean.TRUE),
IN_WORK("in-work", "部工作", Boolean.TRUE),
IN_WORK("in-work", "部工作", Boolean.TRUE),
LIGHTNING_PROTECTING_WORK("lightning-protection-work", "防雷工作", Boolean.FALSE);
private final String code;

View File

@ -21,6 +21,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
@ -33,6 +35,8 @@ public class ImageCollectServiceImpl extends ServiceImpl<ImageCollectMapper, Ima
@Value("${upload.perm-path.image}")
private String permPath;
@Value("${upload.temp-path.image}")
private String tempPath;
@Resource
private ImageService imageService;
@ -51,17 +55,20 @@ public class ImageCollectServiceImpl extends ServiceImpl<ImageCollectMapper, Ima
}
ImageCollectEntity imageCollect = Converts.INSTANCE.toImageCollectEntity(req);
this.save(imageCollect);
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
List<ImageEntity> imageList = Converts.INSTANCE.toImageEntity(req.getImageList());
String path_prefix = permPath.concat(FileUtil.FILE_SEPARATOR).concat(ImageSourceEnum.COLLECT.getCode()).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR);
String permPathPrefix = permPath.concat(ImageSourceEnum.COLLECT.getCode()).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR).concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
String temPathPrefix = tempPath.concat(ImageSourceEnum.COLLECT.getCode()).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR);
imageList.stream().forEach(image -> {
image.setPartId(partId);
image.setCollectId(imageCollect.getCollectId());
String path = path_prefix + image.getImageName();
File file = new File(image.getImagePath());
String path = permPathPrefix + image.getImageName();
File file = new File(temPathPrefix.concat(image.getImageName()));
if (file.exists()) {
byte[] bytes = FileUtil.readBytes(file);
FileUtil.writeBytes(bytes, path);
image.setImagePath(path);
String url = "/static/image/".concat(StrUtil.removePrefix(path,permPath).replace(FileUtil.FILE_SEPARATOR, StrUtil.SLASH));
image.setImagePath(url);
FileUtil.del(file);
}else {
imageList.remove(image);

View File

@ -131,11 +131,11 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
// 创建完整的目录
temCategory.mkdirs();
}
String path_prefix = tempPath.concat(StrUtil.BACKSLASH).concat(imageSource).concat(StrUtil.BACKSLASH).concat(partId).concat(StrUtil.BACKSLASH);
String temPathPrefix = tempPath.concat(imageSource).concat(FileUtil.FILE_SEPARATOR).concat(partId).concat(FileUtil.FILE_SEPARATOR);
for (MultipartFile file : files) {
if (!file.isEmpty()) {
try {
String path = path_prefix + file.getOriginalFilename();
String path = temPathPrefix + file.getOriginalFilename();
FileUtil.writeBytes(file.getBytes(),path);
list.add(imageRespBuilder(path));
} catch (Exception e) {
@ -152,7 +152,7 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
throw new ServiceException(Message.IMAGE_IS_EMPTY);
}
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String path_prefix = permPath.concat(FileUtil.FILE_SEPARATOR).concat(dateStr).concat(FileUtil.FILE_SEPARATOR).concat(imageSource).concat(FileUtil.FILE_SEPARATOR);
String path_prefix = permPath.concat(imageSource).concat(FileUtil.FILE_SEPARATOR).concat(dateStr).concat(FileUtil.FILE_SEPARATOR);
if (Objects.nonNull(imageWorkReq)) {
if (StrUtil.isNotBlank(imageWorkReq.getUploadUser())) {
path_prefix = path_prefix.concat(imageWorkReq.getUploadUser()).concat(FileUtil.FILE_SEPARATOR);
@ -169,7 +169,8 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
}
String path = path_prefix + file.getOriginalFilename();
FileUtil.writeBytes(file.getBytes(),path);
return 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);
}
@Transactional(rollbackFor = Exception.class)
@ -197,7 +198,8 @@ public class ImageServiceImpl extends ServiceImpl<ImageMapper, ImageEntity> impl
req.setCameraManufacturer(obj.getStr("Make"));
req.setCameraModel(obj.getStr("Model"));
req.setImageName(obj.getStr("File Name"));
req.setImagePath(StrUtil.removePrefix(path, tempPath));
String url = FileUtil.FILE_SEPARATOR.concat("static").concat(FileUtil.FILE_SEPARATOR).concat("image").concat(FileUtil.FILE_SEPARATOR).concat("temp").concat(StrUtil.SLASH).concat(StrUtil.removePrefix(path, tempPath));
req.setImagePath(StrUtil.replace(url, FileUtil.FILE_SEPARATOR, StrUtil.SLASH));
BigDecimal imageSize = new BigDecimal(extractDigit(obj.getStr("File Size"))).divide(new BigDecimal(1024*1024), 4, RoundingMode.HALF_UP);
req.setImageSize(imageSize.toString().concat("M"));
req.setImageWidth(extractDigit(obj.getStr("Image Width")));

View File

@ -117,6 +117,8 @@ zlm-config:
dynamicPortEnd: 30185
upload:
temp-path: F:\Upload\Image\Temp
perm-path: F:\Upload\Image
temp-path:
image: D:\Upload\Image\Temp\
perm-path:
image: D:\Upload\Image\