一个很神奇的bug(粗心所致),出现在通过http请求获取url的图片的流,进行下载。在开发环境和本地都可以,但是当发布在生产环境的时候就报错java.net.SocketTimeoutException: connect timed out。
后面经过排查,发现是由于该资源在服务器本地(开始以为图片信息在文件服务器),不需要调用http请求去获取资源,直接本地拿图片流就可以。
public static File UrltoFile(String sourceFilePath) throws Exception { File sourceFile = new File(sourceFilePath); if (!sourceFile.exists() || !sourceFile.isFile()) { throw new IOException("Source file not found or not a file: " + sourceFilePath); } // 创建临时文件 File tempFile = File.createTempFile("xie_temp_", ".jpg", null); // JPG图片 tempFile.deleteOnExit(); // JVM退出时自动删除临时文件 try (InputStream ins = new FileInputStream(sourceFile); OutputStream os = new FileOutputStream(tempFile)) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = ins.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } } return tempFile;