判断http链接中文件是否存在

最近项目遇到需要从http请求下载文件到服务器,下载前需要判断下http中的文件是否存在。如果判断本地服务器上文件是否存在,用file.exists来判断。但是这个方法却无法判断http中文件是否存在。

如果要判断http文件是否存在,用如下代码:

java 复制代码
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class Main {
    public static void main(String[] args) {
        String urlString = "http://example.com/file.txt";
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("文件存在");
            } else {
                System.out.println("文件不存在");
            }
        } catch (IOException e) {
            System.out.println("连接失败");
        }
    }
}

如果对方系统需要身份验证,那么需要加如下代码。我们系统是需要token验证。加权限验证的代码如下:

java 复制代码
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class Main {
    public static void main(String[] args) {
        String urlString = "http://example.com/file.txt";
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            
            //权限校验
            connection.setRequestProperty("X-JFrog-Art-Api","cmv125VmedaeDAFdafLFAF2ed");

            //HEAD请求,不返回响应体,但是有些服务器可能不支持,则改成GET请求
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("文件存在");
            } else {
                System.out.println("文件不存在");
            }
        } catch (IOException e) {
            System.out.println("连接失败");
        }
    }
}

判断http中文件是否存在可以利用java.nio的方法,代码如下:

java 复制代码
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class Main {
    public static void main(String[] args) {
        String urlString = "http://example.com/file.txt";
        try {
            URL url = new URL(urlString);
            Path path = Paths.get(url.toURI());
            if (Files.exists(path)) {
                System.out.println("文件存在");
            } else {
                System.out.println("文件不存在");
            }
        } catch (IOException e) {
            System.out.println("连接失败");
        }
    }
}
相关推荐
zqy02274 小时前
HTTP的Web服务测试在Python中的实现
python·网络协议·http
大筒木老辈子14 小时前
Linux笔记---HTTP协议
笔记·网络协议·http
乖女子@@@1 天前
协议_https协议
http·https·iphone
吾日三省吾码1 天前
用 Python UTCP 直调 HTTP、CLI、MCP……
开发语言·python·http
百渡ovO1 天前
HTTP快速入门
网络·网络协议·http
NeverSettle_2 天前
2025前端网络相关知识深度解析
前端·javascript·http
high20112 天前
【 运维相关】-- HTTP 压测/负载发生器之新秀 oha
运维·网络协议·http
AD钙奶-lalala2 天前
HTTP response code 200 206 416详解
网络·网络协议·http
sun03222 天前
使用 javax.net.ssl.HttpsURLConnection 发送 HTTP 请求_以及为了JWT通信选用OSS的Jar的【坑】
http·.net·ssl
MC皮蛋侠客2 天前
使用python test测试http接口
开发语言·python·http