开发者

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 开发者_如何学Pythonme 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