Android examples for Network:File Upload
upload File Range
import java.io.DataOutputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URI; import android.util.Log; public class Main { private static final String TAG = ""; public static boolean uploadFileRange(URI uri, java.io.File targetFile, long start, long end) { boolean result = false; HttpURLConnection conn = null; DataOutputStream os = null;/*from ww w. ja va2s .co m*/ try { final int bufferSize = 1024 * 64; byte[] buffer = new byte[bufferSize]; long totalBytesToRead = end - start, totalBytesRead = 0; RandomAccessFile raf = new RandomAccessFile(targetFile, "r"); int readSize; conn = (HttpURLConnection) uri.toURL().openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); // Use a post method. conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/mixed"); conn.setRequestProperty("Range", start + "-" + end); os = new DataOutputStream(conn.getOutputStream()); try { raf.seek(start); do { readSize = raf.read(buffer, 0, bufferSize); if (readSize > 0) { os.write(buffer, 0, readSize); } totalBytesRead += readSize; if (totalBytesRead >= totalBytesToRead) { break; } } while (readSize > 0); } catch (Exception e) { result = false; Log.e(TAG, e.getMessage(), e); } finally { raf.close(); } os.flush(); os.close(); } catch (Exception e) { result = false; Log.e(TAG, e.getMessage(), e); } finally { try { if (conn.getResponseCode() / 100 == 2) { result = true; } } catch (Exception e) { Log.e(TAG, e.getMessage(), e); result = false; } conn.disconnect(); } return result; } }