Reputation: 637
I am using url to download a this jar but it downloads 0kb file. I using other url even ftp url is working just fine, the issue is only with this file.
url which is not working
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();
}
Upvotes: 1
Views: 93
Reputation: 4185
I think the problem here is that URL redirects to another, which is 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.
Upvotes: 3
Reputation: 6732
The request is redirected to HTTPS
. And therefore no content is given.
If you change the link to HTTPS
, everything works.
Upvotes: 0