java.net.SocketTimeoutException: connect timed out

一个很神奇的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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值