1.数据流生成文件
BufferedOutputStream fos2 = null; BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream("F:/user/测试.docx")); fos2 = new BufferedOutputStream(new FileOutputStream("F:/user/测试2.docx")); byte[] buf = new byte[1024]; byte[] buf2 = new byte[1024]; int len; while (true) { if (!((len = bis.read(buf2, 0, buf2.length)) > 0)) break; fos2.write(buf2, 0, len); } fos2.flush(); fos2.close(); bis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }
2.minio文件流下载
OutputStream outputStream = null; InputStream inputStream = null; try { inputStream = minioUtils.getObject(GetObjectArgs.builder() .bucket("bucketName").object("filePath").build()) // response.setContentType("multipart/form-data"); response.setContentType("application/octet-stream;charset=utf8"); response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("测试.doc", "UTF-8")); outputStream = response.getOutputStream(); byte[] buf = new byte[1024]; int len; while ((len= inputStream.read(buf, 0, buf.length)) > 0) { outputStream.write(buf, 0, len); } outputStream.flush(); outputStream.close(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e2) { log.info("关闭输入流时出现错误", e2); } } if (outputStream != null) { try { outputStream.close(); } catch (Exception e2) { log.info("关闭输入流时出现错误", e2); } } }
3.数据流式下载,ContentType设置在流处理前,否则设置失效。