Article View: es.comp.lenguajes.java
Article #1003problemas al descargar fichero con URLConnection
From: korkuts
Date: Sat, 16 Sep 2006 14:12
Date: Sat, 16 Sep 2006 14:12
84 lines
3983 bytes
3983 bytes
buenas.
Tengo un problemillas, estoy probando java y estoy haciendo un simple
programita que se baja un archivo binario desde un servidor web, el
problema que es que lo baja corrupto, vamos que no entiendo lo baja con
bytes de mรกs. Y no se el motivo, os pongo el codigo a ver si me podeis
echar una mano:
import java.io.*;
import java.net.*;
import java.lang.*;
public class DescargarUrl{
URL url;
URLConnection servidor;
InputStream conexion;
BufferedInputStream archivo_remoto;
FileOutputStream archivo_local;
BufferedOutputStream escritor;
int descargado;
int peso;
DescargarUrl(String url_archivo){
try{
url = new URL(url_archivo);
servidor = url.openConnection();
conexion = servidor.getInputStream();
peso = servidor.getContentLength();
archivo_remoto = new
BufferedInputStream(conexion);
System.out.println("Iniciando descarga de " + peso + " bytes");
}catch(Exception e){
}
}
public void inicioDescarga(String destino){
try{
archivo_local= new FileOutputStream(destino);
escritor = new
BufferedOutputStream(archivo_local);
}catch(IOException e){
}
}
public boolean descargarBytes()
{
int bytes_disponibles;
try{
byte[] datos = new byte[1024];
if((bytes_disponibles =
archivo_remoto.read(datos)) == -1)
{
escritor.write(datos);
System.out.println("Fin de flujo");
escritor.close();
return false;
}
else
{ descargado +=
bytes_disponibles;
escritor.write(datos);
return true;
}
}
catch(IOException e)
{
}
return true;
}
public static void main(String args[])
{
DescargarUrl mul = new
DescargarUrl("http://www.crilandor.com/archivos/hues.mul");
mul.inicioDescarga("hues.mul");
while(mul.descargarBytes())
{
System.out.println(mul.descargado + "bytes");
}
}
}
Technical Details
Message-ID:
<1158415925.195742.170680@m7g2000cwm.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!m7g2000cwm.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 1116f3,d4aabcf80516c543,start X-Google-Attributes: gid1116f3,public X-Google-Language: SPANISH,ASCII Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!m7g2000cwm.googlegroups.com!not-for-mail From: "korkuts" <ricardo_fermon@yahoo.es> Newsgroups: es.comp.lenguajes.java Subject: problemas al descargar fichero con URLConnection Date: 16 Sep 2006 07:12:05 -0700 Organization: http://groups.google.com Lines: 85 Message-ID: <1158415925.195742.170680@m7g2000cwm.googlegroups.com> NNTP-Posting-Host: 83.38.16.164 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1158415929 2377 127.0.0.1 (16 Sep 2006 14:12:09 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 16 Sep 2006 14:12:09 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: m7g2000cwm.googlegroups.com; posting-host=83.38.16.164; posting-account=4Ky5KA0AAABWlRU1fNC_bCZAATiEe-3i