SpringBoot+Vue实现文件上传功能

目录

1.后端代码部分:

2.前端代码部分

3.效果展示

1.后端代码部分:

@RestController
@RequestMapping("/file")
public class FileController {
    private final String UPLOAD_PATH = "D:/OBS/";//这里写上你需要上传的路径(模拟服务器)

    @PostMapping("/upload")
    public ResponseEntity uploadFile(@RequestParam MultipartFile file) {
        // ... File upload logic ...
        if (file.isEmpty()) {
            return new ResponseEntity<>("文件不能为空", HttpStatus.BAD_REQUEST);
        }
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOAD_PATH + File.separator + file.getOriginalFilename());
            Files.write(path, bytes);
            return new ResponseEntity<>("文件上传成功", HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>("文件上传失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

2.前端代码部分



  

3.效果展示

 

 

 SpringBoot+Vue实现文件上传功能_第1张图片

你可能感兴趣的:(vue.js,spring,boot,前端,后端)