判断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("连接失败");
        }
    }
}
相关推荐
李庆政3701 天前
OkHttp的基本使用 实现GET/POST请求 authenticator自动认证 Cookie管理 请求头设置
java·网络协议·http·okhttp·ssl
IT WorryFree1 天前
LLD 自动发现场景 → 对应使用哪种探测方式(SNMP/HTTP/Agent)最优
网络·网络协议·http
dling81 天前
nginx如何配置https请求访问
nginx·http·https
房开民1 天前
http服务端 mongoose常用api
网络协议·http·xcode
好好学习,天天向上~1 天前
一套从 HTTP 抓取到动态页面爬取的 Python 全栈爬虫框架(附安装与实战)
爬虫·python·http
大连好光景2 天前
接口测试之Postman+Jmeter
jmeter·http·postman
米丘2 天前
从 HTTP 到 WebSocket:深入 Vite HMR 的网络层原理
http·node.js·vite
A.A呐2 天前
【Linux第二十一章】http
linux·运维·http
白毛大侠2 天前
WebSocket 核心:借 HTTP 建联,做自己的通信
websocket·网络协议·http
難釋懷2 天前
OpenResty封装http工具
http·junit·openresty