JAVA:如何从输入流中获取文件Mime类型

问题描述 投票:0回答:1

今天,我的团队让我在工作中实现了一个有趣的功能,该功能是从输入流中获取文件类型。以下是对我有用的解决方案。在此处与可能需要它的人共享。

java file stream mime
1个回答
0
投票
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MimeFileType {
    public static void main(String args[]) {
        try {
            URL url = new URL (“https://www.url.com/pdf");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(“GET”);
            connection.setDoOutput(true);
            InputStream content = (InputStream)connection.getInputStream();
            connection.getHeaderField(“Content-Type”);

            System.out.println(“Content-Type “+ connection.getHeaderField(“Content-Type”));
            BufferedReader in = new BufferedReader (new InputStreamReader(content));

            } catch (Exception e) {
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.