Java Remote File Download

Source: Internet
Author: User

From: http://hi.baidu.com/kh994354738/item/0795dae9eae94fc4bbf37d5f

Package COM. pickedpoint. sois. test. action; import Java. io. *; import java.net. *; public class testurl {/*** download Remote File * @ Param photourl file path * @ Param filename download to local path * @ return success */Public Boolean saveurlas (string photourl, string filename) {try {URL url = new URL (photourl); httpurlconnection connection = (httpurlconnection) URL. openconnection (); datainputstream in = new datainputstream (connection. Getinputstream (); dataoutputstream out = new dataoutputstream (New fileoutputstream (filename); byte [] buffer = new byte [4096]; int COUNT = 0; while (COUNT = in. read (buffer)> 0) {out. write (buffer, 0, count);} Out. close (); In. close (); Return true;} catch (exception e) {system. out. println (E); Return false ;}/ *** obtain the remote file content (compatible with HTTP and FTP) * @ Param urlstring file path * @ return file content */Public String Getdocumentat (string urlstring) {stringbuffer document = new stringbuffer (); try {URL url = new URL (urlstring); urlconnection conn = URL. openconnection (); bufferedreader reader = new bufferedreader (New inputstreamreader (Conn. getinputstream (), "UTF-8"); string line = NULL; while (line = reader. readline ())! = NULL) {document. append (LINE + "\ n");} reader. close ();} catch (malformedurlexception e) {system. out. println ("unable to connect to URL:" + urlstring);} catch (ioexception e) {system. out. println ("ioexception when connecting to URL:" + urlstring);} return document. tostring ();}/***** @ Param ARGs */public static void main (string [] ARGs) {testurl test = new testurl (); string photourl = "HT TP: // service.pickedpoint.com/pointpublisher/gx_service.xml "; string filename = photourl. substring (photourl. lastindexof ("/"); string filepath = "/home/developer/soft/Apache-Tomcat-6.0.32/webapps/soisweb2/isystems/pointpublisher/resource"; system. out. println (test. getdocumentat (photourl); // Boolean flag = test. saveurlas (photourl, filepath + filename); // system. out. println ("Run OK! \ N get URL File "+ flag );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.