2023.12.1版本
This commit is contained in:
parent
6d32616d24
commit
c3fb605ab6
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue