图片上传下载

数据模型:

imageUrl: '',

<el-form-item label="楼盘图片:" prop="pic" class="uploadImg" v-model="emp.pic">
                <el-upload
                        class="avatar-uploader"
                        action="/premises/upload"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

/*图片上传下载*/
            handleAvatarSuccess(res, file) {
                this.imageUrl = "/premises/download?filename="+res;
                this.emp.pic=res;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },

图片上传

/**
     * 图片上传
     * @return
     */
  

@PostMapping("upload")
public String Upload(MultipartFile file){
    //获取文件名称,使用UUID工具类为文件重命名
    String filename = file.getOriginalFilename();
    System.out.println(filename);
    String fileType = filename.substring(filename.lastIndexOf("."));
    filename= UUID.randomUUID().toString().replace("-","")+fileType;
    // File picFile = new File("C://Users//17630//Pictures//Saved Pictures//"+filename);
    File picFile = new File("D:\\img\\"+filename);
    try {
        file.transferTo(picFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return filename;
}

图片下载


    @GetMapping("/download")
    public void download(String filename, HttpServletResponse response){
        FileInputStream fileInputStream=null;
        ServletOutputStream outputStream=null;
        try {
            fileInputStream=new FileInputStream(new File("D://imgs/"+filename));
            outputStream= response.getOutputStream();
            int len=0;
            byte[] bytes=new byte[1024];
            while ((len=fileInputStream.read(bytes))!=-1){
                outputStream.write(bytes);
                outputStream.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

表格

<template slot-scope="scope">
                    <img style="width:100px;height: 50px" v-bind:src="'/premises/download?name='+scope.row.pic">
                </template>

  • 21
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

瓶盖子io

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

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

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

打赏作者

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

抵扣说明:

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

余额充值