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:net.pms.medialibrary.commons.helpers.FileImportHelper.java

/**
 * Saves the file specified by the url to the file saveFileName
 * @param url url of the file to save//from   w w  w . j  a  va2  s .  com
 * @param saveFileName absolute path to save the file to
 * @throws IOException thrown if the save operation failed or the content type of the file was of type text
 */
public static void saveUrlToFile(String url, String saveFileName) throws IOException {
    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.");
    }
    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");
    }

    FileOutputStream out = new FileOutputStream(saveFileName);
    out.write(data);
    out.flush();
    out.close();

    log.debug(String.format("Saved url='%s' to file='%s'", url, saveFileName));
}

From source file:com.feilong.commons.core.io.FileUtilTest.java

/**
 * Test get content length.//from  w  w  w . jav a2 s. c  om
 */
@Test
public void testGetContentLength() {
    try {
        URL url = new URL("http://www.jinbaowang.cn/images//20110722/096718c3d1c9b4a1.jpg");
        URLConnection urlConnection = url.openConnection();
        int contentLength = urlConnection.getContentLength();
        log.info(FileUtil.formatSize(contentLength));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    try {
        URL url = new URL("http://localhost:8080/TestHttpURLConnectionPro/index.jsp");
        url.openConnection();
    } catch (MalformedURLException e) {
        log.error(e.getClass().getName(), e);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.jboss.arquillian.container.tomcat.CommonTomcatManager.java

public void deploy(String name, URL content) throws IOException, DeploymentException {
    final String contentType = "application/octet-stream";
    Validate.notNullOrEmpty(name, "Name must not be null or empty");
    Validate.notNull(content, "Content to be deployed must not be null");

    URLConnection conn = content.openConnection();
    int contentLength = conn.getContentLength();
    InputStream stream = new BufferedInputStream(conn.getInputStream());

    // Building URL
    StringBuilder command = new StringBuilder(getDeployCommand());
    try {//from   w w  w .  j a  v  a  2 s.  c o  m
        command.append(URLEncoder.encode(name, configuration.getUrlCharset()));
    } catch (UnsupportedEncodingException e) {
        throw new DeploymentException("Unable to construct path for Tomcat manager", e);
    }

    execute(command.toString(), stream, contentType, contentLength);
}

From source file:org.biopax.psidev.ontology_manager.impl.OboLoader.java

/**
 * Load an OBO file from an URL./*www  .  j  av a  2  s .  c  om*/
 *
 * @param url the URL to load (must not be null)
 * @return an ontology
 * @see #parseOboFile(File, String)
 */
public OntologyAccess parseOboFile(URL url, String ontologyID) throws OntologyLoaderException {
    // load config file (ie. a map)
    // check if that URL has already been loaded
    // if so, get the associated temp file and check if available
    // if available, then load it and skip URL load
    // if any of the above failed, load it from the network.

    if (url == null) {
        throw new IllegalArgumentException("Please give a non null URL.");
    }

    File ontologyFile = null;

    try {
        if (ontologyFile == null || !ontologyFile.exists() || !ontologyFile.canRead()) {
            // if it is not defined, not there or not readable...
            // Read URL content
            log.info("Loading URL: " + url);
            URLConnection con = url.openConnection();
            long size = con.getContentLength(); // -1 if not stat available
            log.info("size = " + size);

            InputStream is = url.openStream();

            // make the temporary file name specific to the URL
            String name = null;
            String filename = url.getFile();
            int idx = filename.lastIndexOf('/');
            if (idx != -1) {
                name = filename.substring(idx + 1, filename.length());
                name = name.replaceAll("[.,;:&^%$@*?=]", "_");
            } else {
                name = "unknown";
            }

            ontologyFile = Files.createTempFile(name + "_", ".obo").toFile();
            ontologyFile.deleteOnExit();
            log.debug("The OBO file will be temporary stored as: " + ontologyFile.getAbsolutePath());

            FileOutputStream out = new FileOutputStream(ontologyFile);
            if (size == -1)
                size = 1024 * 1024 * 1024; //Integer.MAX_VALUE;
            ReadableByteChannel source = Channels.newChannel(is);
            size = out.getChannel().transferFrom(source, 0, size);
            log.info(size + " bytes downloaded");

            is.close();
            out.flush();
            out.close();
        }

        if (ontologyFile == null) {
            log.error("The ontology file is still null...");
        }

        // Parse file
        return parseOboFile(ontologyFile, ontologyID);

    } catch (IOException e) {
        throw new OntologyLoaderException("Error while loading URL (" + url + ")", e);
    }
}

From source file:com.threewks.thundr.route.staticResource.StaticResourceRouteResolver.java

protected void serve(StaticResource action, Request request, Response response)
        throws ServletException, IOException {
    String resource = request.getRequestPath();
    URL resourceUrl = servletContext.getResource(resource);
    boolean allowed = isAllowed(resource);
    if (resourceUrl == null || !allowed) {
        response.withStatusCode(StatusCode.NotFound);
        Logger.info("%s -> %s not resolved: %s", resource, action, allowed ? "Not found" : "Not Permitted");
        return;/*from   ww  w. j a  v a  2s . com*/
    }

    URLConnection urlConnection = resourceUrl.openConnection();
    String mimeType = deriveMimeType(resource);
    long contentLength = urlConnection.getContentLength();
    long lastModified = urlConnection.getLastModified();
    String acceptEncoding = request.getHeader(Header.AcceptEncoding);
    long cacheTimeSeconds = deriveCacheDuration(resource, mimeType);

    // TODO - v3 - Validate objects written to headers are serialized correctly - ints and dates mostly.
    // @formatter:off
    response.withContentType(mimeType)
            //response.setDateHeader(Header.Expires, System.currentTimeMillis() + cacheTimeSeconds * 1000L); // HTTP 1.0
            .withHeader(Header.Expires, System.currentTimeMillis() + cacheTimeSeconds * 1000L) // HTTP 1.0
            .withHeader(Header.CacheControl, String.format("max-age=%d, public", cacheTimeSeconds)) // HTTP 1.1
            .withHeader(Header.LastModified, lastModified);
    // @formatter:on

    OutputStream os = null;
    InputStream is = urlConnection.getInputStream();

    if (shouldZip(acceptEncoding, mimeType)) {
        HttpServletResponse resp = response.getRawResponse(HttpServletResponse.class);
        GzipResponseWrapper wrapper = new GzipResponseWrapper(resp);
        os = wrapper.getOutputStream();
        StreamUtil.copy(is, os);
        wrapper.finishResponse();
    } else {
        response.withHeader(Header.ContentLength, Long.toString(contentLength));
        os = response.getOutputStream();
        StreamUtil.copy(is, os);
        os.close();
    }

    response.withStatusCode(StatusCode.OK);
    Logger.debug("%s -> %s resolved as %s(%d bytes)", resource, action, mimeType, contentLength);
}

From source file:org.geoserver.ows.AbstractURLPublisher.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    URL url = getUrl(request);/*from w ww  .ja v a 2s.c  o m*/

    // if not found return a 404
    if (url == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return null;
    }

    File file = DataUtilities.urlToFile(url);
    if (file != null && file.exists() && file.isDirectory()) {
        String uri = request.getRequestURI().toString();
        uri += uri.endsWith("/") ? "index.html" : "/index.html";

        response.addHeader("Location", uri);
        response.sendError(HttpServletResponse.SC_MOVED_TEMPORARILY);

        return null;
    }

    // set the mime if known by the servlet container, set nothing otherwise
    // (Tomcat behaves like this when it does not recognize the file format)
    String mime = getServletContext().getMimeType(new File(url.getFile()).getName());
    if (mime != null) {
        response.setContentType(mime);
    }

    // set the content length and content type
    URLConnection connection = null;
    InputStream input = null;
    try {
        connection = url.openConnection();
        long length = connection.getContentLength();
        if (length > 0 && length <= Integer.MAX_VALUE) {
            response.setContentLength((int) length);
        }

        long lastModified = connection.getLastModified();
        if (lastModified > 0) {
            SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.ENGLISH);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            String formatted = format.format(new Date(lastModified)) + " GMT";
            response.setHeader("Last-Modified", formatted);
        }

        // Guessing the charset (and closing the stream)
        EncodingInfo encInfo = null;
        OutputStream output = null;
        final byte[] b4 = new byte[4];
        int count = 0;
        // open the output
        input = connection.getInputStream();

        // Read the first four bytes, and determine charset encoding
        count = input.read(b4);
        encInfo = XmlCharsetDetector.getEncodingName(b4, count);
        response.setCharacterEncoding(encInfo.getEncoding() != null ? encInfo.getEncoding() : "UTF-8");

        // send out the first four bytes read
        output = response.getOutputStream();
        output.write(b4, 0, count);

        // copy the content to the output
        byte[] buffer = new byte[8192];
        int n = -1;
        while ((n = input.read(buffer)) != -1) {
            output.write(buffer, 0, n);
        }
    } finally {
        if (input != null)
            input.close();
    }

    return null;
}

From source file:org.openstatic.http.HttpResponse.java

/** Set the entire response body to the resource represented by this URL object, it will be relayed as read. */
public void setData(URL url) {
    try {//w  w w.jav a  2s.  c  o m
        URLConnection urlc = url.openConnection();
        this.data = urlc.getInputStream();
        this.contentType = urlc.getContentType();
        this.dataLength = urlc.getContentLength();
    } catch (Exception e) {
        this.responseCode = "500 Server Error";
    }
}

From source file:net.paissad.waqtsalat.utils.DownloadHelper.java

/**
 * <p>/*w w w. ja  v a  2 s .  c  o  m*/
 * Download a resource from the specified url and save it to the specified
 * file.
 * </p>
 * <b>Note</b>: If you plan to cancel the download at any time, then this
 * method should be embed into it's own thread.
 * <p>
 * <b>Example</b>:
 * 
 * <pre>
 *  final DownloadHelper downloader = new DownloadHelper();
 *  Thread t = new Thread() {
 *      public void run() {
 *          try {
 *              downloader.download("http://domain.com/file.zip", new File("/tmp/output.zip"));
 *          } catch (Exception e) {
 *              ...
 *          }
 *      }
 *  };
 *  t.start();
 *  // Waits 3 seconds and then cancels the download.
 *  Thread.sleep(3 * 1000L);
 *  downloader.cancel();
 *  ...
 * </pre>
 * 
 * </p>
 * 
 * @param url
 *            - The url of the file to download.
 * @param file
 *            - The downloaded file (where it will be stored).
 * @throws IOException
 * @throws HttpException
 */
public void download(final String url, final File file) throws HttpException, IOException {

    GetMethod method = null;
    InputStream responseBody = null;
    OutputStream out = null;

    try {
        final boolean fileExisted = file.exists();

        HttpClient client = new HttpClient();
        method = new GetMethod(url);
        method.setFollowRedirects(true);
        method.setRequestHeader("User-Agent", WSConstants.WS_USER_AGENT);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(3, false));
        method.getParams().setParameter(HttpMethodParams.WARN_EXTRA_INPUT, Boolean.TRUE);

        // Execute the method.
        int responseCode = client.executeMethod(method);
        if (responseCode != HttpStatus.SC_OK) {
            logger.error("Http method failed : {}.", method.getStatusLine().toString());
        }

        // Read the response body.
        responseBody = method.getResponseBodyAsStream();

        // Let's try to compute the amount of total bytes of the file to
        // download.
        URL u = new URL(url);
        URLConnection urlc = u.openConnection();
        this.totalBytes = urlc.getContentLength();
        long lastModified = urlc.getLastModified();

        // The OutputStream for the 'downloaded' file.
        out = new BufferedOutputStream(new FileOutputStream(file));

        this.updateState(DownloadState.IN_PROGRESS);

        byte[] data = new byte[BUFFER_SIZE];
        int length;
        while ((length = responseBody.read(data, 0, BUFFER_SIZE)) != -1 && !isCancelled) {
            out.write(data, 0, length);
            this.bytesDownloaded += length;
            setChanged();
            notifyObservers(getBytesDownloaded());
        }

        if (isCancelled) {
            this.updateState(DownloadState.CANCELED);
            logger.info("The download has been cancelled.");

        } else {
            // The download was not cancelled.
            out.flush();
            if (lastModified > 0) {
                file.setLastModified(lastModified);
            }

            if (bytesDownloaded != totalBytes) {
                logger.warn("The expected bytes to download is {}, but got {}.", getTotalBytes(),
                        getBytesDownloaded());
                this.updateState(DownloadState.ERROR);
            }

            this.updateState(DownloadState.FINISHED);
            logger.info("Download of '{}' finished successfully.", url);
        }

        // If the file did not exist before the download but does exit now,
        // we must remove it if an error occurred or if the download was
        // cancelled.
        if (getState() == DownloadState.CANCELED || getState() == DownloadState.ERROR) {
            if (!fileExisted && file.exists()) {
                FileUtils.forceDelete(file);
            }
        }

    } catch (HttpException he) {
        this.updateState(DownloadState.ERROR);
        logger.error("Fatal protocol violation : " + he);
        throw new HttpException("Error while downloading from the url '" + url + "'", he);

    } catch (IOException ioe) {
        this.updateState(DownloadState.ERROR);
        logger.error("Fatal transport error : " + ioe);
        throw new IOException(ioe);

    } finally {
        if (method != null)
            method.releaseConnection();
        if (responseBody != null)
            responseBody.close();
        if (out != null)
            out.close();
    }
}

