Springboot从URL获取数据流并写入httpRespon
2018-05-15 本文已影响0人
我爱吃蛋糕_ab5e
//获取输入流
public static InputStream getInputStream() {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL("http://10.110.200.157:8080/services/attachment/file/download/AttachmentDownload?id=2429");
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
inputStream = httpURLConnection.getInputStream();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
//测试:写接口将输入流写入httpResponse
@GET
@Path(value = "/findFile/{id}")
@Operation(code = Operation.READ, desc = Operation.READ_DESC)
public String findFile(@PathParam("id") Long id) throws BusinessAccessException {
String url = "http://10.110.200.157:8080/services/attachment/file/download/AttachmentDownload?id=2429";
ServletOutputStream outputStream = null;
InputStream decryptInputStream = getInputStream();
try{
httpResponse.reset();
outputStream = httpResponse.getOutputStream();
// 在http响应中输出流
byte[] cache = new byte[1024];
int nRead = 0;
while ((nRead = decryptInputStream.read(cache)) != -1) {
outputStream.write(cache, 0, nRead);
outputStream.flush();
}
outputStream.flush();
}catch (Exception e){
e.printStackTrace();
}
return null;
}