From f1477eb60dba69ce5d65c36b00951fe10a596465 Mon Sep 17 00:00:00 2001 From: Zhang Liguo <482370576@qq.com> Date: Tue, 26 Dec 2023 22:43:14 +0800 Subject: [PATCH] =?UTF-8?q?attachment=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lovenav/controller/ConfigController.java | 6 +- .../java/com/lovenav/dao/AttachmentDao.java | 2 +- .../lovenav/service/AttachmentService.java | 6 +- .../serviceImpl/AttachmentServiceImpl.java | 60 +++++++++++++++++-- .../serviceImpl/ConfigServiceImpl.java | 5 ++ 5 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/lovenav/controller/ConfigController.java b/src/main/java/com/lovenav/controller/ConfigController.java index e7c6625..6ee7ba4 100644 --- a/src/main/java/com/lovenav/controller/ConfigController.java +++ b/src/main/java/com/lovenav/controller/ConfigController.java @@ -21,9 +21,8 @@ public class ConfigController { AttachmentService attachmentService; @RequestMapping("/updateConfig") public String updateConfig(Config config){ - Date date=new Date(); try { - config.setUpdatetime(date.getTime()); + if (config.getName().equals("")||config.getValue().equals("")){ return "配置文件不能为空!"; } @@ -44,8 +43,7 @@ public class ConfigController { if (config.getName().equals("")||config.getValue().equals("")){ return "属性值不能为空"; }else { - Date date=new Date(); - config.setUpdatetime(date.getTime()); + return configService.addConfig(config); } }catch (NullPointerException e){ diff --git a/src/main/java/com/lovenav/dao/AttachmentDao.java b/src/main/java/com/lovenav/dao/AttachmentDao.java index 833d60a..4d824d2 100644 --- a/src/main/java/com/lovenav/dao/AttachmentDao.java +++ b/src/main/java/com/lovenav/dao/AttachmentDao.java @@ -15,7 +15,7 @@ public interface AttachmentDao { Attachment selectByPrimaryKey(Long id); - int updateByPrimaryKeySelective(Attachment record); + int updateByPrimaryKeySelective(Attachment attachment); int updateByPrimaryKey(Attachment record); } \ No newline at end of file diff --git a/src/main/java/com/lovenav/service/AttachmentService.java b/src/main/java/com/lovenav/service/AttachmentService.java index e7c336e..24907d3 100644 --- a/src/main/java/com/lovenav/service/AttachmentService.java +++ b/src/main/java/com/lovenav/service/AttachmentService.java @@ -10,9 +10,11 @@ import java.util.HashMap; import java.util.Map; public interface AttachmentService { - HashMap upload(MultipartFile file,String cate,Long id) throws IOException; + HashMap upload(MultipartFile file,String cate) throws IOException; + + public HashMapstoreFile(MultipartFile multipartFile,String cate) throws IOException; + public HashMapupdateFile(MultipartFile multipartFile,String cate,Config config) throws IOException; - public HashMapstoreFile(MultipartFile multipartFile,String cate,Long id) throws IOException; public Attachment selectAttachment(Long id); } diff --git a/src/main/java/com/lovenav/service/serviceImpl/AttachmentServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/AttachmentServiceImpl.java index f98b683..7b2429c 100644 --- a/src/main/java/com/lovenav/service/serviceImpl/AttachmentServiceImpl.java +++ b/src/main/java/com/lovenav/service/serviceImpl/AttachmentServiceImpl.java @@ -7,6 +7,7 @@ import com.lovenav.entity.Config; import com.lovenav.service.AttachmentService; import com.lovenav.utils.MD5Utils; import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.units.qual.C; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ResourceUtils; @@ -29,12 +30,12 @@ public class AttachmentServiceImpl implements AttachmentService { AttachmentDao attachmentDao; @Override - public HashMap upload(MultipartFile file,String cate,Long id) throws IOException { - HashMap map = storeFile(file,cate,id); + public HashMap upload(MultipartFile file,String cate) throws IOException { + HashMap map = storeFile(file,cate); return map; } @Override - public HashMap storeFile(MultipartFile multipartFile,String cate,Long id) throws IOException { + public HashMap storeFile(MultipartFile multipartFile,String cate) throws IOException { HashMap map = new HashMap<>(); Attachment attachment=new Attachment(); File path = null; @@ -66,7 +67,8 @@ public class AttachmentServiceImpl implements AttachmentService { attachment.setSuffix(suffix); attachment.setMd5(MD5Utils.md5(upload.getAbsolutePath()+ relPath)); attachment.setStorage("local"); - attachment.setId(id); +// attachment.setId(id); + attachmentDao.insertSelective(attachment); map.put("id", String.valueOf(attachment.getId())); multipartFile.transferTo(fileUp); @@ -80,6 +82,56 @@ public class AttachmentServiceImpl implements AttachmentService { } } + @Override + public HashMap updateFile(MultipartFile multipartFile, String cate, Config config) throws IOException { + HashMap map = new HashMap<>(); + Attachment attachment=new Attachment(); + File path = null; + try { + path = new File(ResourceUtils.getURL("classpath:").getPath()); + File upload = new File(path.getAbsolutePath(),"static/"+cate+"/"); + Date date=new Date(); + + DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); + DateFormat format1 = new SimpleDateFormat("yyyyMM"); + String date1=format.format(date); + String fileName = multipartFile.getOriginalFilename();//文件全名 + String date2=format1.format(date); + File parentDir = new File(upload.getAbsolutePath()+"/" + date2); + if(!upload.exists()){ + upload.mkdirs(); + } + if(!parentDir.exists()){ + parentDir.mkdirs(); + } + String suffix = suffix(fileName);//文件后缀 + String finalFileName=UUID.randomUUID().toString().replaceAll("-",""); + String relPath = "/" + date2 + "/" + finalFileName + suffix; + File fileUp = new File(upload.getAbsolutePath()+ relPath); + + attachment.setCreatetime(date); + attachment.setFileName(finalFileName); + attachment.setPath(upload.getAbsolutePath()+ "/" + date2 + "/"); + attachment.setSuffix(suffix); + attachment.setMd5(MD5Utils.md5(upload.getAbsolutePath()+ relPath)); + attachment.setStorage("local"); + attachment.setId(Long.valueOf(config.getValue())); +// attachment.setId(id); + attachmentDao.updateByPrimaryKeySelective(attachment); +// map.put("id", String.valueOf(attachment.getId())); + map.put("msg","更新成功!"); + map.put("id", String.valueOf(attachment.getId())); + multipartFile.transferTo(fileUp); + map.put("url", "/img" + relPath); + log.info(relPath); + return map; + } catch (FileNotFoundException e) { + throw e; + } catch (IOException e) { + throw e; + } + } + @Override public Attachment selectAttachment(Long id) { Attachment attachment=attachmentDao.selectByPrimaryKey(id); diff --git a/src/main/java/com/lovenav/service/serviceImpl/ConfigServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/ConfigServiceImpl.java index 752d475..be2f1ce 100644 --- a/src/main/java/com/lovenav/service/serviceImpl/ConfigServiceImpl.java +++ b/src/main/java/com/lovenav/service/serviceImpl/ConfigServiceImpl.java @@ -6,6 +6,7 @@ import com.lovenav.service.ConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.HashMap; @Service public class ConfigServiceImpl implements ConfigService { @@ -18,6 +19,8 @@ public class ConfigServiceImpl implements ConfigService { @Override public int updateConfig(Config config) { + Date date=new Date(); + config.setUpdatetime(date.getTime()); if (configDao.selectByName(config.getName())==null){ return 2; } @@ -29,6 +32,8 @@ public class ConfigServiceImpl implements ConfigService { if(configDao.selectByName(config.getName())!=null){ return "该配置文件已经存在!"; }else { + Date date=new Date(); + config.setUpdatetime(date.getTime()); int result=configDao.insertSelective(config); if (result==1){ return "添加成功!";