vue+springboot上传图片

后端代码

package com.example.demo.WebController;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/file")
public class UpLoadImage {

    @PostMapping("/upload")
    public  String UpLoadImg(@RequestParam("file") MultipartFile multipartFile)
    {
        System.out.println("-------------开始--------------");
        System.out.println(multipartFile);
        System.out.println("-------------结束--------------");

        String filePath="C:\\opt\\upload\\file\\imgPool\\888.jpg";
        File file = new File(filePath);
        try {
            multipartFile.transferTo(file);  //转存文件
        } catch (IOException e) {
            //throw new RuntimeException(e);
            return "false";
        }

        return "success";
    }
}

前端代码

   <el-form-item label="文件">
        <el-upload
          :action="'/file/upload?dir=imgPool'"
          :show-file-list="false"
          :headers="uploadHeaders"
          list-type="picture-card"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <el-image v-if="filePath" style="width: 100%;height: 100%" :src="fileUrl+filePath"/>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        
      </el-form-item>

前端读取图片有两种方式

1、springboot做映射;

2、nginx做映射,将静态资源发布成网站

先说1:

新建一个java类

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Component
public class MyWebMvcConfig implements WebMvcConfigurer {

    private String filePath = "C:/opt/upload/file/imgPool";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/imgPool/**").addResourceLocations("file:" + filePath + "/");
    }
}

第二种方法:

nginx配置本地静态资源_糖朝的博客-CSDN博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
这里提供一个简单的示例代码。 前端部分:使用Vue.js实现图片上传 1. 在Vue组件中定义一个文件上传input元素,并绑定一个change事件: ```html <template> <div> <input type="file" @change="handleFileUpload"> </div> </template> ``` 2. 在Vue组件中定义一个handleFileUpload方法,当文件上传input的值发生变化时,将上传的文件存储到data属性中的uploadedFile中: ```javascript <script> export default { data() { return { uploadedFile: null } }, methods: { handleFileUpload(event) { this.uploadedFile = event.target.files[0]; } } } </script> ``` 3. 在Vue组件中定义一个submit方法,将上传的文件发送到后端: ```javascript <script> export default { methods: { submit() { let formData = new FormData(); formData.append('file', this.uploadedFile); axios.post('/api/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' } }) .then(response => { console.log(response.data); }) .catch(error => { console.log(error); }); } } } </script> ``` 后端部分:使用Spring Boot实现图片上传 1. 在Spring Boot中定义一个Controller,处理文件上传的请求: ```java @RestController @RequestMapping("/api") public class FileUploadController { @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { try { byte[] bytes = file.getBytes(); Path path = Paths.get("uploads/" + file.getOriginalFilename()); Files.write(path, bytes); return "File uploaded successfully!"; } catch (IOException e) { e.printStackTrace(); return "File upload failed!"; } } } ``` 2. 在Spring Boot的application.properties中配置文件上传的相关属性: ```properties spring.servlet.multipart.enabled=true spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB ``` 注意:这里的配置表示上传的文件大小不能超过10MB。 以上就是一个简单的vue+springboot上传图片的代码实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖朝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值