List of usage examples for java.net HttpURLConnection getContentLength
public int getContentLength()
From source file:Main.java
/** * @param url/*from w w w. j av a2 s .c o m*/ * @return */ public static Bitmap getBitMapFromURL(URL url) { Bitmap mBitmap = null; try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); conn.getContentLength(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); mBitmap = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch (Exception e) { Log.e("Exception in MapScrollActivity.getBitmapFromURL", "" + e.getMessage()); e.printStackTrace(); } return mBitmap; }
From source file:net.daporkchop.porkbot.util.HTTPUtils.java
/** * Performs a GET request to the specified URL and returns the result. * <p/>//from w w w. ja v a 2 s . c o m * The response will be parsed as UTF-8. * If the server returns an error but still provides a body, the body will be returned as normal. * If the server returns an error without any body, a relevant {@link java.io.IOException} will be thrown. * * @param url URL to submit the GET request to * @return Raw text response from the server * @throws IOException The request was not successful */ public static String performGetRequest(@NonNull URL url, int max) throws IOException { final HttpURLConnection connection = createUrlConnection(url); if (connection.getContentLength() > max) { throw new IOException("Content too big!"); } return sendRequest(connection); }
From source file:Main.java
public static byte[] getBytes(String url) { try {//from w w w. java 2 s . c o m URL u = new URL(url); HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Charset", "UTF-8"); InputStream is = connection.getInputStream(); int contentLength = connection.getContentLength(); byte[] data = new byte[contentLength]; int alreadyRead = 0; int len = 0; while (alreadyRead < contentLength) { len = is.read(data, alreadyRead, contentLength - alreadyRead); alreadyRead += len; } is.close(); return data; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static boolean downloadFile(File file, URL url) throws IOException { BufferedInputStream bis = null; BufferedOutputStream bos = null; HttpURLConnection conn = null; try {// www. ja v a 2 s. com conn = (HttpURLConnection) url.openConnection(); bis = new BufferedInputStream(conn.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(file)); int contentLength = conn.getContentLength(); byte[] buffer = new byte[BUFFER_SIZE]; int read = 0; int count = 0; while ((read = bis.read(buffer)) != -1) { bos.write(buffer, 0, read); count += read; } if (count < contentLength) return false; int responseCode = conn.getResponseCode(); if (responseCode / 100 != 2) { // error return false; } // finished return true; } finally { try { bis.close(); bos.close(); conn.disconnect(); } catch (Exception e) { } } }
From source file:Main.java
public static byte[] retrieveImageData_fromUrl(String imageUrl) throws IOException { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK"); // determine the image size and allocate a buffer int fileSize = connection.getContentLength(); byte[] imageData = new byte[fileSize]; // download the file Log.d(TAG, "fetching image " + imageUrl + " (" + fileSize + ")"); if (fileSize > 0) { BufferedInputStream istream = new BufferedInputStream(connection.getInputStream()); int bytesRead = 0; int offset = 0; while (bytesRead != -1 && offset < fileSize) { bytesRead = istream.read(imageData, offset, fileSize - offset); offset += bytesRead;//from w ww. j a v a2s . com } istream.close(); } else Log.d(TAG, "fileSize is 0! skipping"); // clean up connection.disconnect(); return imageData; }
From source file:com.qhn.bhne.xhmusic.utils.MyUtils.java
public static BufferedInputStream getInputStream(String musicPicRes) { URL iconUrl = null;/* ww w. j a va 2 s . c o m*/ try { iconUrl = new URL(musicPicRes); URLConnection conn = iconUrl.openConnection(); HttpURLConnection http = (HttpURLConnection) conn; int length = http.getContentLength(); conn.connect(); // ?? BufferedInputStream bis = new BufferedInputStream(conn.getInputStream(), length); return bis; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.teleca.jamendo.util.download.DownloadTask.java
public static Boolean downloadFile(DownloadJob job) throws IOException { // TODO rewrite to apache client PlaylistEntry mPlaylistEntry = job.getPlaylistEntry(); String mDestination = job.getDestination(); URL u = new URL(mPlaylistEntry.getTrack().getStream()); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true);//from w ww. j ava2 s .c o m c.connect(); job.setTotalSize(c.getContentLength()); Log.i(JamendoApplication.TAG, "creating file"); String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination); String fileName = DownloadHelper.getFileName(mPlaylistEntry, job.getFormat()); try { // Create multiple directory boolean success = (new File(path)).mkdirs(); if (success) { Log.i(JamendoApplication.TAG, "Directory: " + path + " created"); } } catch (Exception e) {//Catch exception if any Log.e(JamendoApplication.TAG, "Error creating folder", e); return false; } FileOutputStream f = new FileOutputStream(new File(path, fileName)); InputStream in = c.getInputStream(); if (in == null) { // When InputStream is a NULL return false; } byte[] buffer = new byte[1024]; int lenght = 0; while ((lenght = in.read(buffer)) > 0) { f.write(buffer, 0, lenght); job.setDownloadedSize(job.getDownloadedSize() + lenght); } f.close(); downloadCover(job); return true; }
From source file:com.zack6849.alphabot.api.Utils.java
public static String getTitle(String link) { String response = ""; try {//from w w w . j a va 2 s . c o m HttpURLConnection conn = (HttpURLConnection) new URL(link).openConnection(); conn.addRequestProperty("User-Agent", USER_AGENT); String type = conn.getContentType(); int length = conn.getContentLength() / 1024; response = String.format("HTTP %s: %s", conn.getResponseCode(), conn.getResponseMessage()); String info; if (type.contains("text") || type.contains("application")) { Document doc = Jsoup.connect(link).userAgent(USER_AGENT).followRedirects(true).get(); String title = doc.title() == null || doc.title().isEmpty() ? "No title found!" : doc.title(); info = String.format("%s - (Content Type: %s Size: %skb)", title, type, length); return info; } info = String.format("Content Type: %s Size: %skb", type, length); return info; } catch (IOException ex) { if (ex.getMessage().contains("UnknownHostException")) { return Colors.RED + "Unknown hostname!"; } return response.isEmpty() ? Colors.RED + "An error occured" : response; } }
From source file:org.kontalk.upload.KontalkBoxUploadConnection.java
public static String responseToString(HttpURLConnection conn, final Charset charset) throws IOException { final InputStream instream = conn.getInputStream(); if (instream == null) { return null; }/*w w w . j av a2 s . co m*/ try { int i = conn.getContentLength(); if (i < 0) { i = 4096; } final Reader reader = new InputStreamReader(instream, charset); final StringBuilder buffer = new StringBuilder(i); final char[] tmp = new char[1024]; int l; while ((l = reader.read(tmp)) != -1) { buffer.append(tmp, 0, l); } return buffer.toString(); } finally { instream.close(); } }
From source file:Main.java
public static boolean hasActiveInternetConnection(Context context) { if (isNetworkAvailable(context)) { try {/*w ww . j a va 2 s . c om*/ HttpURLConnection connection = (HttpURLConnection) (new URL( "http://clients3.google.com/generate_204")).openConnection(); connection.setRequestProperty("User-Agent", "Test"); connection.setRequestProperty("Connection", "close"); connection.setReadTimeout(1500); connection.connect(); return (connection.getResponseCode() == 204 && connection.getContentLength() == 0); } catch (IOException e) { Log.e("ERROR", "Error checking internet connection"); } } else Log.e("ERROR", "No network available"); return false; }