文件上传、下载、预览,前后端处理

文件上传

    @PostMapping(value = PATH + "upload")
    public ResponseEntity<FileInfo> upload(MultipartFile file) {
        //获取文件名
        String filename = file.getOriginalFilename();
        //获取文件后缀名
        String suffix = filename.split("\\.")[1];
        //获取文件输出路径
        String path = FileUtil.getFilesPath();
        //创建文件目录
        File files = new File(path);
        if (!files.exists()) {
            files.mkdir();
        }
        //获取文件uuid
        String uuidName = UUID.randomUUID().toString().replace("-", "");
        try {
            file.transferTo(new File(path, uuidName + "." + suffix));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回文件基本信息
        FileInfo fileInfo = new FileInfo();
        fileInfo.setName(filename);
        fileInfo.setUuid(uuidName + "." + suffix);
        fileInfo.setSuffix(suffix);
        fileInfo.setFilePath(path + "\\" + uuidName + "." + suffix);
        // 如果使用 fileInfoService.save(fileInfo); 先保存 则返回带有id
        return ResponseEntity.ok(fileInfo);
    }

文件下载

 @PostMapping(value = PATH + "download")
    public ResponseEntity download(@RequestBody FileInfo fileInfo) {
        //获取文件基本信息
        String uuid = fileInfo.getUuid();
        //获取所在路径
        String path = FileUtil.getFilesPath();
        File file = new File(path + "\\" + uuid);
        // 实现文件下载
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            byte[] buffer = bos.toByteArray();
            return ResponseEntity.ok().body(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return ResponseEntity.ok("下载失败!");
    }

前端处理

下载


	download(){
		this.$axios({
          method:"post",
          url:"/download",
          data:fileInfo,
          responseType: 'blob'
      }).then(result => {
        const blob = new Blob([result])
        const downloadElement = document.createElement('a')
        // 创建下载链接
        const href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        // 下载后文件名
        downloadElement.download = fileInfo.name
        document.body.appendChild(downloadElement)
        // 点击下载
        downloadElement.click()
        document.body.removeChild(downloadElement)
        window.URL.revokeObjectURL(href)
      })
	}

预览

	view(){
		this.$axios({
          method:"post",
          url:"/download",
          data:fileInfo,
          responseType: 'blob'
      }).then(result => {
        const binaryData = []
        binaryData.push(result)
        // 或 application/image
        const url = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }))
        window.open(url)
      })
	}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值