Merge remote-tracking branch 'origin/master'

This commit is contained in:
Cool 2024-09-03 00:35:05 +08:00
commit 3be9a64ae5
8 changed files with 52 additions and 5 deletions

View File

@ -4,10 +4,15 @@ import com.alibaba.fastjson.JSONObject;
import com.bigdata.wxappserver.service.UserService; import com.bigdata.wxappserver.service.UserService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/** /**
* Created with IntelliJ IDEA. * Created with IntelliJ IDEA.
@ -22,7 +27,6 @@ import org.springframework.web.bind.annotation.RestController;
@Slf4j @Slf4j
public class UserController { public class UserController {
@Autowired @Autowired
UserService userService; UserService userService;
@ -43,6 +47,10 @@ public class UserController {
@RequestMapping("delete") @RequestMapping("delete")
public JSONObject delete(@RequestParam("id")Integer id){ public JSONObject delete(@RequestParam("id")Integer id){
return userService.delete(id); return userService.delete(id);
}
@PostMapping("/upload")
public JSONObject upload(@RequestParam("file") MultipartFile file) {
return userService.upload(file);
} }

View File

@ -26,5 +26,9 @@ public class User extends Base{
* 用户地址 * 用户地址
*/ */
private String address; private String address;
/**
* 用户头像
*/
private String avatarUrl;
} }

View File

@ -11,13 +11,22 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
public class UserService extends ServiceImpl<UserMapper, User> { public class UserService extends ServiceImpl<UserMapper, User> {
private static final String UPLOAD_DIR = "uploads/";
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public JSONObject addOrUpdate(JSONObject jsonObject) { public JSONObject addOrUpdate(JSONObject jsonObject) {
User user = jsonObject.toJavaObject(User.class); User user = jsonObject.toJavaObject(User.class);
@ -77,4 +86,30 @@ public class UserService extends ServiceImpl<UserMapper, User> {
return new JSONObject().fluentPut("data", user); return new JSONObject().fluentPut("data", user);
} }
public JSONObject upload(MultipartFile file) {
// 检查上传文件是否为空
if (file.isEmpty()) {
return new JSONObject().fluentPut("message", "上传文件为空").fluentPut("success", false);
}
try {
// 创建上传目录
File directory = new File(UPLOAD_DIR);
if (!directory.exists()) {
directory.mkdirs();
}
// 保存文件到服务器
String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
Path path = Paths.get(UPLOAD_DIR + fileName);
Files.write(path, file.getBytes());
// 构建文件的访问URL
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/" + UPLOAD_DIR)
.path(fileName)
.toUriString();
return new JSONObject().fluentPut("success", true).fluentPut("fileDownloadUri", fileDownloadUri);
} catch (IOException e) {
e.printStackTrace();
return new JSONObject().fluentPut("success", false).fluentPut("message", "上传文件失败");
}
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB