判断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("连接失败");
        }
    }
}
相关推荐
哑巴语天雨3 小时前
前端面试-网络协议篇
websocket·网络协议·http·面试·https
小梁不秃捏6 小时前
HTTP 常见状态码技术解析(应用层)
网络·网络协议·计算机网络·http
卑微的小鬼8 小时前
rpc和http的区别,为啥golang使用grpc 不使用http?
http·rpc·golang
fajianchen8 小时前
什么是HTTP/2协议?NGINX如何支持HTTP/2并提升网站性能?
nginx·http
元气满满的热码式9 小时前
logstash中的input插件(http插件,graphite插件)
网络·网络协议·http·elasticsearch·云原生
IsToRestart1 天前
什么是RPC,和HTTP有什么区别?
网络协议·http·rpc
不修×蝙蝠1 天前
HTTP 协议(Ⅲ)
服务器·http·javaee·http协议
铁锅与大鹅1 天前
http+nginx
网络协议·nginx·http
s_fox_2 天前
nginx ngx_http_module(7) 指令详解
运维·nginx·http
荔枝荷包蛋6662 天前
【Linux】HTTP:Cookie 和 Session 详解
网络·网络协议·http