Example usage for java.net URLConnection getContentType

List of usage examples for java.net URLConnection getContentType

Introduction

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

Prototype

public String getContentType() 

Source Link

Document

Returns the value of the content-type header field.

Usage

From source file:MainClass.java

public static void saveBinaryFile(URL u) {
    int bufferLength = 128;
    try {//from  www.  jav  a 2 s .  c o  m
        URLConnection uc = u.openConnection();
        String ct = uc.getContentType();
        int contentLength = uc.getContentLength();
        if (ct.startsWith("text/") || contentLength == -1) {
            System.err.println("This is not a binary file.");
            return;
        }

        InputStream stream = uc.getInputStream();
        byte[] buffer = new byte[contentLength];
        int bytesread = 0;
        int offset = 0;
        while (bytesread >= 0) {
            bytesread = stream.read(buffer, offset, bufferLength);
            if (bytesread == -1)
                break;
            offset += bytesread;
        }
        if (offset != contentLength) {
            System.err.println("Error: Only read " + offset + " bytes");
            System.err.println("Expected " + contentLength + " bytes");
        }

        String theFile = u.getFile();
        theFile = theFile.substring(theFile.lastIndexOf('/') + 1);
        FileOutputStream fout = new FileOutputStream(theFile);
        fout.write(buffer);
    } catch (Exception e) {
        System.err.println(e);
    }
    return;
}

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

/**
 * Returns the character set of the content provided by the given
 * {@link URLConnection}./* w  w  w. ja v  a  2 s  .c  om*/
 *
 * @throws IOException if the character set cannot be determined.
 */
public static String getCharSet(URLConnection connection) throws IOException {
    String contentType = connection.getContentType();
    if (contentType != null) {
        HeaderValueParser parser = new BasicHeaderValueParser();
        HeaderElement[] values = BasicHeaderValueParser.parseElements(contentType, parser);
        if (values.length > 0) {
            NameValuePair param = values[0].getParameterByName("charset");
            if (param != null) {
                return param.getValue();
            }
        }
    }
    if (connection instanceof HttpURLConnection) {
        return HTTP.DEFAULT_CONTENT_CHARSET;
    } else {
        throw new IOException("Unabled to determine character encoding");
    }
}

From source file:com.machinepublishers.jbrowserdriver.Util.java

static String charset(URLConnection conn) {
    String charset = conn.getContentType();
    if (charset != null) {
        Matcher matcher = charsetPattern.matcher(charset);
        if (matcher.find()) {
            charset = matcher.group(1);//from  w  ww . j av a  2  s  .  co m
            if (Charset.isSupported(charset)) {
                return charset;
            }
        }
    }
    return "utf-8";
}

From source file:Main.java

/**
 * @param http/*from   w  w w.  j a  v a2  s. co m*/
 * @return extracted title of page from html.
 * @throws IOException
 */
public static String getPageTitle(String linkUrl) throws IOException {
    URL url = new URL(linkUrl);
    URLConnection conn = null;
    try {

        conn = url.openConnection();

        String type = conn.getContentType();
        if (type != null && !type.toLowerCase().contains("text/html"))
            return null; // if not html, we can't get title. Return from here.
        else {
            // read response stream
            BufferedReader bufReader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), Charset.defaultCharset()));
            int n = 0;
            int readCount = 0;
            char[] buf = new char[1024];
            StringBuilder content = new StringBuilder();

            // read till end of file or 8 times the buffer (no need to read
            // all data, assuming we get content in first 8 reads)
            while (readCount < 8 && (n = bufReader.read(buf, 0, buf.length)) != -1) {
                content.append(buf, 0, n);
                readCount++;
            }
            bufReader.close();

            // extract the title
            Matcher matcher = HTML_TITLE_PATTERN.matcher(content);
            if (matcher.find()) {

                // replace whitespaces and HTML brackets

                return matcher.group(1).replaceAll("[\\s\\<>]+", " ").trim();
            } else
                return null;
        }
    } finally {
        //any cleanup?
    }
}

From source file:eu.scape_project.pit.util.FileUtils.java

/**
 * Read file of URL into file.//from  www. j av  a2s  .c  om
 * @param url URL where the input file is located
 * @param ext 
 * @return Result file
 * @throws IOException 
 */
public static File urlToFile(URL url, String ext) throws IOException {
    File fOut = null;

    fOut = getTmpFile("fromurl", "." + ext);

    URLConnection uc = url.openConnection();
    logger.info("ContentType: " + uc.getContentType());
    InputStream in = uc.getInputStream();
    org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut);
    logger.info("File of length " + fOut.length() + " created from URL " + url.toString());

    in.close();

    return fOut;
}

From source file:UrlHelper.java

public static void downloadFile(String adresse, File dest) {
    BufferedReader reader = null;
    FileOutputStream fos = null;/*from w w w.ja  v a  2 s.  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:$.FileUtils.java

public static void urlToPath(URL url, File fOut) throws IOException {
        URLConnection uc = url.openConnection();
        logger.info("ContentType: " + uc.getContentType());
        InputStream in = uc.getInputStream();
        org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut);
        logger.info("File of length " + fOut.length() + " created from URL " + url.toString());
        in.close();/*w ww.j a v  a 2  s .  c  om*/
    }

From source file:$.FileUtils.java

/**
     * Read file of URL into file.//ww w.j  ava2  s .  c  om
     * @param url URL where the input file is located
     * @return Result file
     */
    public static File urlToFile(URL url, String ext) throws IOException {
        File fOut = null;

        fOut = getTmpFile("fromurl", "." + ext);

        URLConnection uc = url.openConnection();
        logger.info("ContentType: " + uc.getContentType());
        InputStream in = uc.getInputStream();
        org.apache.commons.io.FileUtils.copyInputStreamToFile(in, fOut);
        logger.info("File of length " + fOut.length() + " created from URL " + url.toString());

        in.close();

        return fOut;
    }

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());
    }/*  w  ww.j  ava  2  s  .  c om*/
}

From source file:de.unigoettingen.sub.commons.util.stream.StreamUtils.java

/************************************************************************************
 * get MimeType as {@link String} from given URL
 * /*from www  . j  a v a  2 s  .  co  m*/
 * @param url the url from where to get the MimeType
 * @return MimeType as {@link String}
 * @throws IOException
 ************************************************************************************/
public static String getMimeTypeFromUrl(URL url) throws IOException {

    URLConnection con = url.openConnection();
    return con.getContentType();
}