Descripción
Sintaxis
public abstract class URLConnection extends Object
Constructores
Campos
Métodos
- addRequestProperty()
- connect()
- getAllowUserInteraction()
- getConnectTimeout()
- getContent()
- getContentEncoding()
- getContentLength()
- getContentLengthLong()
- getContentType()
- getDate()
- getDefaultAllowUserInteraction()
- getDefaultRequestProperty()
- getDefaultUseCaches()
- getDoInput()
- getDoOutput()
- getExpiration()
- getFileNameMap()
- getHeaderField()
- getHeaderFieldDate()
- getHeaderFieldInt()
- getHeaderFieldKey()
- getHeaderFieldLong()
- getHeaderFields()
- getIfModifiedSince()
- getInputStream()
- getLastModified()
- getOutputStream()
- getPermission()
- getReadTimeout()
- getRequestProperties()
- getRequestProperty()
- getURL()
- getUseCaches()
- guessContentTypeFromName()
- guessContentTypeFromStream()
- setAllowUserInteraction()
- setConnectTimeout()
- setContentHandlerFactory()
- setDefaultAllowUserInteraction()
- setDefaultRequestProperty()
- setDefaultUseCaches()
- setDoInput()
- setDoOutput()
- setFileNameMap()
- setIfModifiedSince()
- setReadTimeout()
- setRequestProperty()
- setUseCaches()
- toString()
Ejemplo
String url = "http://i.imgur.com/HZ1hq.jpg";
String name = "lineadecodigo.jpg";
String folder = "descargas/";
File dir = new File(folder);
File file = new File(folder + name);
try {
URLConnection conn = new URL(url).openConnection();
conn.connect();
InputStream in = conn.getInputStream();
OutputStream out = new FileOutputStream(file);
int b = 0;
while (b != -1) {
b = in.read();
if (b != -1)
out.write(b);
}
out.close();
in.close();
} catch (MalformedURLException e) {
System.out.println("la url: " + url + " no es valida!");
} catch (IOException e) {
e.printStackTrace();
}
Líneas de Código
Vídeos Java
Disfruta también de nuestros artículos sobre Java en formato vídeo. Aprovecha y suscribete a nuestro canal.