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:com.servoy.extension.MarketPlaceExtensionProvider.java

private File ws_getEXP(String extensionId, String version, IProgress progressMonitor) {
    BufferedInputStream bis = null;
    FileOutputStream fos = null;/*from  www  .  j  ava 2  s .com*/
    File outputFile = new File(destinationDir, extensionId + "_" + version + ".exp"); //$NON-NLS-1$ //$NON-NLS-2$
    try {
        destinationDir.mkdirs();
        URLConnection ws_connection = ws_getConnection(WS_ACTION_EXP, "application/binary", extensionId, //$NON-NLS-1$
                version);
        fos = new FileOutputStream(outputFile);
        bis = new BufferedInputStream(ws_connection.getInputStream());

        int total = ws_connection.getContentLength();
        if (progressMonitor != null) {
            progressMonitor.setStatusMessage("0 KB of " + getSizeString(total) + "..."); //$NON-NLS-1$//$NON-NLS-2$
            progressMonitor.start(total);
        }

        byte[] buffer = new byte[1091];
        int len;
        int downloaded = 0;
        long lastLabelUpdateTime = System.currentTimeMillis();
        while ((len = bis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
            if (progressMonitor != null) {
                downloaded += len;
                progressMonitor.worked(len);
                if (System.currentTimeMillis() - lastLabelUpdateTime > 500) {
                    // do not update label faster then 0.5 sec - it looks strange
                    progressMonitor.setStatusMessage(
                            getSizeString(downloaded) + " of " + getSizeString(total) + "..."); //$NON-NLS-1$//$NON-NLS-2$
                    lastLabelUpdateTime = System.currentTimeMillis();
                }
                if (progressMonitor.shouldCancelOperation())
                    break;
            }
        }
    } catch (Exception ex) {
        String msg = "Cannot get extension file from marketplace. Error is : " + ex.getMessage(); //$NON-NLS-1$
        Debug.error(ex);
        messages.addError(msg);
    } finally {
        Utils.closeInputStream(bis);
        Utils.closeOutputStream(fos);
    }

    if (progressMonitor != null && progressMonitor.shouldCancelOperation()) {
        FileUtils.deleteQuietly(outputFile);
        outputFile = null;
    }

    return outputFile;
}

From source file:control.services.DownloadPortraitsHttpServiceImpl.java

@Override
public int getSize(String portraitsFileName) throws FileNotFoundException {

    int size = 0;
    URL website;/* w ww.j  a v  a2 s  .c  o  m*/
    URLConnection conn = null;
    try {
        website = new URL(PROTOCOL_HOST + CLASH_HOST + PORTRAITS_PATH + portraitsFileName);
        //   website = new URL("https://www.colorado.edu/conflict/peace/download/peace.zip");

        conn = website.openConnection();
        if (conn instanceof HttpURLConnection) {
            ((HttpURLConnection) conn).setRequestMethod("HEAD");
        }
        conn.getInputStream();
        size = conn.getContentLength();
    } catch (IOException ex) {
        LOG.error(ex);
        throw new FileNotFoundException(ex.getMessage());
    } finally {
        if (conn instanceof HttpURLConnection) {
            ((HttpURLConnection) conn).disconnect();
        }
    }
    return size;
}

From source file:ch.entwine.weblounge.kernel.site.SiteServlet.java

/**
 * Tries to serve the request as a static resource from the bundle.
 * //from ww w  .jav  a2  s. co  m
 * @param request
 *          the http servlet request
 * @param response
 *          the http servlet response
 * @throws ServletException
 *           if serving the request fails
 * @throws IOException
 *           if writing the response back to the client fails
 */
protected void serviceResource(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {

    Http11ResponseType responseType = null;
    String requestPath = request.getPathInfo();

    // There is also a special set of resources that we don't want to expose
    if (isProtected(requestPath)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    String bundlePath = UrlUtils.concat("/site", requestPath);

    // Does the resource exist?
    final URL url = bundle.getResource(bundlePath);
    if (url == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // Load the resource from the bundle
    URLConnection connection = url.openConnection();
    String contentEncoding = connection.getContentEncoding();
    long contentLength = connection.getContentLength();
    long lastModified = connection.getLastModified();

    if (contentLength <= 0) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // final Resource resource = Resource.newResource(url);
    // if (!resource.exists()) {
    // response.sendError(HttpServletResponse.SC_NOT_FOUND);
    // return;
    // }

    // We don't allow directory listings
    // if (resource.isDirectory()) {
    // response.sendError(HttpServletResponse.SC_FORBIDDEN);
    // return;
    // }

    String mimeType = tika.detect(bundlePath);

    // Try to get mime type and content encoding from resource
    if (mimeType == null)
        mimeType = connection.getContentType();

    if (mimeType != null) {
        if (contentEncoding != null)
            mimeType += ";" + contentEncoding;
        response.setContentType(mimeType);
    }

    // Send the response back to the client
    InputStream is = connection.getInputStream();
    // InputStream is = resource.getInputStream();
    try {
        logger.debug("Serving {}", url);
        responseType = Http11ProtocolHandler.analyzeRequest(request, lastModified,
                Times.MS_PER_DAY + System.currentTimeMillis(), contentLength);
        if (!Http11ProtocolHandler.generateResponse(response, responseType, is)) {
            logger.warn("I/O error while generating content from {}", url);
        }
    } finally {
        IOUtils.closeQuietly(is);
    }

}

From source file:org.liberty.android.fantastischmemo.downloader.DownloaderAnyMemo.java

private void downloadDatabase(final DownloadItem di) throws Exception {
    String filename = di.getExtras("filename");
    if (filename == null) {
        throw new Exception("Could not get filename");
    }/*from w  w w. jav  a2s.c  o  m*/

    String sdpath = AMEnv.DEFAULT_ROOT_PATH;
    File outFile = new File(sdpath + filename);
    mHandler.post(new Runnable() {
        public void run() {
            mProgressDialog = new ProgressDialog(DownloaderAnyMemo.this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMessage(getString(R.string.loading_downloading));
            mProgressDialog.show();

        }
    });
    try {
        OutputStream out;
        if (outFile.exists()) {
            throw new IOException("Database already exist!");
        }
        try {
            outFile.createNewFile();
            out = new FileOutputStream(outFile);

            URL myURL = new URL(di.getAddress());
            Log.v(TAG, "URL IS: " + myURL);
            URLConnection ucon = myURL.openConnection();
            final int fileSize = ucon.getContentLength();
            mHandler.post(new Runnable() {
                public void run() {
                    mProgressDialog.setMax(fileSize);
                }
            });

            byte[] buf = new byte[BUFFER_SIZE];

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, BUFFER_SIZE);
            Runnable increaseProgress = new Runnable() {
                public void run() {
                    mProgressDialog.setProgress(mDownloadProgress);
                }
            };
            int len = 0;
            int lenSum = 0;
            while ((len = bis.read(buf)) != -1) {
                out.write(buf, 0, len);
                lenSum += len;
                if (lenSum > fileSize / 50) {
                    /* This is tricky.
                     * The UI thread can not be updated too often.
                     * So we update it only 50 times
                     */
                    mDownloadProgress += lenSum;
                    lenSum = 0;
                    mHandler.post(increaseProgress);
                }
            }
            out.close();
            is.close();
            /* Uncompress the zip file that contains images */
            if (filename.endsWith(".zip")) {
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgressDialog.setProgress(fileSize);
                        mProgressDialog.setMessage(getString(R.string.downloader_extract_zip));
                    }
                });
                AMZipUtils.unZipFile(outFile, new File(AMEnv.DEFAULT_ROOT_PATH));
                /* Delete the zip file if it is successfully decompressed */
                outFile.delete();

            }
            /* We do not check ttf file as db */
            if (!filename.toLowerCase().endsWith(".ttf")) {
                /* Check if the db is correct */
                filename = filename.replace(".zip", ".db");
                String fullpath = sdpath + filename;
                AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(DownloaderAnyMemo.this,
                        fullpath);
                try {
                    long count = helper.getCardDao().getTotalCount(null);
                    if (count <= 0L) {
                        throw new RuntimeException("Downloaded empty db.");
                    }
                } finally {
                    AnyMemoDBOpenHelperManager.releaseHelper(helper);
                }

                /* Add downloaded item to file list */
                recentListUtil.addToRecentList(fullpath);
            }
        } catch (Exception e) {
            if (outFile.exists()) {
                outFile.delete();
            }
            throw new Exception(e);
        }

    } catch (Exception e) {
        Log.e(TAG, "Error downloading", e);
        throw new Exception(e);
    } finally {
        mHandler.post(new Runnable() {
            public void run() {
                mProgressDialog.dismiss();
            }
        });
    }

}

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

/**
 * @see wicket.util.watch.IModifiable#lastModifiedTime()
 * @return The last time this resource was modified
 */// w w  w  .j  av  a  2s.  c  o  m
public Time lastModifiedTime() {
    if (file != null) {
        long lastModified = file.lastModified();
        if (lastModified != this.lastModified) {
            this.lastModified = lastModified;
            this.contentLength = (int) file.length();
        }
    } else {
        URLConnection urlConnection = null;
        try {
            urlConnection = url.openConnection();

            // update the last modified time.
            long lastModified = urlConnection.getLastModified();
            if (lastModified != this.lastModified) {
                this.lastModified = lastModified;
                this.contentLength = urlConnection.getContentLength();
            }
        } catch (IOException e) {
            log.error("getLastModified for " + url + " failed: " + e.getMessage());
        } finally {
            // if applicable, disconnect
            if (urlConnection != null) {
                if (urlConnection instanceof HttpURLConnection) {
                    ((HttpURLConnection) urlConnection).disconnect();
                } else {
                    try {
                        urlConnection.getInputStream().close();
                    } catch (Exception ex) {
                        // ignore
                    }
                }
            }
        }
    }
    return Time.milliseconds(lastModified);
}

From source file:org.sbs.util.download.MultiThreadDownload.java

/**
 * //w ww .ja v  a2  s.  com
 * @param url
 * @param path
 * @param fileName
 * @return
 * @throws DownLoadException
 */
@SuppressWarnings("unchecked")
public File downLoad(final URL url, String path, String fileName) throws DownLoadException {
    // ??
    this.downLen = -1;
    this.contentLen = 0;
    this.date = new Date();
    this.finished = false;
    try {
        URLConnection con = url.openConnection();
        //?
        this.contentLen = con.getContentLength();
        //??
        if (StringUtils.isBlank(fileName)) {
            fileName = StringUtils.substringAfterLast(url.getPath(), "/");
        }
        //
        File _path = new File(path);
        if (!_path.exists()) {
            _path.mkdirs();
        }
        final File file = new File(path + File.separator + fileName);
        if (file.exists())
            file.delete();
        if (this.threadNum == 0 && this.blockSize > 0) {
            this.threadNum = (int) (contentLen / blockSize);
            if (this.threadNum == 0) {
                this.threadNum = 1;
            }
        }
        long subLen = contentLen / threadNum;
        List<Future<DownLoadBean>> result = Lists.newArrayList();
        for (int i = 0; i < threadNum; i++) {
            final int pos = (int) (i * subLen);
            final int end = (int) ((i + 1) * subLen) - 1;
            final int current = pos;
            Future<DownLoadBean> f = (Future<DownLoadBean>) pool.submit(new Callable<DownLoadBean>() {
                int $pos = pos;
                int $end = end;
                int $current = current;

                @Override
                public DownLoadBean call() throws Exception {
                    //buff
                    BufferedInputStream bis = null;
                    RandomAccessFile fos = null;
                    byte[] buf = new byte[BUFFER_SIZE];
                    URLConnection con = null;
                    try {
                        con = url.openConnection();
                        con.setAllowUserInteraction(true);
                        //???startPosendPos
                        con.setRequestProperty("Range", "bytes=" + $pos + "-" + $end);
                        fos = new RandomAccessFile(file, "rw");
                        //startPos
                        fos.seek($pos);
                        //????curPos???endPos
                        //endPos?
                        bis = new BufferedInputStream(con.getInputStream());
                        while ($current < $end) {
                            int len = bis.read(buf, 0, BUFFER_SIZE);
                            if (len == -1) {
                                break;
                            }
                            fos.write(buf, 0, len);
                            $current = $current + len;
                            if ($current - 1 > $end) {
                                throw new DownLoadException(
                                        "????");
                            }
                            addLen(len);
                        }
                        bis.close();
                        fos.close();
                    } catch (IOException ex) {
                        /* ?????
                        StringBuffer sb = new StringBuffer();
                        sb.append($pos).append("\t").append($current).append("\t").append($end).append("\n");
                         FileUtils.write(new File(file.getAbsolutePath()+".pos"), sb, true);
                         */
                        throw new RuntimeException(ex);
                    }
                    log.debug(this.hashCode() + ":??[" + $pos + "," + $end + "]");
                    return new DownLoadBean($pos, $end, $current);
                }
            });
            result.add(f);
        }
        Long resultTotal = 0L;
        for (Future<DownLoadBean> f : result) {
            DownLoadBean dInfo = f.get();
            resultTotal += dInfo.getCurrent() - dInfo.getPos();
        }
        // ?
        if (contentLen > resultTotal + 1) {
            // ???
            FileUtils.write(new File(down_error_log_file), url.toString() + "\n", true);
            throw new DownLoadException("?");
        } else {
            finished = true;
            return file;
        }
    } catch (IOException | InterruptedException | ExecutionException e) {
        // 
        try {
            FileUtils.write(new File(down_error_log_file), url.toString() + "\n", true);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    }
    return null;
}

From source file:org.jab.docsearch.utils.NetUtils.java

/**
 * Gets URL size (content)/*ww  w . j  av a  2 s  .  c  o  m*/
 *
 * @param url  URL for connect
 * @return     size in bytes of a url or 0 if broken or timed out connection
 */
public long getURLSize(final String url) {
    try {
        URL tmpURL = new URL(url);
        URLConnection conn = tmpURL.openConnection();

        // set connection parameter
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", USER_AGENT);

        // connect
        conn.connect();

        long contentLength = conn.getContentLength();

        if (logger.isDebugEnabled()) {
            logger.debug("getURLSize() content lentgh=" + contentLength + " of URL='" + url + "'");
        }

        return contentLength;
    } catch (IOException ioe) {
        logger.error("getURLSize() failed for URL='" + url + "'", ioe);
        return 0;
    }
}

From source file:org.liberty.android.fantastischmemopro.downloader.DownloaderAnyMemo.java

private void downloadDatabase(final DownloadItem di) throws Exception {
    String filename = di.getExtras("filename");
    if (filename == null) {
        throw new Exception("Could not get filename");
    }//from  ww w. j  a  v a  2 s  .com

    String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath()
            + getString(R.string.default_dir);
    File outFile = new File(sdpath + filename);
    mHandler.post(new Runnable() {
        public void run() {
            mProgressDialog = new ProgressDialog(DownloaderAnyMemo.this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMessage(getString(R.string.loading_downloading));
            mProgressDialog.show();

        }
    });
    try {
        OutputStream out;
        if (outFile.exists()) {
            throw new IOException("Database already exist!");
        }
        try {
            outFile.createNewFile();
            out = new FileOutputStream(outFile);

            URL myURL = new URL(di.getAddress());
            Log.v(TAG, "URL IS: " + myURL);
            URLConnection ucon = myURL.openConnection();
            final int fileSize = ucon.getContentLength();
            mHandler.post(new Runnable() {
                public void run() {
                    mProgressDialog.setMax(fileSize);
                }
            });

            byte[] buf = new byte[8192];

            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, 8192);
            Runnable increaseProgress = new Runnable() {
                public void run() {
                    mProgressDialog.setProgress(mDownloadProgress);
                }
            };
            int len = 0;
            int lenSum = 0;
            while ((len = bis.read(buf)) != -1) {
                out.write(buf, 0, len);
                lenSum += len;
                if (lenSum > fileSize / 50) {
                    /* This is tricky.
                     * The UI thread can not be updated too often.
                     * So we update it only 50 times
                     */
                    mDownloadProgress += lenSum;
                    lenSum = 0;
                    mHandler.post(increaseProgress);
                }
            }
            out.close();
            is.close();
            /* Uncompress the zip file that contains images */
            if (filename.endsWith(".zip")) {
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgressDialog.setProgress(fileSize);
                        mProgressDialog.setMessage(getString(R.string.downloader_extract_zip));
                    }
                });

                BufferedOutputStream dest = null;
                BufferedInputStream ins = null;
                ZipEntry entry;
                ZipFile zipfile = new ZipFile(outFile);
                Enumeration<?> e = zipfile.entries();
                while (e.hasMoreElements()) {
                    entry = (ZipEntry) e.nextElement();
                    Log.v(TAG, "Extracting: " + entry);
                    if (entry.isDirectory()) {
                        new File(sdpath + "/" + entry.getName()).mkdir();
                    } else {
                        ins = new BufferedInputStream(zipfile.getInputStream(entry), 8192);
                        int count;
                        byte data[] = new byte[8192];
                        FileOutputStream fos = new FileOutputStream(sdpath + "/" + entry.getName());
                        dest = new BufferedOutputStream(fos, 8192);
                        while ((count = ins.read(data, 0, 8192)) != -1) {
                            dest.write(data, 0, count);
                        }
                        dest.flush();
                        dest.close();
                        ins.close();
                    }
                }
                /* Delete the zip file if it is successfully decompressed */
                outFile.delete();
            }
            /* We do not check ttf file as db */
            if (!filename.toLowerCase().endsWith(".ttf")) {
                /* Check if the db is correct */
                filename = filename.replace(".zip", ".db");
                DatabaseHelper dh = new DatabaseHelper(DownloaderAnyMemo.this, sdpath, filename);
                dh.close();
            }
        } catch (Exception e) {
            if (outFile.exists()) {
                outFile.delete();
            }
            throw new Exception(e);
        }

    } catch (Exception e) {
        Log.e(TAG, "Error downloading", e);
        throw new Exception(e);
    } finally {
        mHandler.post(new Runnable() {
            public void run() {
                mProgressDialog.dismiss();
            }
        });
    }

}

