https文件下载异常问题排查
项目场景:
博主今天在生产环境遇到一个图片下载的问题,生产服务器图片下载报错,导致下载异常通过日志文件发现报错Caused by: java.net.ConnectException: Connection timed out (Connection timed out)连接超时,但是项目当中代码并未设置图片的url读取超时时间,代码片段如下:URLConnection并未设置超时时间,所以当时就怀疑是否是nginx设置了超时时间或者前端设置了超时时间。
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLUtil.encode(fileName.replace(",", ",")));
URL url = new URL(uploadFileEntity.getFilePath());
URLConnection connection = url.openConnection();
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
outputStream.close();
inputStream.close();
问题描述
这里我们是通过 new URL方法来获取请求连接的,此处的连接有可能是http协议的地址,也可能是https协议的地址。openConnection方法在http协议下返回的是HttpURLConnection,而https协议返回的是HttpsURLConnection。
生产环境下的图片地址是https协议的ip+端口的形式的地址,此时代码在connection.getInputStream()时会报错超时,导致接口响应失败。
原因分析:
对于这种超时,我们需要考虑的几个点,前端超时、后端超时、还有nginx超时。
前端超时: 系统数据还未完全的响应完成,前端过了超时时间就会断开http/https连接导致超时异常(怀疑)。
后端超时: 系统需要下载的文件过大,我们的后端的URLConnection设置了超时时间,导致数据还未读取完成连接就关闭了导致了超时(默认没设置就是永不超时,排除)。
niginx超时: 对于我们的系统来说,一般都会通过nginx代理访问,默认的话nginx的超时时间是60s。在我通过页面多次点击的情况下发现每次都是60s准时超时,就是说是因为后端代码异常导致的nginx的超时异常。
通过把请求的https的地址放置到上述的代码当中,编写一个单元测试会发现,会报错Exception in thread "main"
javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names
matching IP address xxx found。并不是超时异常,因为我们本地是没有https的证书,但是线上docker配置了ssl证书。所以又引出了
其他的问题。
解决方案:
通过在现场环境执行curl命令下载文件,看一下响应时间,排查一下问题
curl --request POST --header "content-type: application/json" --header "accept: application/octet-stream"
--header "Authorization: Bearer token" --header "Connection: keep-alive" --header "server-group: xxx"
--data "{\"id\": \"xxx\"}" -o test.jpg http://xxx/api/service/uploadFile/downloadById
将问价下载到本地执行路径下,并取名test.jpg。发现代码响应时间不超过5s钟,所以严重怀疑是证书的问题,但是因为问题排查到这里,每个环节都觉得自己没问题,所以博主只能使用一个取巧的方法来解决这个问题。当下载的文件为https协议的文件时忽略https验证,使用此方法也是迫于无奈。
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + URLUtil.encode(fileName.replace(",", ",")));
URL url = new URL(uploadFileEntity.getFilePath());
URLConnection connection = url.openConnection();
//如果是https请求,忽略证书校验。http请求则不做任何处理
if (connection instanceof HttpsURLConnection) {
HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
httpsConnection.setHostnameVerifier((hostname, session) -> true);
}
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
outputStream.close();
inputStream.close();