Example usage for java.net URLConnection getContentLength

List of usage examples for java.net URLConnection getContentLength

Introduction

In this page you can find the example usage for java.net URLConnection getContentLength.

Prototype

public int getContentLength() 

Source Link

Document

Returns the value of the content-length header field.

Usage

From source file:UrlHelper.java

public static void downloadFile(String adresse, File dest) {
    BufferedReader reader = null;
    FileOutputStream fos = null;/*  w  ww.  jav a  2s  .c o  m*/
    InputStream in = null;
    try {

        // cration de la connection
        URL url = new URL(adresse);
        URLConnection conn = url.openConnection();
        String FileType = conn.getContentType();

        int FileLenght = conn.getContentLength();
        if (FileLenght == -1) {
            throw new IOException("Fichier non valide.");
        }

        // lecture de la rponse
        in = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        if (dest == null) {
            String FileName = url.getFile();
            FileName = FileName.substring(FileName.lastIndexOf('/') + 1);
            dest = new File(FileName);
        }
        fos = new FileOutputStream(dest);
        byte[] buff = new byte[1024];
        int l = in.read(buff);
        while (l > 0) {
            fos.write(buff, 0, l);
            l = in.read(buff);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.kalypso.commons.java.net.UrlUtilities.java

public static long getContentLength(final URL url) throws IOException {
    final File file = FileUtils.toFile(url);
    if (file != null)
        return file.length();

    final File platformFile = ResourceUtilities.findJavaFileFromURL(url);
    if (platformFile != null)
        return platformFile.length();

    final URLConnection connection = url.openConnection();
    return connection.getContentLength();
}

From source file:org.gitools.ui.app.welcome.WelcomeEditor.java

private static URL getWelcomeURL() {
    try {/*from   w ww. jav  a 2  s . c om*/
        URL url = new URL(Settings.get().getWelcomeUrl() + "?appversion="
                + Application.getGitoolsVersion().toString() + "&uuid=" + Settings.get().getUuid());
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(10000);

        if (connection.getContentLength() != -1) {
            return url;
        }

    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }

    return WelcomeEditor.class.getResource("/html/welcome.html");
}

From source file:com.google.android.feeds.ContentHandlerUtils.java

/**
 * Returns the body of a {@link URLConnection} as a {@link String}.
 *//*from w  ww .  j  a  v a  2 s .  c  o m*/
public static String toString(URLConnection connection) throws IOException {
    if (connection == null) {
        throw new IllegalArgumentException("URLConnection is null");
    }
    int contentLength = connection.getContentLength();
    if (contentLength < 0) {
        contentLength = DEFAULT_BUFFER_SIZE;
    }
    String charset = getCharSet(connection);
    InputStream input = getUncompressedInputStream(connection);
    try {
        InputStreamReader reader = new InputStreamReader(input, charset);
        StringBuilder builder = new StringBuilder(contentLength);
        char[] buffer = new char[1024];
        for (int n = reader.read(buffer); n != -1; n = reader.read(buffer)) {
            builder.append(buffer, 0, n);
        }
        return builder.toString();
    } finally {
        input.close();
    }
}

From source file:com.baidu.api.client.core.DownloadUtil.java

/**
 * Download the file pointed by the url and return the content as byte array.
 *
 * @param strUrl         The Url to be downloaded.
 * @param connectTimeout Connect timeout in milliseconds.
 * @param readTimeout    Read timeout in milliseconds.
 * @param maxFileSize    Max file size in BYTE.
 * @return The file content as byte array.
 *//*  w  w w . j  a v  a 2s .co m*/
public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize) {
    InputStream in = null;
    try {
        URL url = new URL(strUrl); // URL?
        URLConnection ucon = url.openConnection();
        ucon.setConnectTimeout(connectTimeout);
        ucon.setReadTimeout(readTimeout);
        ucon.connect();
        if (ucon.getContentLength() > maxFileSize) {
            String msg = "File " + strUrl + " size [" + ucon.getContentLength()
                    + "] too large, download stoped.";
            throw new ClientInternalException(msg);
        }
        if (ucon instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) ucon;
            if (httpCon.getResponseCode() > 399) {
                String msg = "Failed to download file " + strUrl + " server response "
                        + httpCon.getResponseMessage();
                throw new ClientInternalException(msg);
            }
        }
        in = ucon.getInputStream(); // ?
        byte[] byteBuf = new byte[BUFFER_SIZE];
        byte[] ret = null;
        int count, total = 0;
        // ??
        while ((count = in.read(byteBuf, total, BUFFER_SIZE - total)) > 0) {
            total += count;
            if (total + 124 >= BUFFER_SIZE)
                break;
        }
        if (total < BUFFER_SIZE - 124) {
            ret = ArrayUtils.subarray(byteBuf, 0, total);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MATERIAL_SIZE);
            count = total;
            total = 0;
            do {
                bos.write(byteBuf, 0, count);
                total += count;
                if (total > maxFileSize) {
                    String msg = "File " + strUrl + " size exceed [" + maxFileSize + "] download stoped.";
                    throw new ClientInternalException(msg);
                }
            } while ((count = in.read(byteBuf)) > 0);
            ret = bos.toByteArray();
        }
        if (ret.length < MIN_SIZE) {
            String msg = "File " + strUrl + " size [" + maxFileSize + "] too small.";
            throw new ClientInternalException(msg);
        }
        return ret;
    } catch (IOException e) {
        String msg = "Failed to download file " + strUrl + " msg=" + e.getMessage();
        throw new ClientInternalException(msg);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
            // ?
            System.out.println("Exception while close url - " + e.getMessage());
        }
    }
}

