提问HttpURLConnection获取图片错误(并发高了出现)

代码如下:

//转发流的方式
HttpURLConnection conn = null;
InputStream inputStream = null;
ServletOutputStream outputStream = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
int responseCode = conn.getResponseCode();
log.info("responseCode :{}", responseCode);
conn.setConnectTimeout(300000);
conn.setReadTimeout(300000);

inputStream = conn.getInputStream();
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
log.error("获取图片错误", e);
response.setStatus(500);
if (outputStream == null) {
try {
outputStream = response.getOutputStream();
outputStream.println("get image erro , please try again test");
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}finally {
IOUtils.closeQuietly((Closeable) outputStream);
}
}
} finally {
IOUtils.closeQuietly((Closeable) inputStream);
IOUtils.closeQuietly((Closeable) outputStream);
}


错误
org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:351)
at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:776)
at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:681)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:386)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:364)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:96)
at org.apache.tomcat.util.http.fileupload.IOUtils.copyLarge(IOUtils.java:171)
at org.apache.tomcat.util.http.fileupload.IOUtils.copy(IOUtils.java:141)
at com.yitu.picture.service.PictureService.forwardingPictureQuest(PictureService.java:97)
at com.yitu.picture.controller.PictureController.forwardingPictureQuest(PictureController.java:24)
at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)

如果加上每次断开链接
conn.setRequestProperty("Connection", "close");

错误变成:
erro.JPG

 

请登录后发表评论

    没有回复内容