Downloading a file created in server using Java - java

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();

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.

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.

Related

How to save a PDF file from the PDF URL of Orbeon form with Java

I tried to save a PDF document from the PDF URL generated by Orbeon with Java, and always my InputStream is null. When I open this URL in the browser it work well but when I inspect the code I don't find the body of the PDF.
try {
URL urlPDF = new URL(urlPdfOrbeon);
URLConnection connection = urlPDF.openConnection();
InputStream in = connection.getInputStream();
System.out.println(IOUtils.toString(in)) ;
FileOutputStream fos = new FileOutputStream(newFile("yourFile.pdf"));
int length = -1;
byte[] buffer = new byte[1024];
while ((length = in.read(buffer)) > -1) {
fos.write(buffer, 0, length);
}
fos.close();
in.close();
}
The input stream is not null. But it doesn't have any byte to read anymore when you try writing its content to the file. And that's expected, since immediately before, you read everything in the stream by calling
System.out.println(IOUtils.toString(in));
EDIT:
If the input stream actually contains an HTML page asking you to log in, it's probably that you need to be logged in to access this PDF.

Downloading files using servlets

hi i have tried the following java codes which works fine if i use them as a java application but when i use the same code in my servlet page they dont work means i am not able to download the files. Please suggest what changes should i do so that i can download the file using Servlets.
a.
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL("http://169.254.174.150:8084/WebApplication1/files/check.txt").openStream());
File f1 = new File("D:\\a.txt");
java.io.FileOutputStream fos = new java.io.FileOutputStream(f1);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte data[] = new byte[1024];
while (in.read(data, 0, 1024) >= 0) {
bout.write(data);
}
bout.close();
in.close();
}
b. http://www.javabeat.net/examples/2012/04/13/download-file-from-http-https-server-using-java/
One of the older JavaBeat examples like the one you specified can be found here
I found other solutions too but this seems to be the most comprehensive.
Couple of things, insetad of writing it to a file try wrting the data directly to the responce. Before writing data you will have to set the following parameters to the responce
//byte[] filedata = ; intialize your file contents
String filename = "a.txt";
// set the header information in the response.
res.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
res.setContentType("application/x-unknown");
ByteArrayInputStream byteStream = new ByteArrayInputStream(filedata);
BufferedInputStream bufStream = new BufferedInputStream(byteStream);
ServletOutputStream responseOutputStream = res.getOutputStream();
int data = bufStream.read();
while (data != -1)
{
responseOutputStream.write(data);
data = bufStream.read();
}
bufStream.close();
responseOutputStream.close();
where res is a HttpServletResponse object. After this you can write data to responseOutputStream.

Programmatically downloading Oracle Business Intelligence report using java

I need to download an OBI report as csv using java. When I enter
http://<my_host>:<my_port>/analytics/saw.dll?Go&Action=Download&path=<my_path>&Format=csv
in a browser, download pop-up appears and I can download the file.
However, when i try to download the report using java, it downloads an html content saying
"Your browser is not supported by Oracle BI Presentation Services."
Here is piece of code I use:
URL url = new URL("http://<my_host>:<my_port>/analytics/saw.dll?Go&Action=Download&path=<my_path>&Format=csv");
URLConnection urlc = url.openConnection();
urlc.addRequestProperty("User-Agent", "Mozilla/4.0");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
bos = new BufferedOutputStream(new FileOutputStream("file.csv"), 1024);
bis = new BufferedInputStream(urlc.getInputStream());
byte[] data = new byte[1024];
while ((x = bis.read(data, 0, 1024)) >= 0) {
bos.write(data, 0, x);
}
So, how can I download the report?
Try using a complete UA string; the javascript that parses UA is sensitive to it.
As you discovered elsewhere, you hit further problems, with a login page with a javascript redirect to the actual page.

Download file in my webserver

I would like to implement a function where you send a URL of a photo and my server will automatically download and store it in a specified folder.
I studied some use cases, but as a beginner in this area of the web, I was a bit lost. I thought about FTP but is not exactly what I want.
Like that, function on my webservice (using Java + Tomcat + AXIS2)
void getPhoto(URL url){
//receive a photo and store at folder /photos
}
but, I don't know what use, I was looking for some httppost or httpget, should I still looking for in this way? Has a dummie sample, to show me the basic way?
I would like to implement a function where you send a URL of a photo and my server will automatically download and store it in a specified folder.
That's not exactly "uploading", but just "downloading".
Just call openStream() on the URL and you've an InputStream which you can do anything with. Writing to a FileOutputStream for example.
InputStream input = url.openStream();
// ...
hey use this code to download.
try {
URL url = new URL(url of file );
URLConnection conection = url.openConnection();
conection.connect();
InputStream input = new BufferedInputStream(url.openStream());
String downloadloc = "D:\"; // or anything
OutputStream output = new FileOutputStream(downloadloc
+ "\name of file.ext");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
You want to look at using an HttpURLConnection, call it's 'connect' and 'getInputStream' methods, continually reading from that stream and writing that data to a file with e.g. a FileOutputStream.
To download a file using a URL, as an alternative to what suggested by others, you can take a look to Apache Commons HttpClient.
There is also a well written tutorial.

Wanted to know how to ftp the file from remote server

Can anyone help me how to ftp the file from the remote server (to which I have successfully established the connection?) I heard I need to have ftpclient.jar for ftp_ing the file to the local system(Windows). Is it so? If so can anyone help me in getting the jar please?
You can see a tutorial and various available libraries on that link: http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp.html
You can use java.net API:
URL url =
new URL("ftp://user:pass#ftp.example.com/file.zip");
URLConnection connection = url.openConnection();
BufferedInputStream in =
new BufferedInputStream(connection.getInputStream());
FileOutputStream out =
new FileOutputStream("file.zip");
int read = 0;
byte[] data = new byte[1024];
while ((read = in.read(data)) >= 0) {
out.write(data, 0, read);
}
out.close();
in.close();

Categories

Resources