【问题标题】:Quickest way to get content type获取内容类型的最快方法
【发布时间】:2011-08-13 16:56:12
【问题描述】:

我需要检查用户插入的网址的内容类型(如果是图像、音频或视频)。我有这样的代码:

URL url = new URL(urlname);
URLConnection connection = url.openConnection();
connection.connect();
String contentType = connection.getContentType();

我正在获取内容类型,但问题是似乎有必要下载整个文件以检查其内容类型。因此,当文件很大时,它会持续很长时间。我需要在 Google App Engine 应用程序中使用它,因此请求限制为 30 秒。

有没有其他方法可以在不下载文件的情况下获取 url 的内容类型(这样可以更快地完成)?

【问题讨论】:

  • 只是一个想法:如何获取前 n 个字节然后关闭连接?在大多数情况下,应该可以通过文件的开头来猜测内容类型。但我在这里不是专业人士。
  • @pintxo 如果您可以读取标头参数,为什么要这样做:Content-Type 而不是使用GET 获取整个请求,您只需执行HEAD

标签: java url content-type


【解决方案1】:

感谢 DaveHowes 的回答并在谷歌上搜索如何获得 HEAD,我以这种方式得到了它:

URL url = new URL(urlname);
HttpURLConnection connection = (HttpURLConnection)  url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();

【讨论】:

    【解决方案2】:

    如果“另一端”支持,可以使用HEAD HTTP 方法吗?

    【讨论】:

    • 注意重定向,我的远程内容检查遇到了同样的问题。请参阅下面我检查过的代码。
    【解决方案3】:

    注意重定向,我在远程内容检查时遇到了同样的问题。
    这是我的解决方法:

    /**
     * Http HEAD Method to get URL content type
     *
     * @param urlString
     * @return content type
     * @throws IOException
     */
    public static String getContentType(String urlString) throws IOException{
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("HEAD");
        if (isRedirect(connection.getResponseCode())) {
            String newUrl = connection.getHeaderField("Location"); // get redirect url from "location" header field
            logger.warn("Original request URL: '{}' redirected to: '{}'", urlString, newUrl);
            return getContentType(newUrl);
        }
        String contentType = connection.getContentType();
        return contentType;
    }
    
    /**
     * Check status code for redirects
     * 
     * @param statusCode
     * @return true if matched redirect group
     */
    protected static boolean isRedirect(int statusCode) {
        if (statusCode != HttpURLConnection.HTTP_OK) {
            if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP
                || statusCode == HttpURLConnection.HTTP_MOVED_PERM
                    || statusCode == HttpURLConnection.HTTP_SEE_OTHER) {
                return true;
            }
        }
        return false;
    }
    

    您还可以为maxRedirectCount 设置一些计数器以避免无限重定向循环 - 但这不在此处介绍。这只是一个灵感。

    【讨论】:

    • 不错。为什么需要问:if (statusCode != HttpURLConnection.HTTP_OK) {
    • @Dejell 用于处理重定向
    • 您可以使用java.net.HttpURLConnection.setFollowRedirects(boolean) 来减少样板代码的大小。
    • setFollowRedirects 似乎默认为true docs.oracle.com/javase/7/docs/api/java/net/…
    【解决方案4】:

    我遇到了类似的任务,我需要检查 url 的内容类型,以及我管理它的方式是通过改造。首先,您必须定义一个端点以使用您要检查的 url 调用它:

    @GET
    suspend fun getContentType(@Url url: String): Response<Unit>
    

    然后你这样调用它来获取内容类型标题:

    api.getContentType(url).headers()["content-type"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      • 2013-07-07
      • 2018-06-27
      相关资源
      最近更新 更多