rw941123 2023-04-10 19:07 采纳率: 80%
浏览 21
已结题

上传图片,后台保存文件里没有

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

img

img

 <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("src/main/resources/pictures/" + fileName);
        Files.write(path, b);
        return fileName;
    }
 
 
}

@RequestMapping("/addIn2")
    public String addIn2(@RequestBody InStock inStock){
        inStock.setState(0);
        inStock.setFromwho(2);
        int i = inStockDao.addIn(inStock);
        return i > 0 ? "success":"error";
    }
 

<insert id="addIn">
        INSERT INTO instock
            (itemname,numbers,fromwho,worker,state,unit,pictures)
            VALUE
            (#{itemname},#{numbers},#{fromwho},#{worker},#{state},#{unit},#{pictures})
    </insert>
 

  • 写回答

1条回答 默认 最新

  • nssnail 2023-04-10 20:03
    关注

    本地目录也没有图片应该是写入那步就有问题,debugger调试一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上