IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    mysql文件上传下载 vue下载

    John Doe发表于 2023-08-23 02:17:42
    love 0

    JAVA文件以流的形式存在Mysql(blob)数据库,mysql最大支持4G文件(longblb)

    直接将流转换为byte存入数据库

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35

    public Response uploadBlob(MultipartFile file) {
    //数据库实体类 改成自己的
    FileUpload fileUpload = new FileUpload();
    fileUpload.setFileName(file.getOriginalFilename());
    try {
    fileUpload.setContent(FileTypeUtils.input2byte(file.getInputStream()));
    } catch (IOException e) {
    e.printStackTrace();
    }
    save(fileUpload);
    return Response.ok("成功", fileUpload.getId());
    }


    /**
    * 将 流 转换为byte
    *
    * @param inStream
    * @return
    * @throws IOException
    */
    public static byte[] input2byte(InputStream inStream) throws IOException {
    try (ByteArrayOutputStream swapStream = new ByteArrayOutputStream()) {
    byte[] buff = new byte[inStream.available()];
    while (inStream.read(buff) != -1) {
    swapStream.write(buff);
    }
    return swapStream.toByteArray();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }

    下载文件输出文件流到前端

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

    public void getFile(String id, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
    // 获取数据库文件信息
    FileUpload fileUpload = getById(id);
    if (fileUpload.getContent() != null) {
    //获取存储的文件名称
    String filename = fileUpload.getFileName();
    //获取浏览器版本
    String userAgent = request.getHeader("USER-AGENT");
    //IE浏览器
    if (StringUtils.contains(userAgent, "MSIE")) {
    filename = URLEncoder.encode(filename, "UTF8");
    }
    //google,火狐浏览器
    else if (StringUtils.contains(userAgent, "Mozilla")) {
    filename = new String(filename.getBytes(), "ISO8859-1");
    }
    //其他浏览器
    else {
    filename = URLEncoder.encode(filename, "UTF8");
    }
    try {
    byte[] fileStream = fileUpload.getContent();
    response.setHeader("content-type", "application/octet-stream");
    //这边可以设置文件下载时的名字,我这边用的是文件原本的名字,可以根据实际场景设置
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
    toClient.write(fileStream);
    toClient.flush();
    toClient.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    流的方式前端要指定 responseType: ‘blob’

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    methods: {
    downLoad() {
    if (this.fileId) {
    request({
    url: "localhost:8080/file/getFile",
    method: "get",
    params: { id: this.fileId },
    //指定 responseType
    responseType: 'blob'
    }).then((resp) => {
    let blob = new Blob([resp],{
    type:'application/vnd.ms-excel'
    });
    let objectUrl = URL.createObjectURL(blob); //生成一个url
    this.downloadFile(objectUrl,this.form.fileName)
    });
    }
    }
    }

    转成文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public File getFile(String id, HttpServletRequest request, HttpServletResponse response) {
    // 获取数据库文件信息
    FileUpload fileUpload = getById(id);
    if (fileUpload.getContent() != null) {
    File file = null;
    try {
    file = new File(fileUpload.getFileName());
    if (!file.exists()) {
    file.createNewFile();
    }
    OutputStream os = new FileOutputStream(file);
    os.write(fileUpload.getContent());
    os.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return file;
    }
    }


沪ICP备19023445号-2号
友情链接