diff --git a/src/main/java/com/example/chat/controller/IndexController.java b/src/main/java/com/example/chat/controller/IndexController.java new file mode 100644 index 0000000..33c4ebd --- /dev/null +++ b/src/main/java/com/example/chat/controller/IndexController.java @@ -0,0 +1,23 @@ +package com.example.chat.controller; + +import com.example.chat.service.HeadOss; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.annotation.Resource; +import java.io.File; + +@Slf4j +@CrossOrigin +@RestController +public class IndexController { + + @Resource + HeadOss headOss; + + @RequestMapping("/chat/modifyHead") + public void modifyHead(@RequestParam("file")MultipartFile file, @RequestParam("username") String username) { + headOss.ModifyHead(username,file); + } +} diff --git a/src/main/java/com/example/chat/service/HeadOss.java b/src/main/java/com/example/chat/service/HeadOss.java new file mode 100644 index 0000000..1c92c84 --- /dev/null +++ b/src/main/java/com/example/chat/service/HeadOss.java @@ -0,0 +1,55 @@ +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); + } + } + +} diff --git a/src/main/java/com/example/chat/test/Test01.java b/src/main/java/com/example/chat/test/Test01.java new file mode 100644 index 0000000..61f5f75 --- /dev/null +++ b/src/main/java/com/example/chat/test/Test01.java @@ -0,0 +1,17 @@ +package com.example.chat.test; + +import org.junit.Test; + +public class Test01 { + + @Test + public void Test11(){ + String currentDir = System.getProperty("user.dir"); + System.out.println("当前项目的路径是:" + currentDir+"\\com\\example"); + + ClassLoader classLoader = getClass().getClassLoader(); + String currentDir1 = classLoader.getResource("").getPath(); + System.out.println("当前项目的路径是:" + currentDir1); + + } +}