First, the Knowledge base
Tomcat Server Configuration
Understanding the HTTP protocol
Understand Javaio operation knowledge
SDcard Operation Knowledge
Android Permissions Configuration
Ii. Steps of implementation
1. Access to resources from the Internet
1 Publicstring Download (string urlstr) {2StringBuffer SB =NewStringBuffer ();3String line =NULL;4BufferedReader buffer =NULL;5 Try {6 //Create a URL object7URL =NewURL (URLSTR);8 //Create an HTTP connection9HttpURLConnection Urlconn =(httpurlconnection) URLTen . OpenConnection (); One //reading data using IO stream ABuffer =NewBufferedReader (NewInputStreamReader (Urlconn - . getInputStream ())); - while(line = Buffer.readline ())! =NULL) { the Sb.append (line); - } -}Catch(Exception e) { - e.printstacktrace (); +}finally { - Try { + buffer.close (); A}Catch(Exception e) { at e.printstacktrace (); - } - } - returnsb.tostring (); - } - in /** - * This function returns 1: Error on behalf of download file 0: Download file on behalf of Success 1: The file already exists to */ + Public intdownfile (String urlstr, String path, String fileName) { -InputStream InputStream =NULL; the Try { *FileUtils FileUtils =NewFileUtils (); $ Panax Notoginseng if(Fileutils.isfileexist (Path +fileName)) { - return1; the}Else { +InputStream =Getinputstreamfromurl (URLSTR); AFile Resultfile =fileutils.write2sdfrominput (Path,filename, inputstream); the if(Resultfile = =NULL) { + return-1; - } $ } $}Catch(Exception e) { - e.printstacktrace (); - return-1; the}finally { - Try {Wuyi inputstream.close (); the}Catch(Exception e) { - e.printstacktrace (); Wu } - } About return0; $ } - - /** - * Get input stream based on URL A * + * @paramUrlstr the * @return - * @throwsmalformedurlexception $ * @throwsIOException the */ the PublicInputStream Getinputstreamfromurl (String urlstr) the throwsmalformedurlexception, IOException { theURL =NewURL (URLSTR); -HttpURLConnection Urlconn =(HttpURLConnection) url.openconnection (); inInputStream InputStream =Urlconn.getinputstream (); the returnInputStream; the } About}download files from the network
2. Write resources to SDcard
Public classFileUtils {PrivateString Sdpath; PublicString Getsdpath () {returnSdpath; } PublicFileUtils () {//get the directory of the current external storage device///sdcardSdpath = environment.getexternalstoragedirectory () + "/"; } /*** Create files on SD card * *@throwsIOException*/ PublicFile creatsdfile (String fileName)throwsIOException {File file=NewFile (Sdpath +fileName); File.createnewfile (); returnfile; } /*** Create directory on SD card * *@paramDirName*/ Publicfile Creatsddir (String dirName) {file dir=NewFile (Sdpath +dirName); Dir.mkdirs (); returndir; } /*** Determine if the folder on the SD card exists*/ Public Booleanisfileexist (String fileName) {File file=NewFile (Sdpath +fileName); returnfile.exists (); } /*** Write data from a inputstream into the SD card*/ Publicfile Write2sdfrominput (String path,string filename,inputstream input) {file file=NULL; OutputStream Output=NULL; Try{creatsddir (path); File= Creatsdfile (path +fileName); Output=NewFileOutputStream (file);//Write Data byteBuffer [] =New byte[4 * 1024]; while((input.read (buffer))! =-1) {output.write (buffer); } output.flush (); } Catch(Exception e) {e.printstacktrace (); } finally{ Try{output.close (); } Catch(Exception e) {e.printstacktrace (); } } returnfile; }}SDcard Read and write operations
Third, Release Notes
Program source source Mars teacher, express thanks
Android Network file download module finishing