Example usage for java.net URLConnection getContentLengthLong

List of usage examples for java.net URLConnection getContentLengthLong

Introduction

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

Prototype

public long getContentLengthLong() 

Source Link

Document

Returns the value of the content-length header field as a long.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();

    System.out.println(uc.getContentLengthLong());
}

From source file:radet.publisher.Test.java

public static void main(String[] args) throws Exception {
    File file = new File(System.getProperty("java.io.tmpdir"), "Opriri_programate.pdf");

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

    Calendar ifModifiedSince = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
    ifModifiedSince.setTimeInMillis(file.lastModified());
    System.out.println(sdf.format(ifModifiedSince.getTime()));
    System.out.println(ifModifiedSince.getTime());

    URL url = new URL("http://radet.ro/opriri/Opriri_programate.pdf");
    URLConnection connection = url.openConnection();
    //        connection.setRequestProperty("If-Modified-Since", "Wed, 10 Aug 2016 20:35:57 GMT");

    String ifModifiedSinceString = sdf.format(ifModifiedSince.getTime());
    connection.setRequestProperty("If-Modified-Since", sdf.format(ifModifiedSince.getTime()));
    System.out.println("If-Modified-Since " + ifModifiedSinceString);

    try (InputStream urlInputStream = connection.getInputStream()) {
        try (OutputStream pdfOutputStream = new FileOutputStream(file)) {
            byte[] bytes = IOUtils.toByteArray(urlInputStream);
            System.out.println("#bytes " + bytes.length);
            IOUtils.write(bytes, pdfOutputStream);
        }/*from w w w . j a  va 2 s .com*/
    }
    Calendar lastModified = Calendar.getInstance();
    lastModified.setTimeInMillis(connection.getLastModified());
    System.out.println(sdf.format(lastModified.getTime()));
    System.out.println(file.getAbsolutePath());
    System.out.println(connection.getContentLengthLong());
}

From source file:radet.publisher.JmsPublishJob.java

private static boolean downloadPdfIfModifiedSince(long modifiedSince)
        throws IOException, MalformedURLException {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar ifModifiedSince = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
    ifModifiedSince.setTimeInMillis(modifiedSince);

    URL url = new URL("http://radet.ro/opriri/Opriri_programate.pdf");
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("If-Modified-Since", sdf.format(ifModifiedSince.getTime()));
    try (InputStream urlInputStream = connection.getInputStream()) {
        try (OutputStream pdfOutputStream = new FileOutputStream(PDF_FILE)) {
            IOUtils.write(IOUtils.toByteArray(urlInputStream), pdfOutputStream);
        }/*from  w ww . ja  va 2 s .com*/
    }
    return connection.getContentLengthLong() > 0;
}

From source file:com.ramforth.webserver.http.modules.HttpUrlModule.java

private void addContentLengthHeaderForURL(IHttpResponse httpResponse, URLConnection urlConnection) {
    httpResponse.setContentLength(urlConnection.getContentLengthLong());
}

From source file:com.eucalyptus.cloudformation.CloudFormationService.java

