diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java index ed2900e4..f4333e2e 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java @@ -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 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(); }