通过httpClient请求文件流(普通文件和压缩文件)示例
Posted jianyong-long
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过httpClient请求文件流(普通文件和压缩文件)示例相关的知识,希望对你有一定的参考价值。
前言:通过浏览器请求文件流进行文件下载这里就不说了,网上有很多例子,这里主要是记录一下工作中的另一个场景,一个服务器通过HTTPClient向另一个服务请求文件流,在内存中进行业务逻辑处理,并不需要下载到本地,当然,如果你想要下载本地也是可以的,把文件流写到本地磁盘就可以了,也可以写到文件系统中。废话不多说。
一,服务器传输的是普通的文件流,没有经过压缩
服务器:
@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); } } 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) { } } }
客户端:
和第二种情况的客户端代码一样,就 不贴代码了
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);
}
}
以上是关于通过httpClient请求文件流(普通文件和压缩文件)示例的主要内容,如果未能解决你的问题,请参考以下文章
HttpClient StreamContent 附加文件名两次