判断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("连接失败");
        }
    }
}
相关推荐
不可能的是1 天前
前端 SSE 流式请求三种实现方案全解析
前端·http
古译汉书5 天前
【IoT死磕系列】Day 7:只传8字节怎么控机械臂?学习工业控制 CANopen 的“对象字典”(附企业级源码)
数据结构·stm32·物联网·http
21号 15 天前
Http粘包问题回顾
网络·网络协议·http
A懿轩A5 天前
【SpringBoot 快速开发】面向后端开发的 HTTP 协议详解:请求报文、响应码与常见设计规范
spring boot·http·设计规范
吧啦蹦吧5 天前
http-SNI
网络·网络协议·http
~kiss~6 天前
HTTP 429
网络·网络协议·http
Olive6 天前
深入理解 HTTP 请求重试:不只是指数退避那么简单
http
AI-小柒6 天前
Seedance 2.0(即梦 2.0)深度解析:AI 视频进入「导演级」可控时代
大数据·人工智能·网络协议·tcp/ip·http·音视频
弹简特6 天前
【JavaEE10-后端部分】SpringMVC05-综合案例1-从加法计算器看前后端交互:接口文档与HTTP通信详解
java·spring boot·spring·http
弹简特6 天前
【JavaEE12-后端部分】SpringMVC07-综合案例3-从留言板看前后端交互:接口文档与HTTP通信详解
spring boot·网络协议·spring·http·java-ee·交互