A cópia do código é a seguinte:
pacote com.hongyuan.test;
importar java.io.file;
importar java.io.fileOutputStream;
importar java.io.ioException;
importar java.io.inputStream;
importar java.io.OutputStream;
importar java.net.httpurlConnection;
importar java.net.MalformEdUrlexception;
importar java.net.url;
importar java.net.urldecoder;
classe pública FileDownLoad {
public static final string local_path = "d:/";
public static void main (string [] args) {
// Endereço do arquivo a ser baixado
Corda fileurl = "http://211.162.55.16/cdn.baidupcs.com/file/6a73dab7c03023a9835cc73 1826-250528-130564002 & TIME = 1397293108 & SIGN = FDTAXER-DCB740CCC5511E5E8FEDCFF06B08120 3-mQkOeRDtXpyQWwYJkI%2BxKJpxn5k%3D&to=cb&fm=N,B,U,nc&newver=1&expires=1397297563&rt=sh&r=854979042&logid=1751715936&sh=1&vuk=3255571826&fn=Web%E7%A8%8B%E5%BA%8F%E8%AE% Ser%e8%ae%a1%20%E7%AC%AC5%E7%89%88.pdf & wshc_tag = 0 & wsipHost = ipdbm ";
InputStream in = null;
OutputStream out = null;
HttpurlConnection Conn = null;
String filename = null;
tentar {
// Inicialize a conexão
Url url = novo url (fileurl);
conn = (httpurlConnection) url.openconnection ();
Conn.SetDoinput (true);
Conn.SetDoOutput (true);
// Obtenha o nome do arquivo
Disposição de String = Conn.GEtheaderField ("Content-Disposition");
if (disposição! = null &&! "". Equals (disposição)) {
// Obtenha o nome do arquivo desde o início
nome do arquivo = disposição.split (";") [1] .split ("=") [1] .Replaceall ("/" "," ");
}outro{
// Obtenha o nome do arquivo do endereço
FILENAME = FILEURL.SUBSTRING (FILEURL.LASTIndexOF ("/")+1);
}
if (nome do arquivo! = null &&! "". igual (nome do arquivo)) {
// decodificar o nome do arquivo
nome do arquivo = urldecoder.decode (nome do arquivo, "utf-8");
}outro{
// Se o nome do arquivo não puder ser recuperado, uma geração aleatória será gerada
filename = "arquivo _"+(int) (math.random ()*10);
}
// Leia dados
if (Conn.GetRESPONSecode () == httpurlconnection.http_ok) {
byte [] buffer = novo byte [2048];
in = conn.getInputStream ();
out = new FileOutputStream (novo arquivo (local_path, nome do arquivo));
int conting = 0;
int finalizado = 0;
int size = conn.getContentLength ();
while ((count = in.read (buffer))! =-1) {
if (count! = 0) {
out.write (buffer, 0, contagem);
finalizado+= contagem;
System.out.printf ("###############################################d ################# ---->%1 $ .2f %%/n ", (duplo) acabado/size*100);
}outro{
quebrar;
}
}
}
} catch (malformedurlexception e) {
E.PrintStackTrace ();
} catch (ioexception e) {
E.PrintStackTrace ();
} finalmente{
tentar {
out.Close ();
in.Close ();
Conn.Disconnect ();
} catch (ioexception e) {
E.PrintStackTrace ();
}
}
}
}