recuperar reanudar reanudación porque pausar otro las interrumpidas ingles habilitar google error eliminadas detienen descargas descarga continuar como chrome java android http

java - reanudación - reanudar descargas google chrome interrumpidas



cómo reanudar una descarga interrumpida (2)

Aquí hay un código de ejemplo que puedes usar:

import java.io.*; import java.net.*; public class HttpUrlDownload { public static void main(String[] args) { String strUrl = "http://VRSDLSCEN001:80//DLS//lib//clics.jar"; String DESTINATION_PATH = "clics.jar"; int count = 0; while (true) { count++; if (download(strUrl, DESTINATION_PATH) == true || count > 20) { break; } } } public static boolean download(String strUrl, String DESTINATION_PATH) { BufferedInputStream in = null; FileOutputStream fos = null; BufferedOutputStream bout = null; URLConnection connection = null; int downloaded = 0; try { System.out.println("mark ... download start"); URL url = new URL(strUrl); connection = url.openConnection(); File file=new File(DESTINATION_PATH); if(file.exists()){ downloaded = (int) file.length(); } if (downloaded == 0) { connection.connect(); } else { connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); connection.connect(); } try { in = new BufferedInputStream(connection.getInputStream()); } catch (IOException e) { int responseCode = 0; try { responseCode = ((HttpURLConnection)connection).getResponseCode(); } catch (IOException e1) { e1.printStackTrace(); } if (responseCode == 416) { return true; } else { e.printStackTrace(); return false; } } fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true); bout = new BufferedOutputStream(fos, 1024); byte[] data = new byte[1024]; int x = 0; while ((x = in.read(data, 0, 1024)) >= 0) { bout.write(data, 0, x); } in.close(); bout.flush(); bout.close(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (fos != null) { try { fos.close(); } catch (IOException e) { } } if (bout != null) { try { bout.close(); } catch (IOException e) { } } if (connection != null) { ((HttpURLConnection)connection).disconnect(); } } } }

Estoy tratando de descargar un archivo grande de mi Yahoo! servidor del sitio web que aparentemente está configurado (no yo) para desconectar las descargas si no se completan en 100 segundos. El archivo es lo suficientemente pequeño como para transferir normalmente con éxito. En las ocasiones en que la velocidad de datos es lenta y la descarga se desconecta, ¿hay alguna forma de reanudar la conexión URLC en el desplazamiento del archivo donde se produjo la desconexión? Aquí está el código:

// Setup connection. URL url = new URL(strUrl[0]); URLConnection cx = url.openConnection(); cx.connect(); // Setup streams and buffers. int lengthFile = cx.getContentLength(); InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream(strUrl[1]); byte data[] = new byte[1024]; // Download file. for (total=0; (count=input.read(data, 0, 1024)) != -1; total+=count) { publishProgress((int)(total*100/lengthFile)); output.write(data, 0, count); Log.d("AsyncDownloadFile", "bytes: " + total); } // Close streams. output.flush(); output.close(); input.close();


Intente usar un encabezado de solicitud "Rango":

// Open connection to URL. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Specify what portion of file to download. connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); // here "downloaded" is the data length already previously downloaded. // Connect to server. connection.connect();

Una vez hecho esto, puede seek en un punto determinado (justo antes de la duración de los datos de descarga, digamos X ) y comenzar a escribir los datos recién descargados allí. Asegúrese de usar el mismo valor X para el encabezado de rango.

Detalles sobre 14.35.2 Solicitudes de recuperación de rango

Más detalles y código fuente se pueden encontrar here