文件上传和文件下载,(基于ssm框架)

1.文件上传

  •  将文件以二进制流的形式存储到数据库中(mysql),数据库中的文件是以byte[]的形式存在的,也就是说数据库中存放文件的字段是byte[]类型的。
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
  public Response saveFile(HttpSession session, Long Id, MultipartFile file) throws IOException {
    byte[] Doc = file.getBytes();
    //将图片以字节数组的形式 添加到数据库中
    merchantApplyMapper.add(Id, Doc);

    return setSuccessResponse();
  }

2.文件下载

  • 文件下载可以说是文件上传的逆过程,文件下载只需要将数据库中的字节数组取出来传到前台就ok了,是不是so easy!!
 @RequestMapping(value = "/file/{文件Id}", method = RequestMethod.GET)
  public ResponseEntity getFile(@PathVariable("id") Long id) {
  
    MerchantApply File= merchantApplyMapper.selectById(id);

    return ResponseEntity.ok().header("Content-Disposition",
      "attachment; filename=" + doc.getUserName() + ".docx")
      .contentType(MediaType.APPLICATION_OCTET_STREAM).body(doc.getApplyDoc());
  }

前台界面代码实现:(基于 react )

  const fileUrl = `localhost:8080/file/${文件id}`;
  文件下载

不管界面使用什么语言,后台的实现原理都是一样的喔!

纯属个人总结,有不对的地方欢迎指出!!

文件上传和文件下载,(基于ssm框架)_第1张图片

你可能感兴趣的:(ssm,React)