0

I am trying to download a file from a given URL. The URL is not a direct file URL. When this URL is provided in browser manually, we get a prompt for download/save.

For example, http://www.my-domain.com/download/type/salary/format/excel

I have no issues in the URL which has the file name directly in the URL. In the above URL, based on the format and type, server generates the file.

In Java I am trying to download the file using the below code. The file is created, but the content is just the domain content and not the actual excel data.

URL url = new URL("http://www.my-domain.com/download/type/salary/format/excel");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

float totalDataRead = 0;
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
FileOutputStream fos = new FileOutputStream("c:\\test.xls");
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);

byte[] data = new byte[1024];
int i = 0;

while ((i = in.read(data, 0, 1024)) >= 0) {
    totalDataRead = totalDataRead + i;
    bout.write(data, 0, i);
}

bout.close();
in.close();
Purus
  • 5,701
  • 9
  • 50
  • 89
  • Maybe [this article](http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/) will help. If not and you are redirected by some JavaScript code then you probably will have to use something which can interpret JavaScript like WebDriver (something like Selenium). – Pshemo Apr 24 '14 at 07:15
  • I am using this code in Selenium to download the file. – Purus Apr 24 '14 at 07:19
  • Honestly I never used Selenium (just know it is general solution if you need browser-like tool with JS interpreter) but our code doesn't seem to use Selenium. Can you provide more context? Also link to page you have problem with could be helpful with reproducing your problem and creating answer. – Pshemo Apr 24 '14 at 07:36
  • Eventually, a direct URL is presented to your browser for the download. Is there any way to get this direct URL instead of the redirecter? – Davio Apr 24 '14 at 07:43
  • Unfortunately, this is the only URL we can get. We are trying to automate the file download process in selenium. As it can't handle download windows, I am trying to get the file from Java. BTW its an internal site. – Purus Apr 24 '14 at 07:46

2 Answers2

0

The content is whatever the server sent for that URL. You can't do anything about that from the client end. If it contained Javascript for example it won't get executed.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

When you want to solve a problem you have to use the adequate tools to get the thing done. The adequate tools can be found at poi.apache.org.
Have a look at Apache POI.

Rudy Vissers
  • 5,267
  • 4
  • 35
  • 37
  • And which classes from this tool would be adequate here? Could you give some code example? – Pshemo Apr 24 '14 at 07:40
  • How this is related to file download when POI is for creating/writing files. – Purus Apr 24 '14 at 07:47
  • Please have a look : http://stackoverflow.com/questions/16357812/how-to-read-excel-xlsx-in-java-using-poi – Rudy Vissers Apr 24 '14 at 07:52
  • Thanks for link. But you have understood the question wrongly. The question is related to handling file download which can either be a xls, csv or pdf or anything else. – Purus Apr 24 '14 at 07:58