From source file:GetURLInfo.java

/** Use the URLConnection class to get info about the URL */
public static void printinfo(URL url) throws IOException {
    URLConnection c = url.openConnection(); // Get URLConnection from URL
    c.connect(); // Open a connection to URL

    // Display some information about the URL contents
    System.out.println("  Content Type: " + c.getContentType());
    System.out.println("  Content Encoding: " + c.getContentEncoding());
    System.out.println("  Content Length: " + c.getContentLength());
    System.out.println("  Date: " + new Date(c.getDate()));
    System.out.println("  Last Modified: " + new Date(c.getLastModified()));
    System.out.println("  Expiration: " + new Date(c.getExpiration()));

    // If it is an HTTP connection, display some additional information.
    if (c instanceof HttpURLConnection) {
        HttpURLConnection h = (HttpURLConnection) c;
        System.out.println("  Request Method: " + h.getRequestMethod());
        System.out.println("  Response Message: " + h.getResponseMessage());
        System.out.println("  Response Code: " + h.getResponseCode());
    }/*from ww w . ja va 2  s.  co m*/
}

From source file:com.iwgame.iwcloud.baidu.task.util.DownloadUtil.java

/**
 * url//from  ww  w .j a  v a  2s .  c om
 * @param strUrl
 *            The Url to be downloaded.
 * @param connectTimeout
 *            Connect timeout in milliseconds.
 * @param readTimeout
 *            Read timeout in milliseconds.
 * @param maxFileSize
 *            Max file size in BYTE.
 * @return The file content as byte array.
 */
public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize)
        throws IOException {
    InputStream in = null;
    try {
        URL url = new URL(strUrl); // URL?
        URLConnection ucon = url.openConnection();
        ucon.setConnectTimeout(connectTimeout);
        ucon.setReadTimeout(readTimeout);
        ucon.connect();
        if (ucon.getContentLength() > maxFileSize) {
            String msg = "File " + strUrl + " size [" + ucon.getContentLength()
                    + "] too large, download stoped.";
            logger.error(msg);
            throw new ClientInternalException(msg);
        }
        if (ucon instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) ucon;
            if (httpCon.getResponseCode() > 399) {
                String msg = "Failed to download file " + strUrl + " server response "
                        + httpCon.getResponseMessage();
                logger.error(msg);
                throw new ClientInternalException(msg);
            }
        }
        in = ucon.getInputStream(); // ?
        byte[] byteBuf = new byte[BUFFER_SIZE];
        byte[] ret = null;
        int count, total = 0;
        // ??
        while ((count = in.read(byteBuf, total, BUFFER_SIZE - total)) > 0) {
            total += count;
            if (total + 124 >= BUFFER_SIZE)
                break;
        }
        if (total < BUFFER_SIZE - 124) {
            ret = ArrayUtils.subarray(byteBuf, 0, total);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MATERIAL_SIZE);
            count = total;
            total = 0;
            do {
                bos.write(byteBuf, 0, count);
                total += count;
                if (total > maxFileSize) {
                    String msg = "File " + strUrl + " size exceed [" + maxFileSize + "] download stoped.";
                    logger.error(msg);
                    throw new ClientInternalException(msg);
                }
            } while ((count = in.read(byteBuf)) > 0);
            ret = bos.toByteArray();
        }
        if (ret.length < MIN_SIZE) {
            String msg = "File " + strUrl + " size [" + maxFileSize + "] too small.";
            logger.error(msg);
            throw new ClientInternalException(msg);
        }
        return ret;
    } catch (IOException e) {
        String msg = "Failed to download file " + strUrl + " msg=" + e.getMessage();
        logger.error(msg);
        throw e;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            logger.error("Exception while close url - ", e);
            throw e;
        }
    }
}

