• 0

File Downloader in Java


Question

How would I go about making a Java program that, given a URL address of a file, will download that file and save it to a specified location? I have no idea what objects I would use or anything for that, so I can't look anything in the java docs

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Using standard Java I/O (i.e. not NIO) you could do something along these lines (error-checking left up to you):

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;


/**
 * Sample code to read a file from a remote URL.
 */
public class FileGrabber
{
 ? ?/** The size of the buffer. */
 ? ?private static final int BUFFER_SIZE = 1024;


 ? ?/**
 ? ? * Read the remote file and save it to disk.
 ? ? *
 ? ? * @param ? link ? ? ? ?The URL of the file to be downloaded.
 ? ? * @param ? filename ? ?The name to be given to the file once it has been downloaded.
 ? ? *
 ? ? * @throws ?IOException
 ? ? */
 ? ?public void readRemoteFile(String link, String filename)
 ? ? ? ?throws IOException
 ? ?{
 ? ? ? ?URL url = new URL(link);
 ? ? ? ?url.getFile();

 ? ? ? ?byte[] buffer = new byte[BUFFER_SIZE];

 ? ? ? ?InputStream in = new BufferedInputStream(url.openStream());
 ? ? ? ?OutputStream out = new FileOutputStream(filename);

 ? ? ? ?try
 ? ? ? ?{
 ? ? ? ? ? ?int count = 0;

 ? ? ? ? ? ?while ((count = in.read(buffer, 0, buffer.length)) >= 0)
 ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?out.write(buffer, 0, count);
 ? ? ? ? ? ? ? ?out.flush();
 ? ? ? ? ? ?}
 ? ? ? ?}
 ? ? ? ?finally
 ? ? ? ?{
 ? ? ? ? ? ?if (in != null)
 ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?try
 ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ?in.close();
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?catch (IOException ioe)
 ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ?ioe.printStackTrace();
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ?}

 ? ? ? ? ? ?if (out != null)
 ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ?try
 ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ?out.close();
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ? ? ?catch (IOException ioe)
 ? ? ? ? ? ? ? ?{
 ? ? ? ? ? ? ? ? ? ?ioe.printStackTrace();
 ? ? ? ? ? ? ? ?}
 ? ? ? ? ? ?}
 ? ? ? ?}
 ? ?}
}

Edited by Kestrel
Link to comment
Share on other sites

  • 0

Hello, I'm very frustrated with using this downloader.

I converted it to a JSP and hopefully it'll work the same way as it is with a JAVA class. Here is what I had in the JSP:

String outputPath = (request.getParameter("outputPath")!=null?request.getParameter("outputPath"):"");

String outputFile = (request.getParameter("outputFile")!=null?request.getParameter("outputFile"):"");

File filei = new File(outputPath);

File fileo = new File(outputFile);

System.out.println("File : " + filei);

response.reset();

response.setContentType("application/zip");

response.setDateHeader("Expires", 0L);

response.setHeader("Content-Disposition", "fileo=" + fileo);

//response.setContentLength((int)filei.length());

/**

* Sample code to read a file from a remote URL.

*/

/**

* Read the remote file and save it to disk.

*

* @param link The URL of the file to be downloaded.

* @param fileo The name to be given to the file once it has been downloaded.

*

* @throws IOException

*/

//URL url = filei.toURL();

URL url = new URL(outputPath);

filei = new File(url.getFile());

System.out.println("File URL: " + url);

byte[] buffer = new byte[(int)filei.length()];

InputStream inStream = new BufferedInputStream(url.openStream());

OutputStream outStream = new FileOutputStream(fileo);

try

{

int count = 0;

while ((count = inStream.read(buffer, 0, buffer.length)) >= 0)

{

outStream.write(buffer, 0, count);

outStream.flush();

}

}

finally

{

if (inStream != null)

{

try

{

inStream.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

if (outStream != null)

{

try

{

outStream.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

}

Which simply take in the outputpath and filename from the previous page.

Yet I got the following when trying to execute it on the network server:

Servlet failed with IOException

java.net.MalformedURLException: no protocol: /web/domains/xxx/applications/xx2/xx_files/arOutput/20040218/AV/20040218(AV).xls

Anyone could give me a hand in here??

:wacko:

Link to comment
Share on other sites

  • 0

It says no protocol and it really is lacking the http or ftp protocol in the file name. At least its seems that way...

Link to comment
Share on other sites

  • 0

Hi!

I wrote a program in Java which downloads a URL file. I also want to add resuming functionality to it(Like GetRight) which makes it possible to start downloading from for ex. 1000000. byte.

I tried to use InputStream class' skip(long n) function. But It first downloads the file from beginning then skips the bytes.

Is there a solution to this problem??

Thanks.

Link to comment
Share on other sites

  • 0

Try this:

final byte resumeFrom = 2004; //which bytes to start resuming from.

URL url = new URL("...");
URLConnection urlc = url.openConnection();
urlc.setRequestProperty("Range", "bytes=" + resumeFrom + "-");
urlc.connect();
InputStream in = new BufferedInputStream(urlc.getInputStream());

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.