rw941123 2023-04-10 14:14 采纳率: 80%
浏览 42
已结题

vue+springboot上传图片后台文件夹不显示

我想上传图片到后台文件夹,并且数据库中也可以存储图片,我现在的代码是下面这样的,可以成功捐赠没有报错,但是后台的文件夹里没有图片,数据库的表里不是null但是也没有东西,是空的,请问是哪里有问题吗

 <el-row>
                  <el-col :span="8">
                    <el-form-item label="物资图片" prop="unit">
                      <el-upload
                          accept="image/jpeg,image/png"
                          :on-preview="handlePreview"
                          :on-remove="handleRemove"
                          :before-remove="beforeRemove"
                          ref="upload"
                          :action="'http://localhost:8989/api/uploadImage'"
                          :http-request="upload"
                          :auto-upload="false"
                          :before-upload="onBeforeUpload"
                          multiple
                          :limit="1"
                          :on-exceed="handleExceed"
                          v-model="addForm.pictures">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过1M</div>
                      </el-upload>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-form-item>
                  <el-button type="primary" @click="addIn">立即捐赠</el-button>
                </el-form-item>


methods:{
    addIn() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return;
        const {data: res} = await this.$http.post("addIn2", this.addForm);
        if (res != "success") {
          return this.$message.error("捐赠失败~");
        }
        this.$message.success("捐赠成功~");
        this.addDialogVisible = false;
      });
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }?`);
    },
    onBeforeUpload(file)
    {
      const isIMAGE = file.type === 'image/jpeg'||'image/png';
      const isLt1M = file.size / 1024 / 1024 < 1;

      if (!isIMAGE) {
        this.$message.error('上传文件只能是图片格式!');
      }
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 1MB!');
      }
      return isIMAGE && isLt1M;
    },
    upload (file) {
      const _this = this
      let formdata = new FormData()

      // 上传图片并转成Base64编码
      formdata.append('files', file.file)
      console.log(formdata)

      this.$axios.post('/uploadImage', formdata).then((resp) => {
        if (resp.status === 200) {
          console.log(resp.data)
          // 设置图片回显
          _this.form.logo = resp.data
          _this.$message({type: 'success', message: '图片上传成功!'})
        }
      }).catch(() => {
        this.$message({type: 'info', message: '图片太大或格式有误,上传失败,请重新上传!'})
      })
    }
  }
}



@RestController
public class PicturesController {

    @RequestMapping(value = "/api/uploadImage", method = RequestMethod.POST)
    @ResponseBody
    public String uploadImage(@RequestParam("files") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename() + "图片已传入!!");
        byte[] b = file.getBytes();
        String fileName = file.getOriginalFilename();
        Path path = Paths.get("springboot/src/main/resources/pictures/" + fileName);
        Files.write(path, b);
        return fileName;
    }


}

  • 写回答

3条回答 默认 最新

  • 熊熊爱绵羊 2023-04-10 15:34
    关注

    第一百行 Paths.get("springboot/src/main/resources/pictures/" + fileName)改为Paths.get("src/main/resources/pictures/" + fileName)即可。
    解释说明: Paths.get方法源码是调用了FileSystems.getDefault() ,该方法的返回值是当前应用的目录,也就是到你的应用springboot目录,所以你只需要写应用内的目录即可
    另外补充说明: 你的 resources目录下需要有 pictures这个目录

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月18日
  • 已采纳回答 4月10日
  • 创建了问题 4月10日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效