56 lines
1.6 KiB
Java
56 lines
1.6 KiB
Java
|
package com.example.chat.service;
|
||
|
|
||
|
import com.example.chat.dao.UserMapper;
|
||
|
import com.example.chat.dao.chat.ChatMapper;
|
||
|
import com.example.chat.entity.chat.Message;
|
||
|
import com.example.chat.entity.chat.UpdateMessage;
|
||
|
import lombok.extern.slf4j.Slf4j;
|
||
|
import org.springframework.stereotype.Service;
|
||
|
import org.springframework.web.multipart.MultipartFile;
|
||
|
|
||
|
import javax.annotation.Resource;
|
||
|
import java.io.File;
|
||
|
import java.io.IOException;
|
||
|
|
||
|
@Slf4j
|
||
|
@Service
|
||
|
public class HeadOss {
|
||
|
@Resource
|
||
|
UserMapper userMapper;
|
||
|
@Resource
|
||
|
ChatMapper chatMapper;
|
||
|
|
||
|
|
||
|
public void ModifyHead(String username, MultipartFile file){
|
||
|
String folderPath = "/www/wwwroot/oss";
|
||
|
String newFoldPath = folderPath + "/"+ username;
|
||
|
String fileName=file.getOriginalFilename();
|
||
|
File targetFile=new File(newFoldPath+"/"+fileName);
|
||
|
File newFile = new File(newFoldPath);
|
||
|
if (!newFile.exists()) {
|
||
|
if (newFile.mkdir()) {
|
||
|
log.info("创建成功");
|
||
|
}else {
|
||
|
log.error("创建失败");
|
||
|
}
|
||
|
|
||
|
}else {
|
||
|
log.info("该文件夹已存在");
|
||
|
}
|
||
|
try {
|
||
|
if (targetFile.exists()){
|
||
|
targetFile.delete();
|
||
|
}
|
||
|
file.transferTo(targetFile);
|
||
|
log.info("成功添加");
|
||
|
String head=username+"/"+fileName;
|
||
|
userMapper.modifyHead(head,username);
|
||
|
Message message=new Message(username,head,"","");
|
||
|
chatMapper.updateMessage(message);
|
||
|
} catch (IOException e) {
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|