cleliodpaula
cleliodpaula

Reputation: 827

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?

Upvotes: 1

Views: 1199

Answers (4)

Prakhar
Prakhar

Reputation: 2310

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) {

            }

Upvotes: 2

stivlo
stivlo

Reputation: 85476

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.

Upvotes: 0

sje397
sje397

Reputation: 41812

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.

Upvotes: 0

BalusC
BalusC

Reputation: 1108632

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

Upvotes: 2

Related Questions