private static String extractTemplateTextFromURL(String templateUrl, User user)
        throws ValidationErrorException {
    final URL url;
    try {//ww w  .  ja  v  a2s .  co m
        url = new URL(templateUrl);
    } catch (MalformedURLException e) {
        throw new ValidationErrorException("Invalid template url " + templateUrl);
    }
    // First try straight HTTP GET if url is in whitelist
    boolean inWhitelist = WhiteListURLMatcher.urlIsAllowed(url, URL_DOMAIN_WHITELIST);
    if (inWhitelist) {
        InputStream templateIn = null;
        try {
            final URLConnection connection = SslSetup.configureHttpsUrlConnection(url.openConnection());
            templateIn = connection.getInputStream();
            long contentLength = connection.getContentLengthLong();
            if (contentLength > Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES) {
                throw new ValidationErrorException("Template URL exceeds maximum byte count, "
                        + Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES);
            }
            final byte[] templateData = ByteStreams.toByteArray(new BoundedInputStream(templateIn,
                    Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES + 1));
            if (templateData.length > Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES) {
                throw new ValidationErrorException("Template URL exceeds maximum byte count, "
                        + Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES);
            }
            return new String(templateData, StandardCharsets.UTF_8);
        } catch (UnknownHostException ex) {
            throw new ValidationErrorException("Invalid template url " + templateUrl);
        } catch (SSLHandshakeException ex) {
            throw new ValidationErrorException("HTTPS connection error for " + templateUrl);
        } catch (IOException ex) {
            if (Strings.nullToEmpty(ex.getMessage()).startsWith("HTTPS hostname wrong")) {
                throw new ValidationErrorException(
                        "HTTPS connection failed hostname verification for " + templateUrl);
            }
            LOG.info("Unable to connect to whitelisted URL, trying S3 instead");
            LOG.debug(ex, ex);
        } finally {
            IO.close(templateIn);
        }
    }

    // Otherwise, assume the URL is a eucalyptus S3 url...
    String[] validHostBucketSuffixes = new String[] { "walrus", "objectstorage", "s3" };
    String[] validServicePaths = new String[] { ObjectStorageProperties.LEGACY_WALRUS_SERVICE_PATH,
            ComponentIds.lookup(ObjectStorage.class).getServicePath() };
    String[] validDomains = new String[] { DomainNames.externalSubdomain().relativize(Name.root).toString() };
    S3Helper.BucketAndKey bucketAndKey = S3Helper.getBucketAndKeyFromUrl(url, validServicePaths,
            validHostBucketSuffixes, validDomains);
    try (final EucaS3Client eucaS3Client = EucaS3ClientFactory
            .getEucaS3Client(new SecurityTokenAWSCredentialsProvider(user))) {
        if (eucaS3Client.getObjectMetadata(bucketAndKey.getBucket(), bucketAndKey.getKey())
                .getContentLength() > Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES) {
            throw new ValidationErrorException("Template URL exceeds maximum byte count, "
                    + Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES);
        }
        return eucaS3Client.getObjectContent(bucketAndKey.getBucket(), bucketAndKey.getKey(),
                (int) Limits.REQUEST_TEMPLATE_URL_MAX_CONTENT_LENGTH_BYTES);
    } catch (Exception ex) {
        LOG.debug("Error getting s3 object content: " + bucketAndKey.getBucket() + "/" + bucketAndKey.getKey());
        LOG.debug(ex, ex);
        throw new ValidationErrorException(
                "Template url is an S3 URL to a non-existent or unauthorized bucket/key.  (bucket="
                        + bucketAndKey.getBucket() + ", key=" + bucketAndKey.getKey());
    }
}

From source file:org.mycore.common.content.MCRURLContent.java

@Override
public String getETag() throws IOException {
    URLConnection openConnection = url.openConnection();
    openConnection.connect();/*from  w w  w  .  j a va2s.co m*/
    String eTag = openConnection.getHeaderField("ETag");
    if (eTag != null) {
        return eTag;
    }
    long lastModified = openConnection.getLastModified();
    long length = openConnection.getContentLengthLong();
    eTag = getSimpleWeakETag(url.toString(), length, lastModified);
    return eTag == null ? null : eTag.substring(2);
}

From source file:com.kaylerrenslow.armaDialogCreator.updater.tasks.AdcVersionCheckTask.java

