[英]File not downloaded from some HTTP URLs
I am using url to download a this jar but it downloads 0kb file. 我正在使用url下载此jar但它下载0kb文件。 I using other url even ftp url is working just fine, the issue is only with this file. 我使用其他网址甚至ftp网址工作得很好,问题只有这个文件。
url which is not working 网址不起作用
http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar
Using below code. 使用下面的代码。
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();
}
I think the problem here is that URL redirects to another, which is HTTPS: 我认为这里的问题是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
So I'd update your URL to https://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar See URLConnection Doesn't Follow Redirect for good discussions around the Java reasons for this not working. 因此,我将您的URL更新为https://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar请参阅URLConnection不遵循重定向,以便围绕Java无法正常工作的原因进行讨论。
The request is redirected to HTTPS
. 该请求被重定向到HTTPS
。 And therefore no content is given. 因此没有给出任何内容。
If you change the link to HTTPS
, everything works. 如果您将链接更改为HTTPS
,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.