rw941123 2023-04-12 10:56 采纳率: 80%
浏览 18
已结题

数据库表图片路径栏空白

上传图片之后,图片路径没办法存进数据库表中,请问是什么原因呢

<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.upload.submit();
      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("捐赠成功~");
        console.log(this.addForm);
        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.$http.post('/api/uploadImage', formdata).then((resp) => {
        if (resp.status === 200) {
          console.log(resp.data)
          this.addForm.pictures = resp.data;
          // 设置图片回显
          _this.form.logo = resp.data
          _this.$message({type: 'success', message: '图片上传成功!'})
        }
      }).catch(() => {
        this.$message({type: 'info', message: '图片太大或格式有误,上传失败,请重新上传!'})
      })
    }
  }
}

 @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);
        String receivePath = path.toString();
        return receivePath;

    }

  • 写回答

2条回答 默认 最新

  • 混分吴老师 2023-04-12 14:37
    关注

    上传图片后,图片路径无法存储到数据库表中可能有以下几个原因:

    1. 数据库表中的字段类型不匹配:如果存储图片路径的字段类型为整数类型,那么无法存储字符串类型的图片路径。需要将该字段的类型改为字符串类型,如 VARCHAR。

    2. 数据库连接异常:如果数据库连接异常,无法将数据写入数据库表中。可以检查数据库连接是否正常,以及数据库的相关配置是否正确。

    3. SQL 语句错误:如果 SQL 语句中的表名、字段名或参数名错误,也会导致无法将数据写入数据库表中。可以检查 SQL 语句是否正确,以及参数的值是否正确。

    4. 数据库表中的字段长度不足:如果存储图片路径的字段长度不足,无法存储完整的图片路径。需要将该字段的长度扩大,以存储完整的图片路径。

    5. 代码逻辑错误:可能是代码中逻辑错误导致无法将图片路径存储到数据库表中。可以检查代码逻辑是否正确,并进行调试。

    如有帮助请点击采纳

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

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?