Mega Code Archive

 
Categories / Android / Network
 

Get File From HTTP

import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.util.Log; class Main {   public static File GetFileFromHTTP(String URL, String TempFilePrefix,       String TempFileSuffix) {     try {       URL url = new URL(URL);       URLConnection conn = url.openConnection();       conn.setConnectTimeout(10000);       conn.connect();       InputStream is = conn.getInputStream();       if (is != null) {         File f = File.createTempFile(TempFilePrefix, TempFileSuffix);         FileOutputStream fos = new FileOutputStream(f);         byte buff[] = new byte[128];         while (true) {           int readbyte = is.read(buff);           if (readbyte <= 0)             break;           fos.write(buff, 0, readbyte);         }         is.close();         return f;       } else         return null;     } catch (Exception e) {       if (e.getMessage() != null)         //Log.w(Common.LOGCAT_TAG, e.getMessage());         ;       else         e.printStackTrace();       return null;     }   } }