java.lang.Object
java.net.URLConnection
- Direct Known Subclasses:
- HttpURLConnection, JarURLConnection
- See Also:
- Top Examples, Source Code,
setFileNameMap
, getFileNameMap
, URL.openConnection()
,
connect()
,
getContent()
,
getContentEncoding()
,
getContentLength()
,
getContentType()
,
getDate()
,
getExpiration()
,
getHeaderField(int)
,
getHeaderField(java.lang.String)
,
getInputStream()
,
getLastModified()
,
getOutputStream()
,
setAllowUserInteraction(boolean)
,
setDefaultUseCaches(boolean)
,
setDoInput(boolean)
,
setDoOutput(boolean)
,
setIfModifiedSince(long)
,
setRequestProperty(java.lang.String, java.lang.String)
,
setUseCaches(boolean)
public void addRequestProperty(String key,
String value)
- See Also:
getRequestProperties()
, NullPointerException, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[233]Uploading Files over URL connection
By Anonymous on 2005/06/10 01:44:38 Rate
//Uploading Files
String userInfo = user + ":" + password;
String urlString = "ftp://" + userInfo + "@" + host + "/" + file;
URL = new URL ( urlString ) ;
URLConnection urlConn = url.openConnection ( ) ;
OutputStream st = urlConn.getOutputStream ( ) ;
BufferedOutputStream ost = new BufferedOutputStream ( st, bufferSize ) ;
FileInputStream ist = new FileInputStream ( file ) ;
byte buffer [ bufferSize ] ;
int nBytes;
while ( ( nBytes = ist.read ( buffer ) ) != -1 ) {
ost.write ( buffer, nBytes ) ;
}
//addRequestProperty
[1490]Simple example of how to download a page/file/whatever from a url
By Andrew Roth (andrewroth81 { at } hot { dot } removeme { dot } mail { dot } com) on 2005/07/20 15:08:38 Rate
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.JFileChooser;
public class URLDownloader {
private static final int MAX_BUFFER_SIZE = 8192;
public static void downloadURL ( HttpURLConnection u ) throws IOException {
// Display the URL address, and information about it.
System.out.println ( u.getURL ( ) .toExternalForm ( ) + ":" ) ;
System.out.println ( " Content Type: " + u.getContentType ( ) ) ;
System.out.println ( " Content Length: " + u.getContentLength ( ) ) ;
System.out.println ( " Last Modified: " + new Date ( u.getLastModified ( ) ) ) ;
System.out.println ( " Expiration: " + u.getExpiration ( ) ) ;
System.out.println ( " Content Encoding: " + u.getContentEncoding ( ) ) ;
JFileChooser fileChooser = new JFileChooser ( ) ;
int result = fileChooser.showSaveDialog ( null ) ;
if ( result == JFileChooser.APPROVE_OPTION ) {
// set up input stream for url
DataInputStream in = new DataInputStream ( u.getInputStream ( ) ) ;
// set up output stream
OutputStream out = new FileOutputStream ( fileChooser.getSelectedFile ( ) ) ;
// initialize values
final int size = u.getContentLength ( ) ;
int read = 0;
// create buffer
byte [ ] buffer = new byte [ MAX_BUFFER_SIZE ] ;
while ( read < size ) {
if ( in.available ( ) > 0 ) {
System.out.println ( "bytes available = " + in.available ( ) ) ;
// calculate
int toRead = Math.min ( in.available ( ) , MAX_BUFFER_SIZE ) ;
System.out.println ( "to read = " + toRead ) ;
// read from stream
int readLen = in.read ( buffer ) ;
read += readLen;
System.out.println ( "actually read = " + readLen ) ;
System.out.println ( "total read from file = " + read ) ;
// percentage
System.out.println ( ( int ) ( ( float ) read / ( float ) size * 100 ) + "%" ) ;
// write to output file
out.write ( buffer, 0, readLen ) ;
}
}
out.close ( ) ;
in.close ( ) ;
}
}
// Create a URL from the specified address, open a connection to it,
// and then display information about the URL.
public static void main ( String [ ] args )
throws MalformedURLException, IOException
{
URL url = new URL ( args [ 0 ] ) ;
HttpURLConnection connection = ( HttpURLConnection ) url.openConnection ( ) ;
downloadURL ( connection ) ;
connection.disconnect ( ) ;
System.exit ( 0 ) ;
}
}
protected boolean allowUserInteraction
- See Also:
setDefaultAllowUserInteraction(boolean)
, setAllowUserInteraction(boolean)
, getAllowUserInteraction()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void connect()
throws IOException
- See Also:
setConnectTimeout(int)
, getConnectTimeout()
, connected
, SocketTimeoutException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected boolean connected
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected boolean doInput
- See Also:
setDoInput(boolean)
, getDoInput()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected boolean doOutput
- See Also:
setDoOutput(boolean)
, getDoOutput()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean getAllowUserInteraction()
- See Also:
setAllowUserInteraction(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int getConnectTimeout()
- See Also:
connect()
, setConnectTimeout(int)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Object getContent()
throws IOException
- See Also:
setContentHandlerFactory(java.net.ContentHandlerFactory)
, getContentType()
, ContentHandlerFactory.createContentHandler(java.lang.String)
, UnknownServiceException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Object getContent(Class[] classes)
throws IOException
- See Also:
setContentHandlerFactory(java.net.ContentHandlerFactory)
, ContentHandlerFactory.createContentHandler(java.lang.String)
, getContent()
, UnknownServiceException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getContentEncoding()
- See Also:
getHeaderField(java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int getContentLength()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getContentType()
- See Also:
getHeaderField(java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public long getDate()
- See Also:
getHeaderField(java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static boolean getDefaultAllowUserInteraction()
- See Also:
setDefaultAllowUserInteraction(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
@Deprecated
public static String getDefaultRequestProperty(String key)
- See Also:
setDefaultRequestProperty(java.lang.String, java.lang.String)
, getRequestProperty(java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean getDefaultUseCaches()
- See Also:
setDefaultUseCaches(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean getDoInput()
- See Also:
setDoInput(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean getDoOutput()
- See Also:
setDoOutput(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public long getExpiration()
- See Also:
getHeaderField(java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static FileNameMap getFileNameMap()
- See Also:
setFileNameMap(java.net.FileNameMap)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[803]Check a URL file content type
By tcesenthil { at } yahoo { dot } com on 2005/04/05 00:34:17 Rate
String fileName = "c:/test.txt";
FileNameMap fileNameMap = URLConnection.getFileNameMap ( ) ;
String mimeType = fileNameMap.getContentTypeFor ( fileName ) ;
public String getHeaderField(int n)
- See Also:
getHeaderFieldKey(int)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getHeaderField(String name)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public long getHeaderFieldDate(String name,
long Default)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int getHeaderFieldInt(String name,
int Default)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getHeaderFieldKey(int n)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Map<String,List<String>> getHeaderFields()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public long getIfModifiedSince()
- See Also:
setIfModifiedSince(long)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public InputStream getInputStream()
throws IOException
- See Also:
getReadTimeout()
, setReadTimeout(int)
, UnknownServiceException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[767]URLConnection property
By mksvarma { at } epatra { dot } com on 2004/05/07 01:45:27 Rate
import java.net.*;
import java.io.*;
import java.util.*;
public class UrlConnection
{
public static void main ( String args [ ] ) throws MalformedURLException
{
try {
URL url=new URL ( "http","localhost",80,"D://programs//Net//karthik//Demo.java" ) ;
URLConnection con=url.openConnection ( ) ;
System.out.println ( "Date : "+new Date ( con.getDate ( ) ) ) ;
System.out.println ( "Content Type : "+con.getContentType ( ) ) ;
System.out.println ( "Content Length : "+con.getContentLength ( ) ) ;
InputStream in=con.getInputStream ( ) ;
int x=1;
System.out.println ( "x "+x ) ;
int i=con.getContentLength ( ) ;
System.out.println ( "x "+x ) ;
int j=0;
System.out.println ( "x "+x ) ;
while ( ( j=in.read ( ) ) != -1 )
{
System.out.println ( "x "+x ) ;
System.out.print ( ( char ) j ) ;
}
System.out.println ( "x "+x ) ;
in.close ( ) ;
} catch ( Exception e )
{
System.out.println ( "Exp in catch : "+e ) ;
}
}
}
public long getLastModified()
- See Also:
getHeaderField(java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public OutputStream getOutputStream()
throws IOException
- See Also:
- UnknownServiceException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Permission getPermission()
throws IOException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int getReadTimeout()
- See Also:
InputStream.read()
, setReadTimeout(int)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public Map<String,List<String>> getRequestProperties()
- See Also:
- IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getRequestProperty(String key)
- See Also:
setRequestProperty(java.lang.String, java.lang.String)
, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public URL getURL()
- See Also:
url
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public boolean getUseCaches()
- See Also:
setUseCaches(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static String guessContentTypeFromName(String fname)
- See Also:
getContentType()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static String guessContentTypeFromStream(InputStream is)
throws IOException
- See Also:
getContentType()
, InputStream.markSupported()
, InputStream.mark(int)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected long ifModifiedSince
- See Also:
setIfModifiedSince(long)
, getIfModifiedSince()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setAllowUserInteraction(boolean allowuserinteraction)
- See Also:
getAllowUserInteraction()
, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setConnectTimeout(int timeout)
- See Also:
connect()
, getConnectTimeout()
, IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static void setContentHandlerFactory(ContentHandlerFactory fac)
- See Also:
SecurityManager.checkSetFactory()
, getContent()
, SecurityException, Error
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)
- See Also:
getDefaultAllowUserInteraction()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
@Deprecated
public static void setDefaultRequestProperty(String key,
String value)
- See Also:
getDefaultRequestProperty(java.lang.String)
, setRequestProperty(java.lang.String,java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setDefaultUseCaches(boolean defaultusecaches)
- See Also:
getDefaultUseCaches()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setDoInput(boolean doinput)
- See Also:
getDoInput()
, doInput
, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setDoOutput(boolean dooutput)
- See Also:
getDoOutput()
, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1737]How do I access Servlets or CGI programs from Java?
By Anonymous on 2006/03/30 07:04:14 Rate
Using the QUERY method ( i.e., the calling parameters encoded in the URL ) , it is simple to access Servlets or CGI programs from Java:
URLConnection connect ( new URL ( "http:\\me\mycgi.sh" ) ) .getConnection ( ) ;
PrintStream ps = new PrintStream ( connect.getOutputStream ( ) ) ;
connection.setdoOutput ( true ) // defaults to false
ps.print ( myPostData ) ;
ps.close ( ) // very important to close before reading
InputStream is = new PrintStream ( connect.getInputStream ( ) ) ;
// read the results
NOTE: URLConnection ( ) is an abstract class whose sub-class is usually supplied by the environment ( browser ) . Some browsers allow output without calling "doOutput ( true ) ," which violates the Java spec and may be confusing when code works in one place and not the other.
public static void setFileNameMap(FileNameMap map)
- See Also:
getFileNameMap()
, SecurityManager.checkSetFactory()
, SecurityException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setIfModifiedSince(long ifmodifiedsince)
- See Also:
getIfModifiedSince()
, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setReadTimeout(int timeout)
- See Also:
InputStream.read()
, getReadTimeout()
, IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setRequestProperty(String key,
String value)
- See Also:
getRequestProperty(java.lang.String)
, NullPointerException, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setUseCaches(boolean usecaches)
- See Also:
getUseCaches()
, IllegalStateException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String toString()
- See Also:
- Object
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected URL url
- See Also:
getURL()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected URLConnection(URL url)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected boolean useCaches
- See Also:
setDefaultUseCaches(boolean)
, getUseCaches()
, setUseCaches(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples