资源位后台管理--修改--修改批量修改指定资源位图片方法中的文件上传方式

This commit is contained in:
haotian 2025-03-20 14:38:55 +08:00
parent 5c112d7c05
commit cd1b65ccee

View File

@ -682,47 +682,23 @@ public class SysSourceController extends BaseController {
return error("头像文件上传失败");
}
//----------------------------------------------新版文件上传end-----------------------------------------------
// 验证文件类型可选这里只允许jpg,jpeg和png格式
String fileType = sourceImage.getContentType().split("/")[1].toLowerCase();
if (!"jpg".equals(fileType) && !"jpeg".equals(fileType) && !"png".equals(fileType)) {
return error("只支持jpg和png格式的图片");
}
String localPath = RuoYiConfig.getProfile();
String filePath = localPath + "/single_source";
try {
// 获取当前时间的Instant对象
Instant now = Instant.now();
// 将Instant转换为指定时区的ZonedDateTime对象
ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault());
// 定义时间格式ISO 8601格式
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 格式化ZonedDateTime为字符串
String timestampString = zonedDateTime.format(formatter).replace("-", "_").replace(":", "_");
System.out.println("上传文件名: "+sourceId+"_"+timestampString+".jpg");
// 保存文件
sourceImage.transferTo(new File(filePath, sourceId+"_"+timestampString+".jpg"));
// 修改资源位的图片, 先都能修改吧
SysSource sysSource = sysSourceService.selectSysSourceBySourceId(sourceId);
sysSource.setWorkAddress(filePath+"/"+sourceId+"_"+timestampString+".jpg");
sysSource.setWorkAddress(sourceImagePath);
int flag = sysSourceService.updateSysSource(sysSource);
if (flag > 0){
AjaxResult ajax = success();
ajax.put("workAddress", filePath+"/"+sourceId+"_"+timestampString+".jpg");
ajax.put("workAddress", sourceImagePath);
return ajax;// 可以返回文件的相对路径或URL给前端
}
else{
return error("修改头像失败");
}
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
return error("文件上传失败");
}
@ -740,64 +716,40 @@ public class SysSourceController extends BaseController {
if (sourceImage.isEmpty()) {
return error("文件为空");
}
// 验证文件类型可选这里只允许jpg,jpeg和png格式
String fileType = sourceImage.getContentType().split("/")[1].toLowerCase();
if (!"jpg".equals(fileType) && !"jpeg".equals(fileType) && !"png".equals(fileType)) {
return error("只支持jpg和png格式的图片");
//------------------------------------------------新版文件上传-------------------------------------------------
String sourceImagePath = "";
try{
sourceImagePath = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/single_source", sourceImage);
}
catch (Exception e){
return error("头像文件上传失败");
}
//----------------------------------------------新版文件上传end-----------------------------------------------
System.out.println("sourceImagePath: "+sourceImagePath);
String localPath = RuoYiConfig.getProfile();
String filePath = localPath + "/single_source";
// 获取当前时间的Instant对象
Instant now = Instant.now();
// 将Instant转换为指定时区的ZonedDateTime对象
ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault());
// 定义时间格式ISO 8601格式
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 格式化ZonedDateTime为字符串
String timestampString = zonedDateTime.format(formatter).replace("-", "_").replace(":", "_");
// System.out.println("上传文件名: "+sourceStrList+"_"+timestampString+".jpg");
List<Double> sourceIdList = GsonUtils.fromJson(sourceStrList, List.class);
// 保存文件
try{
sourceImage.transferTo(new File(filePath, sourceIdList.get(0).longValue()+"_"+timestampString+".jpg"));
}catch (IOException e) {
e.printStackTrace();
return error("文件上传失败");
}
for (Double sourceDId : sourceIdList) {
Long sourceId = sourceDId.longValue();
System.out.println("sourceId: "+sourceId);
// 修改资源位的图片, 先都能修改吧
SysSource sysSource = sysSourceService.selectSysSourceBySourceId(sourceId);
sysSource.setWorkAddress(filePath+sourceIdList.get(0).longValue()+"_"+timestampString+".jpg");
sysSource.setWorkAddress(sourceImagePath);
int flag = sysSourceService.updateSysSource(sysSource);
if (flag == 0){
return error("修改像失败");
return error("修改资源位默认图像失败");
}
else{
continue;
}
}
AjaxResult ajax = success();
ajax.put("workAddress", filePath+sourceIdList.get(0).longValue()+"_"+timestampString+".jpg");
ajax.put("workAddress", sourceImagePath);
return ajax;// 可以返回文件的相对路径或URL给前端
// return success();
}