package com.example.demo02.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.*; @RestController public class FileController { @RequestMapping(value = "/getCommonFile", method = RequestMethod.POST) public void getFile(HttpServletResponse response) { BufferedInputStream buffInputStream = null; OutputStream outputStream = null; try { String filePath = "E:\\ideaprojectTestFile\\excel.zip"; buffInputStream = new BufferedInputStream(new FileInputStream(new File(filePath))); 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(); } } } @RequestMapping(value = "/getZipFile", method = RequestMethod.POST) public void getZipFile(HttpServletResponse response) { BufferedInputStream buffInputStream = null; OutputStream outputStream = null; try { String filePath = "D:\\testFile\\test.gz"; buffInputStream = new BufferedInputStream(new FileInputStream(new File(filePath))); 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(); } } } }
package com.example.demo01.file; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import java.io.*; public class GetFile { public static void main(String[] args) { try { getCommonFile2(); } catch (IOException e) { e.printStackTrace(); } } public static void getCommonFile2() throws IOException { //从服务器请求文件流,具体代码就不贴了 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("http://localhost:9000/getCommonFile"); CloseableHttpResponse response = httpClient.execute(httpPost); InputStream inputStream = response.getEntity().getContent(); File outputFile = new File("E:\\ideaprojectTestFile\\excelCopy.zip"); OutputStream outputStream = new FileOutputStream(outputFile); byte[] buff = new byte[1024]; //如果是稍微大的文件,这里配置的大一些 int len = 0; while ((len = inputStream.read(buff)) > 0) { //把从服务端读取的文件流保存到ByteArrayOutputSteam中 outputStream.write(buff, 0, len); outputStream.flush(); } inputStream.close(); response.close(); outputStream.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); // } } public static void getCommonFile() throws IOException { //从服务器请求文件流,具体代码就不贴了 CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("http://localhost:9000/getCommonFile"); CloseableHttpResponse response = httpClient.execute(httpPost); InputStream inputStream = response.getEntity().getContent(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] buff = new byte[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); // } File outputFile = new File("E:\\ideaprojectTestFile\\excelCopy.zip"); OutputStream outputStream = new FileOutputStream(outputFile); byteArray.writeTo(outputStream); outputStream.close(); byteArray.close(); } }
https://www.cnblogs.com/jianyong-long/p/9693061.html
https://blog.csdn.net/justry_deng/article/details/81042379
原文:https://www.cnblogs.com/6xiong/p/14383676.html