From source file:jp.terasoluna.fw.batch.unit.util.ClassLoaderUtils.java

/**
 * <pre>//from  w  ww .  j a v  a  2  s  . c o m
 * srcPaths??????????
 * ??????
 * ????destPaths????
 * </pre>
 * 
 * @param destPaths
 * @param srcPaths
 */
public static void addPathIfExists(List<String> destPaths, List<String> srcPaths) {
    Assert.notNull(destPaths);
    Assert.notNull(srcPaths);

    ClassLoader cl = getClassLoader();
    for (String path : srcPaths) {
        if (path != null) {
            URL r = cl.getResource(path);
            if (r != null) {
                try {
                    String protocol = r.getProtocol();
                    if (protocol.equals("file")) {
                        URI uri = r.toURI();
                        File f = new File(uri);
                        if (f.isFile()) {
                            // ?????
                            destPaths.add(path);
                        }
                    } else {
                        // jar
                        URLConnection con = null;
                        try {
                            con = r.openConnection();
                            if (con.getContentLength() > 0) {
                                // ?????
                                destPaths.add(path);
                            }
                        } catch (IOException e) {
                            // ?????????
                            LOG.warn(con + " is illegal.", e);
                        }
                    }
                } catch (URISyntaxException e) {
                    // r != null???????????????
                    LOG.warn(path + " is illegal.", e);
                }
            } else {
                LOG.debug(path + " is not found.");
            }
        }
    }
}

From source file:org.castafiore.utils.ResourceUtil.java

public static byte[] readUrlBinary(String url) throws Exception {

    URL u = new URL(url);
    URLConnection uc = u.openConnection();
    String contentType = uc.getContentType();
    int contentLength = uc.getContentLength();
    if (contentType.startsWith("text/") || contentLength == -1) {
        throw new IOException("This is not a binary file.");
    }//from   w w  w.  j a  va2s. c o m
    InputStream raw = uc.getInputStream();
    InputStream in = new BufferedInputStream(raw);
    byte[] data = new byte[contentLength];
    int bytesRead = 0;
    int offset = 0;
    while (offset < contentLength) {
        bytesRead = in.read(data, offset, data.length - offset);
        if (bytesRead == -1)
            break;
        offset += bytesRead;
    }
    in.close();

    if (offset != contentLength) {
        throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
    }

    return data;

}

From source file:Downloader.java

/**
 * Downloads the specified page to disk.
 * /*from   ww w. j  ava2s . c  om*/
 * @param url The URL to retrieve
 * @param file The file to save the page to
 * @param listener The progress listener for this download
 * @throws java.io.IOException If there's an I/O error while downloading
 */
public static void downloadPage(final String url, final String file, final DownloadListener listener)
        throws IOException {

    final URLConnection urlConn = getConnection(url, "");
    final File myFile = new File(file);

    final FileOutputStream output = new FileOutputStream(myFile);
    final InputStream input = urlConn.getInputStream();
    final int length = urlConn.getContentLength();
    int current = 0;

    final byte[] buffer = new byte[512];
    int count;

    do {
        count = input.read(buffer);

        if (count > 0) {
            current += count;
            output.write(buffer, 0, count);

            if (listener != null) {
                listener.downloadProgress(100 * current / length);
            }
        }
    } while (count > 0);

    input.close();
    output.close();
}