From source file:org.jahia.services.render.scripting.bundle.BundleSourceResourceResolver.java

private boolean urlExists(URL url) {
    // Try a URL connection content-length header...
    try {// w w  w .  j  av  a  2  s. c  o m
        URLConnection con = null;
        con = url.openConnection();
        con.setUseCaches(false);
        HttpURLConnection httpCon = (con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
        if (httpCon != null) {
            httpCon.setRequestMethod("HEAD");
            if (httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return true;
            }
        }
        if (con.getContentLength() > 0) {
            return true;
        }
        if (httpCon != null) {
            // no HTTP OK status, and no content-length header: give up
            httpCon.disconnect();
            return false;
        } else {
            // Fall back to stream existence: can we open the stream?
            InputStream is = getInputStream(url);
            is.close();
            return true;
        }
    } catch (IOException e) {
        logger.debug("Testing existence of resource " + url, e);
        return false;
    }
}

From source file:argendata.web.controller.DatasetController.java

private int getResourceLength(PreDataset ds) {
    // String length = null;

    int len = 0;/*ww w.j  ava2 s.c  o  m*/

    String format = null;
    if (ds.getDistribution() != null && ds.getFormat() != null) {
        format = ds.getFormat().toLowerCase();
    }

    if (format != null && (format.endsWith("doc") || format.endsWith("docx") || format.endsWith("xls")
            || format.endsWith("xlsx") || format.endsWith("ppt") || format.endsWith("pptx")
            || format.endsWith("pps") || format.endsWith("odt") || format.endsWith("ods")
            || format.endsWith("odp") || format.endsWith("swx") || format.endsWith("sxi")
            || format.endsWith("wpd") || format.endsWith("pdf") || format.endsWith("rtf")
            || format.endsWith("txt") || format.endsWith("csv") || format.endsWith("tsv"))) {

        // me conecto y pido el content-length
        URL url;
        try {
            url = new URL(ds.getAccessURL());
            URLConnection conn = url.openConnection();

            len = conn.getContentLength();

            if (len != -1) {

                Integer l = (len / 1024);
                // length= l.toString()+"KB";
                len = l;
            } else {
                // length = "desconocido";
                len = 0;
            }

        } catch (MalformedURLException e) {
            logger.error("No se pudo obtener los headers del recurso " + ds.getAccessURL());
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error("No se pudo obtener los headers del recurso " + ds.getAccessURL());
            logger.error(e.getMessage(), e);
        }

    } else {
        // length = "-";
        len = 0;
    }

    return len;
}