Home > other >  Download the android TXT problem! O solve!!!!!!
Download the android TXT problem! O solve!!!!!!

Time:11-04

FileUtils. Java code:

Public class FileUtils {
Private String SDPATH;

Public String getSDPATH () {
Return SDPATH;
}

Public FileUtils () {
//get the current external storage directory
///SDCARD
SDPATH=Environment. External.getexternalstoragedirectory () + "/";
}

/* *
* create a file on SD card
*
* @ throws IOException
*/
The public File creatSDFile (String fileName) throws IOException {
The File File=new File (SDPATH + fileName);
File. CreateNewFile ();
Return the file;
}

/* *
* create a directory in SD card
*
* @ param dirName
*/
The public File creatSDDir (String dirName) {
The File dir=new File (SDPATH + dirName);
dir.mkdirs();
Return the dir.
}

/* *
* whether the folder on your SD card is
*/
Public Boolean isFileExist (String fileName) {
The File File=new File (SDPATH + fileName);
Return the file. The exists ();
}

/* *
* will an InputStream data written to the SD card in the
*/
Public File write2SDFromInput (String path, String fileName,
InputStream input) {
The File File=null;
OutputStream output=null;
Try {
CreatSDDir (path);
The file=creatSDFile (path and fileName);
The output=new FileOutputStream (file);
Byte buffer []=new byte (4 * 1024);
While ((input. Read (buffer))!=1) {
The output. The write (buffer);
}
The output. The flush ();
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Try {
The output. The close ();
} the catch (Exception e) {
e.printStackTrace();
}
}
Return the file;
}

}


HttpDownloader. Java code:

Public class HttpDownloader {
Private URL URL=null;

/* *
* based on the URL to download file, if the contents of the file is a text, the return value of a function is the content of the file 1. Create a URL object
* 2. Via the URL object, create an HttpURLConnection object 3. Get InputStram 4. Read the data from the InputStream
*
* @ param urlStr
* @ return
*/
Public String download (String urlStr) {
StringBuffer sb=new StringBuffer();
The String line=null;
BufferedReader buffer=null;
Try {
//create a URL object
System. The out. Println (" perform ");
Url=new url (urlStr);
//create a Http connection
System. The out. Println (" perform 1 ");
HttpURLConnection urlConn url=(HttpURLConnection)
The openConnection ();
//use the IO stream to read data
System. Out. Println (" 2 ");
Buffer=new BufferedReader (new InputStreamReader (//null pointer
UrlConn. GetInputStream ()));
System. Out. Println (" 3 ");
While ((line=buffer. ReadLine ())!=null) {
System. Out. Println (" execution 4 ");
Sb. Append (line);
}

} the catch (Exception e) {
System. The out. Println (" download: "+ um participant etMessage ());
e.printStackTrace();
} the finally {
Try {
Buffer. The close ();
} the catch (Exception e) {
System. The out. Println (" download buffer. The close () : "+ um participant etMessage ());
e.printStackTrace();
}
}
Return sb. ToString ();
}

/* *
* this function returns the plastic - 1: on behalf of the download file error 0: for download file 1: success represents the file already exists
*/
Public int downFile (String urlStr, String path, String fileName) {
InputStream InputStream=null;
Try {
FileUtils FileUtils=new FileUtils ();

If (fileUtils isFileExist (path and fileName)) {
return 1;
} else {
InputStream=getInputStreamFromUrl (urlStr);
The File resultFile=fileUtils. Write2SDFromInput (path and fileName,
InputStream);
If (resultFile==null) {
return -1;
}
}
} the catch (Exception e) {
System. The out. Println (" downFile: "+ um participant etMessage ());
e.printStackTrace();
return -1;
} the finally {
Try {
inputStream.close();
} the catch (Exception e) {
System. The out. Println (" downFile close: "+ um participant etMessage ());
e.printStackTrace();
}
}
return 0;
}

/* *
* based on the URL to get the input stream
*
* @ param urlStr
* @ return
* @ throws MalformedURLException
* @ throws IOException
*/
Public InputStream getInputStreamFromUrl (String urlStr)
Throws MalformedURLException, IOException {
Url=new url (urlStr);
HttpURLConnection urlConn=(HttpURLConnection) url. OpenConnection ();
InputStream InputStream=urlConn. GetInputStream ();
Return an inputStream.
}
}


MainActivity. Java code:

Public class MainActivity extends the Activity {
Private Button downloadTxtButton;
Private Button downloadMp3Button;

@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
setContentView(R.layout.activity_main);

DownloadTxtButton=(Button) the findViewById (R.i d.d ownloadTxtButton);
DownloadMp3Button=(Button) the findViewById (R.i d.d ownloadMp3Button);
DownloadTxtButton. SetOnClickListener (new DownloadTxtListener ());
DownloadMp3Button. SetOnClickListener (new DownloadMp3Listener ());

/* if (savedInstanceState==null) {
GetSupportFragmentManager (). The beginTransaction ()
. The add (R.i, dc ontainer, new PlaceholderFragment ()), commit ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related