簡體   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