From source file:org.springframework.extensions.webscripts.servlet.mvc.ResourceController.java

/**
 * Commit the resource to the response stream.
 * Sets appropriate date, length and content type headers.
 * // w w  w.j a  v a2s .c  o  m
 * @throws IOException
 */
public void commitResponse(final String path, final URL resourceUrl, final HttpServletResponse response)
        throws IOException {
    // determine properties of the resource being served back
    final URLConnection resourceConn = resourceUrl.openConnection();
    applyHeaders(path, response, resourceConn.getContentLength(), resourceConn.getLastModified());

    // stream back to response
    copyStream(resourceUrl.openStream(), response.getOutputStream());
}

From source file:wicket.util.resource.UrlResourceStream.java

/**
 * Private constructor to force use of static factory methods.
 * //from ww w. j av a2  s . c o m
 * @param url
 *            URL of resource
 */
public UrlResourceStream(final URL url) {
    // Save URL
    this.url = url;
    URLConnection connection = null;
    try {
        connection = url.openConnection();
        contentLength = connection.getContentLength();
        contentType = connection.getContentType();
        lastModified = connection.getLastModified();
        try {
            file = new File(new URI(url.toExternalForm()));
        } catch (Exception ex) {
            log.debug("cannot convert url: " + url + " to file (" + ex.getMessage()
                    + "), falling back to the inputstream for polling");
        }
        if (file != null && !file.exists()) {
            file = null;
        }
    } catch (IOException ex) {
        // It should be impossible to get here or the original URL
        // couldn't have been constructed. But we re-throw with details
        // anyway.
        final IllegalArgumentException illegalArgumentException = new IllegalArgumentException(
                "Invalid URL parameter " + url);
        illegalArgumentException.initCause(ex);
        throw illegalArgumentException;
    } finally {
        // if applicable, disconnect
        if (connection != null) {
            if (connection instanceof HttpURLConnection) {
                ((HttpURLConnection) connection).disconnect();
            } else {
                try {
                    connection.getInputStream().close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
    }
}