前言:通过浏览器请求文件流进行文件下载这里就不说了,网上有很多例子,这里主要是记录一下工作中的另一个场景,一个服务器通过HTTPClient向另一个服务请求文件流,在内存中进行业务逻辑处理,并不需要下载到本地,当然,如果你想要下载本地也是可以的,把文件流写到本地磁盘就可以了,也可以写到文件系统中。废话不多说。
备注(下面所说的压缩文件都是.gz格式的)
一,服务器传输的是普通的文件流,没有经过压缩
服务器:
@RequestMapping(value = "/getCommonFile", method = RequestMethod.POST) public void getFile(HttpServletResponse response) { BufferedInputStream buffInputStream = null; OutputStream outputStream = null; try { buffInputStream = new BufferedInputStream(new FileInputStream(new File("D:\\testFile\\test.txt"))); outputStream = response.getOutputStream(); byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些 int len = 0; while((len = buffInputStream.read(buff)) > 0) { //把文件流写入到response的输出流中,供请求端请求 outputStream.write(buff, 0, len); outputStream.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(buffInputStream != null) { buffInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }
客户端:
public void getCommonFile() throws IOException { //从服务器请求文件流,具体代码就不贴了 CloseableHttpResponse response = HttpSender.toPost(FILE_URL, null); InputStream inputStream = response.getEntity().getContent(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些 int len = 0; while((len = inputStream.read(buff)) > 0) { //把从服务端读取的文件流保存到ByteArrayOutputSteam中 byteArray.write(buff, 0, len); byteArray.flush(); } inputStream.close(); response.close(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(new ByteArrayInputStream(byteArray.toByteArray()), "utf-8")); String line = null; while((line = bufferedReader.readLine()) != null) { System.out.println(line); }
二,服务器传输的是压缩的文件流(直接读取的压缩文件)
@RequestMapping(value = "/getZipFile", method = RequestMethod.POST) public void getZipFile(HttpServletResponse response) { BufferedInputStream buffInputStream = null; OutputStream outputStream = null; try { buffInputStream = new BufferedInputStream(new FileInputStream(new File("D:\\testFile\\test.gz"))); outputStream = response.getOutputStream(); byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些 int len = 0; while((len = buffInputStream.read(buff)) > 0) { //把文件流写入到response的输出流中,供请求端请求 outputStream.write(buff, 0, len); outputStream.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(buffInputStream != null) { buffInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }
客户端:
public void getZipFile() throws IOException { //从服务器请求文件流,具体代码就不贴了 CloseableHttpResponse response = HttpSender.toPost(FILE_URL, null); InputStream inputStream = response.getEntity().getContent(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些 int len = 0; while((len = inputStream.read(buff)) > 0) { //把从服务端读取的文件流保存到ByteArrayOutputSteam中 byteArray.write(buff, 0, len); byteArray.flush(); } inputStream.close(); response.close(); //GZIPInputstream解压文件,然后读取文件 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(new GZIPInputStream( new ByteArrayInputStream(byteArray.toByteArray())), "utf-8")); String line = null; while((line = bufferedReader.readLine()) != null) { System.out.println(line); } }
三,服务器传输的是压缩的文件流(直接读取的普通文件,然后在内存中将文件流进行压缩)
@RequestMapping(value = "/getCommontToZipFile", method = RequestMethod.POST) public void getCommontToZipFile(HttpServletRequest request, HttpServletResponse response) { BufferedInputStream buffInputStream = null; OutputStream outputStream = null; GZIPOutputStream zipOut = null; try { outputStream = response.getOutputStream(); buffInputStream = new BufferedInputStream(new FileInputStream(new File("D:\\testFile\\test.txt"))); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //压缩文件 zipOut = new GZIPOutputStream(byteArrayOutputStream); byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些 int len = 0; while((len = buffInputStream.read(buff)) > 0) { //把文件流写入到byteArrayOutputStream里面 zipOut.write(buff, 0, len); zipOut.flush(); } outputStream.write(byteArrayOutputStream.toByteArray()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(buffInputStream != null) { buffInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(zipOut != null) { zipOut.close(); } } catch (IOException e) { e.printStackTrace(); } } }
客户端:
和第二种情况的客户端代码一样,就 不贴代码了
四,直接读取多个压缩文件,把多个压缩文件流 写在一个byteArray中
服务端:
@RequestMapping(value = "/getMultiZipFile", method = RequestMethod.POST) public void getMultiZipFile(HttpServletRequest request, HttpServletResponse response) { ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); List<File> files = new ArrayList<File>(); File file = new File("D:\\testFile\\test1.gz"); files.add(file); file = new File("D:\\testFile\\test2.gz"); files.add(file); for(File file1 : files) { readDetailDataToByteArray(byteArray, file1); } writeToResponse(byteArray, response); } public static void readDetailDataToByteArray(ByteArrayOutputStream byteArray, File file) { BufferedInputStream bufferedInputStream = null; try { bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[1024*1024]; int j; while ((j = bufferedInputStream.read(b)) > 0) { byteArray.write(b, 0, j); byteArray.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(bufferedInputStream != null) { bufferedInputStream.close(); } } catch (Exception e) { } } } private void writeToResponse(ByteArrayOutputStream byteArray, HttpServletResponse response) { OutputStream outputStream = null; ByteArrayInputStream inputStream = null; response.addHeader(HttpHeaders.CONTENT_TYPE, "utf-8"); response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.gz" ); GZIPOutputStream zipOut = null; ByteArrayOutputStream zipOutByteArray = new ByteArrayOutputStream(); try { outputStream = response.getOutputStream(); zipOut = new GZIPOutputStream(zipOutByteArray); inputStream = new ByteArrayInputStream(byteArray.toByteArray()); byte[] b = new byte[Integer.parseInt(buffSize)]; int j; while ((j = inputStream.read(b)) > 0) { zipOut.write(b, 0, j); zipOut.flush(); }
//这里的zipOut一定要关闭,要不然客户端接收到的压缩流写到本地后,打开会报错 zipOut.close(); outputStream.write(zipOutByteArray.toByteArray()); } catch (IOException e1) { }finally { try { if(zipOutByteArray != null) { zipOutByteArray.close(); } } catch (Exception e) { } try { if(byteArray != null) { byteArray.close(); } } catch (Exception e) { } try { if(inputStream != null) { inputStream.close(); } } catch (Exception e) { } try { if(outputStream != null) { outputStream.close(); } } catch (Exception e) { } } }
客户端:
和第二种情况的客户端代码一样,就 不贴代码了