attachment修复

This commit is contained in:
Zhang Liguo 2023-12-26 22:43:14 +08:00
parent 88bd3352d5
commit f1477eb60d
5 changed files with 68 additions and 11 deletions

View File

@ -21,9 +21,8 @@ public class ConfigController {
AttachmentService attachmentService; AttachmentService attachmentService;
@RequestMapping("/updateConfig") @RequestMapping("/updateConfig")
public String updateConfig(Config config){ public String updateConfig(Config config){
Date date=new Date();
try { try {
config.setUpdatetime(date.getTime());
if (config.getName().equals("")||config.getValue().equals("")){ if (config.getName().equals("")||config.getValue().equals("")){
return "配置文件不能为空!"; return "配置文件不能为空!";
} }
@ -44,8 +43,7 @@ public class ConfigController {
if (config.getName().equals("")||config.getValue().equals("")){ if (config.getName().equals("")||config.getValue().equals("")){
return "属性值不能为空"; return "属性值不能为空";
}else { }else {
Date date=new Date();
config.setUpdatetime(date.getTime());
return configService.addConfig(config); return configService.addConfig(config);
} }
}catch (NullPointerException e){ }catch (NullPointerException e){

View File

@ -15,7 +15,7 @@ public interface AttachmentDao {
Attachment selectByPrimaryKey(Long id); Attachment selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(Attachment record); int updateByPrimaryKeySelective(Attachment attachment);
int updateByPrimaryKey(Attachment record); int updateByPrimaryKey(Attachment record);
} }

View File

@ -10,9 +10,11 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public interface AttachmentService { public interface AttachmentService {
HashMap<String,String> upload(MultipartFile file,String cate,Long id) throws IOException; HashMap<String,String> upload(MultipartFile file,String cate) throws IOException;
public HashMap<String,String>storeFile(MultipartFile multipartFile,String cate) throws IOException;
public HashMap<String,String>updateFile(MultipartFile multipartFile,String cate,Config config) throws IOException;
public HashMap<String,String>storeFile(MultipartFile multipartFile,String cate,Long id) throws IOException;
public Attachment selectAttachment(Long id); public Attachment selectAttachment(Long id);
} }

View File

@ -7,6 +7,7 @@ import com.lovenav.entity.Config;
import com.lovenav.service.AttachmentService; import com.lovenav.service.AttachmentService;
import com.lovenav.utils.MD5Utils; import com.lovenav.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
@ -29,12 +30,12 @@ public class AttachmentServiceImpl implements AttachmentService {
AttachmentDao attachmentDao; AttachmentDao attachmentDao;
@Override @Override
public HashMap<String,String> upload(MultipartFile file,String cate,Long id) throws IOException { public HashMap<String,String> upload(MultipartFile file,String cate) throws IOException {
HashMap<String, String> map = storeFile(file,cate,id); HashMap<String, String> map = storeFile(file,cate);
return map; return map;
} }
@Override @Override
public HashMap<String, String> storeFile(MultipartFile multipartFile,String cate,Long id) throws IOException { public HashMap<String, String> storeFile(MultipartFile multipartFile,String cate) throws IOException {
HashMap<String,String> map = new HashMap<>(); HashMap<String,String> map = new HashMap<>();
Attachment attachment=new Attachment(); Attachment attachment=new Attachment();
File path = null; File path = null;
@ -66,7 +67,8 @@ public class AttachmentServiceImpl implements AttachmentService {
attachment.setSuffix(suffix); attachment.setSuffix(suffix);
attachment.setMd5(MD5Utils.md5(upload.getAbsolutePath()+ relPath)); attachment.setMd5(MD5Utils.md5(upload.getAbsolutePath()+ relPath));
attachment.setStorage("local"); attachment.setStorage("local");
attachment.setId(id); // attachment.setId(id);
attachmentDao.insertSelective(attachment); attachmentDao.insertSelective(attachment);
map.put("id", String.valueOf(attachment.getId())); map.put("id", String.valueOf(attachment.getId()));
multipartFile.transferTo(fileUp); multipartFile.transferTo(fileUp);
@ -80,6 +82,56 @@ public class AttachmentServiceImpl implements AttachmentService {
} }
} }
@Override
public HashMap<String, String> updateFile(MultipartFile multipartFile, String cate, Config config) throws IOException {
HashMap<String,String> 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 @Override
public Attachment selectAttachment(Long id) { public Attachment selectAttachment(Long id) {
Attachment attachment=attachmentDao.selectByPrimaryKey(id); Attachment attachment=attachmentDao.selectByPrimaryKey(id);

View File

@ -6,6 +6,7 @@ import com.lovenav.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@Service @Service
public class ConfigServiceImpl implements ConfigService { public class ConfigServiceImpl implements ConfigService {
@ -18,6 +19,8 @@ public class ConfigServiceImpl implements ConfigService {
@Override @Override
public int updateConfig(Config config) { public int updateConfig(Config config) {
Date date=new Date();
config.setUpdatetime(date.getTime());
if (configDao.selectByName(config.getName())==null){ if (configDao.selectByName(config.getName())==null){
return 2; return 2;
} }
@ -29,6 +32,8 @@ public class ConfigServiceImpl implements ConfigService {
if(configDao.selectByName(config.getName())!=null){ if(configDao.selectByName(config.getName())!=null){
return "该配置文件已经存在!"; return "该配置文件已经存在!";
}else { }else {
Date date=new Date();
config.setUpdatetime(date.getTime());
int result=configDao.insertSelective(config); int result=configDao.insertSelective(config);
if (result==1){ if (result==1){
return "添加成功!"; return "添加成功!";