• springboot+vue上传图片


    这里是一个简单的示例,演示了如何在Spring Boot中从Vue.js上传图像:

    1.前端Vue.js代码:

    <template>
      <div>
        <input type="file" @change="handleFileUpload">
        <button @click="uploadImage">上传图片button>
      div>
    template>
    
    <script>
    export default {
      name: 'ImageUploader',
      data() {
        return {
          imageFile: null,
          imageUrl: ''
        };
      },
      methods: {
        handleFileUpload(event) {
          this.imageFile = event.target.files[0];
        },
        uploadImage() {
          let formData = new FormData();
          formData.append('image', this.imageFile);
          axios.post('/api/image/upload', formData, {
            headers: {
              'Content-Type': 'multipart/form-data'
            }
          }).then(response => {
            this.imageUrl = response.data.imageUrl;
          });
        }
      }
    };
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    1. 后端Spring Boot代码:
    @RestController
    public class ImageController {
    
        @Value("${upload.path}")
        private String uploadPath;
    
        @PostMapping("/api/image/upload")
        public ResponseEntity<?> uploadImage(@RequestParam("image") MultipartFile file) {
            Map<String, Object> response = new HashMap<>();
            try {
                if (!file.isEmpty()) {
                    String fileName = file.getOriginalFilename();
                    Path filePath = Paths.get(uploadPath + "/" + fileName);
                    Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
                    String imageUrl = "/api/image/" + fileName;
                    response.put("imageUrl", imageUrl);
                } else {
                    response.put("status", "error");
                    response.put("message", "No file uploaded");
                    return ResponseEntity.badRequest().body(response);
                }
            } catch (IOException e) {
                response.put("status", "error");
                response.put("message", e.getMessage());
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
            }
            response.put("status", "success");
            return ResponseEntity.ok(response);
        }
    
        @GetMapping("/api/image/{fileName:.+}")
        public ResponseEntity<Resource> getImage(@PathVariable String fileName) throws IOException {
            Path filePath = Paths.get(uploadPath + "/" + fileName);
            Resource resource = new UrlResource(filePath.toUri());
            return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(resource);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    这是一个非常简单的示例,涵盖了从Vue.js前端上传一个图像并将其保存到Spring Boot后端服务器的全部过程。你可以根据实际需求进行修改和扩展。

  • 相关阅读:
    用DIV+CSS技术设计的网页与实现制作【体育文化】dreamweaver学生网页设计
    《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
    缓存技术使用
    【Typescript】面向对象(上篇),包含类,构造函数,继承,super,抽象类
    #mysql错误01#
    如何做软件产品介绍的网站?
    go搭建服务器基础
    关于大模型是否开源的分析
    MySQL篇---第三篇
    Linux命令查询版本命令
  • 原文地址:https://blog.csdn.net/qq_36151389/article/details/132856978