private void downloadLatestRelease(String downloadUrl) throws Exception {
    setIndeterminateProgress();//from   www. j  a  va 2s  . c  o m
    setStatusText("Updater.downloading_newest_version");

    URL url = new URL(downloadUrl);

    BufferedInputStream in = null;
    FileOutputStream fout = null;
    URLConnection urlConnection = null;
    long workDone = 0;

    downloadDirectory.mkdirs();
    try {
        urlConnection = url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream());
        fout = new FileOutputStream(downloadDirectory.getAbsolutePath() + "/" + adcJarSave.getName());

        long downloadSize = urlConnection.getContentLengthLong();
        if (downloadDirectory.getFreeSpace() < downloadSize) {
            in.close();
            fout.close();
            urlConnection.getInputStream().close();
            throw new NotEnoughFreeSpaceException(ADCUpdater.bundle.getString("Updater.not_enough_free_space"));
        }

        final byte data[] = new byte[1024];
        int count;
        while ((count = in.read(data, 0, 1024)) != -1) {
            fout.write(data, 0, count);
            workDone += count;
            updateProgress(workDone, downloadSize);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (urlConnection != null) {
            urlConnection.getInputStream().close();
        }
        if (fout != null) {
            fout.close();
        }
    }
    setStatusText("Updater.download_complete");
    Thread.sleep(1000);
    setStatusText("Updater.launching");
    Thread.sleep(1000);
}

From source file:de.matzefratze123.heavyspleef.util.Updater.java

public void update(final CommandSender announceTo) {
    if (!updateAvailable) {
        return;//from  w w w.  j  a v a 2  s .  c o m
    }

    new Thread(new Runnable() {

        @Override
        public void run() {

            URL url;

            try {
                url = new URL(downloadUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return;
            }

            try {
                URLConnection conn = url.openConnection();

                InputStream in = conn.getInputStream();

                File folder = Bukkit.getServer().getUpdateFolderFile();
                File out = new File(folder, fileName);

                if (!out.exists()) {
                    out.createNewFile();
                }

                FileOutputStream writer = new FileOutputStream(out);

                int read;
                byte[] buffer = new byte[1024];

                long size = conn.getContentLengthLong();
                long downloaded = 0;

                int lastPercentPrinted = 0;

                while ((read = in.read(buffer, 0, 1024)) > 0) {
                    downloaded += read;

                    writer.write(buffer, 0, read);

                    int percent = (int) ((downloaded / (double) size) * 100);
                    if (percent % 10 == 0 && announceTo != null && percent != lastPercentPrinted) {
                        announceTo.sendMessage(ChatColor.GREEN + "Progress: " + percent + "%");
                        lastPercentPrinted = percent;
                    }
                }

                writer.flush();
                writer.close();
                in.close();
                Logger.info("Downloaded " + fileName + " into update-folder " + updateFolder.getAbsolutePath()
                        + "!");

                if (announceTo != null) {
                    announceTo.sendMessage(ChatColor.DARK_GREEN + "Finished! Please " + ChatColor.UNDERLINE
                            + "restart" + ChatColor.DARK_GREEN + " to activate the version.");
                }
            } catch (IOException e) {
                Logger.severe("Error while downloading new version: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:com.opoopress.maven.plugins.plugin.downloader.ProgressURLDownloader.java

private void downloadInternal(URL url, File destination) throws IOException {
    OutputStream out = null;/*from w  w  w.j a v  a  2 s . c o m*/
    URLConnection conn;
    InputStream in = null;
    try {
        //URL url = address.toURL();
        conn = url.openConnection();

        //user agent
        final String userAgentValue = calculateUserAgent();
        conn.setRequestProperty("User-Agent", userAgentValue);

        //do not set gzip header if download zip file
        if (useGzip) {
            conn.setRequestProperty("Accept-Encoding", "gzip");
        }

        if (!useCache) {
            conn.setRequestProperty("Pragma", "no-cache");
        }

        in = conn.getInputStream();
        out = new BufferedOutputStream(new FileOutputStream(destination));

        copy(in, out);

        if (checkContentLength) {
            long contentLength = conn.getContentLengthLong();
            if (contentLength > 0 && contentLength != destination.length()) {
                throw new IllegalArgumentException("File length mismatch. expected: " + contentLength
                        + ", actual: " + destination.length());
            }
        }

        if (keepLastModified) {
            long lastModified = conn.getLastModified();
            if (lastModified > 0) {
                destination.setLastModified(lastModified);
            }
        }
    } finally {
        logMessage("");
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}