Compare commits
No commits in common. "8a275b7b219f732ceb6d8e8a784d8c3f124981b2" and "7976fc79d30f2347535b39c5f64d3a13ecf84e16" have entirely different histories.
8a275b7b21
...
7976fc79d3
|
@ -4,15 +4,10 @@ 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.*;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
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.
|
||||||
|
@ -27,6 +22,7 @@ import java.nio.file.Paths;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserService userService;
|
UserService userService;
|
||||||
|
|
||||||
|
@ -47,10 +43,6 @@ 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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,9 +26,5 @@ public class User extends Base{
|
||||||
* 用户地址
|
* 用户地址
|
||||||
*/
|
*/
|
||||||
private String address;
|
private String address;
|
||||||
/**
|
|
||||||
* 用户头像
|
|
||||||
*/
|
|
||||||
private String avatarUrl;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,22 +11,13 @@ 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);
|
||||||
|
@ -86,30 +77,4 @@ 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.
Before Width: | Height: | Size: 4.9 KiB |
Binary file not shown.
Before Width: | Height: | Size: 4.9 KiB |
Binary file not shown.
Before Width: | Height: | Size: 4.9 KiB |
Binary file not shown.
Before Width: | Height: | Size: 4.9 KiB |
Binary file not shown.
Before Width: | Height: | Size: 4.9 KiB |
Loading…
Reference in New Issue