繁体   English   中英

文件未从某些HTTP URL下载

[英]File not downloaded from some HTTP URLs

我正在使用url下载此jar但它下载0kb文件。 我使用其他网址甚至ftp网址工作得很好,问题只有这个文件。

网址不起作用

http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar

使用下面的代码。

try 
{
    bis = new BufferedInputStream(url.openStream());
    String fileName = DownloadSourceUtils.getUniqueFileName(DownloadSourceConstants.DOWNLOAD_LOCALTION, url.getFile());

    File directory = new File(DownloadSourceConstants.DOWNLOAD_LOCALTION);
    if (! directory.exists()){
        directory.mkdir();
        // If you require it to make the entire directory path including parents,
        // use directory.mkdirs(); here instead.
    }

    fos = new FileOutputStream(DownloadSourceConstants.DOWNLOAD_LOCALTION+File.separator+fileName);

    byte dataBuffer[] = new byte[1024];
    int bytesRead;
    while ((bytesRead = bis.read(dataBuffer, 0, 1024)) != -1) 
    {
        fos.write(dataBuffer, 0, bytesRead);
    }
    fos.flush();
    output = url + " downloaded successfully";
    return output;
} 
catch (IOException e)
{
    output = e.getMessage();
    return output;
}
finally
{
    if(bis != null)
        bis.close();
    if(fos != null)
        fos.close();
}

我认为这里的问题是URL重定向到另一个,即HTTPS:

$ curl -i http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar
HTTP/1.1 301 Moved Permanently
Location: https://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar
Content-Length: 0
Date: Fri, 16 Nov 2018 14:37:09 GMT
Server: lighttpd/1.4.45
Connection: keep-alive

因此,我将您的URL更新为https://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar请参阅URLConnection不遵循重定向,以便围绕Java无法正常工作的原因进行讨论。

该请求被重定向到HTTPS 因此没有给出任何内容。

如果您将链接更改为HTTPS ,一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM