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

我上传图片之后,后台的文件夹没有图片,数据库表的pictures里不是null,但是是空白的,也没有东西,请问是哪里有问题呢?麻烦大家帮我看一下,谢谢。

 
                  <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文件,且不超过1Mdiv>
                      el-upload>
                    el-form-item>
                  el-col>
                
 
                <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("src/main/resources/pictures/" + fileName);
        Files.write(path, b);
        return fileName;
    }
 
 
}
 

以下内容部分参考ChatGPT模型:


问题分析:

根据提问者的描述,图片已经上传成功,但是后台的文件夹没有图片,数据库表的pictures里不是null,但是是空白的,也没有东西。因此,问题可能出在后台对于上传图片的处理上。

解决思路:

  1. 检查上传图片的路径是否正确。提问者的代码中,上传的路径为 "src/main/resources/pictures/",需要确保这个路径是存在的,且有写入权限。

  2. 检查上传图片时是否出现异常。可以在后台代码中打印日志,查看是否有异常信息。例如,可以在上传图片的方法中加入try-catch语句,捕获可能出现的异常信息,并打印到日志中。

  3. 检查上传图片的文件名是否有重复。如果上传的图片文件名与已存在的文件名相同,可能会覆盖原有的文件。可以在上传图片的方法中,对于文件名进行处理,避免重复。

示例代码:

@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();
        File folder = new File("src/main/resources/pictures/");
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File uploadFile = new File(folder, fileName);
        if (uploadFile.exists()) {
            String prefix = fileName.substring(0, fileName.lastIndexOf("."));
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            fileName = prefix + "_" + System.currentTimeMillis() + suffix;
            uploadFile = new File(folder, fileName);
        }
        Files.write(uploadFile.toPath(), b);
        return fileName;
    }
}

在上面的代码中,我们对上传的文件名进行了处理,如果文件名已存在,就在文件名后面加上时间戳,避免文件名重复的问题。

另外,我们还判断了文件夹是否存在,如果不存在,就创建文件夹。这样可以确保上传的文件夹是存在的。

总结:

以上就是对于上传图片后后台文件夹不显示的问题的解决思路和示例代码。需要注意的是,我们需要对上传的文件进行一些处理,避免出现重复的文件名等问题。同时,也需要确保上传的文件夹是存在的。


如果我的建议对您有帮助、请点击采纳、祝您生活愉快

你的前端代码中上传图片的部分看起来没有问题,但是你需要确认后端代码中的图片上传路径是否正确。在这段代码中,图片上传的路径是“src/main/resources/pictures/”,这意味着图片会被上传到你的应用程序的源代码文件夹中。如果你要让上传的图片在应用程序运行时可见,你需要确保这个路径是你的应用程序的根路径下的某个文件夹。你也需要确保这个文件夹的权限允许写入文件。除此之外,你需要确认你的数据库中是否已经正确保存了上传图片的信息,包括图片的文件名、路径等等。如果数据库中没有正确保存这些信息,你需要检查一下你的数据库插入语句是否正确执行。