List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:org.xenmaster.site.XenMasterSite.java
public static InputStream getFileAsStream(String file) throws IOException { URL url = new URL(XENMASTER_ORG_DOWNLOAD + "/" + file); URLConnection uc = url.openConnection(); uc.connect(); InputStream is = uc.getInputStream(); return is;//from ww w. j ava2 s . c o m }
From source file:Main.java
public static String getURLContent(String urlStr) throws Exception { URL url = new URL(urlStr); URLConnection connection = url.openConnection(); connection.setDoOutput(true);//ww w .j a v a 2s . c o m connection.connect(); OutputStream ous = connection.getOutputStream(); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(ous)); bw.write("index.htm"); bw.flush(); bw.close(); printRequestHeaders(connection); InputStream ins = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(ins)); StringBuffer sb = new StringBuffer(); String msg = null; while ((msg = br.readLine()) != null) { sb.append(msg); sb.append("\n"); // Append a new line } br.close(); return sb.toString(); }
From source file:Main.java
static private BitmapDrawable bitmapDrawableFromURL(Resources resources, String str_url, boolean scale, int w, int h) {/*from ww w .j av a 2 s.c om*/ if (str_url == null || str_url.length() <= 0) return null; BitmapDrawable thumb = null; try { URL url = new URL(str_url); URLConnection connection = url.openConnection(); connection.connect(); InputStream stream = connection.getInputStream(); BufferedInputStream data = new BufferedInputStream(stream); Bitmap bitmap = BitmapFactory.decodeStream(data); if (bitmap != null) { if (scale) thumb = new BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, w, h, true)); else thumb = new BitmapDrawable(resources, bitmap); } } catch (MalformedURLException e) { //e.printStackTrace(); } catch (IOException e) { //e.printStackTrace(); } return thumb; }
From source file:Main.java
public static Bitmap getBitmapAndScale(String url, int requiredSize) { Bitmap bm = null;//ww w .java2 s . c o m try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, o); // The new size we want to scale to // Find the correct scale value. It should be the power of 2. int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; bm = BitmapFactory.decodeStream(is, null, o2); return bm; } catch (Exception e) { e.printStackTrace(); } finally { return bm; } }
From source file:Main.java
public static File downloadFile(String urlstr, File saveFile) { try {//from w w w. j a va 2 s . com URL url = new URL(urlstr);// cause speed low. URLConnection con = url.openConnection(); con.setDoInput(true); con.connect(); InputStream ins = con.getInputStream(); final int bufsize = 102400; byte[] buffer = new byte[bufsize]; int len = -1; FileOutputStream bos = new FileOutputStream(saveFile); while ((len = ins.read(buffer)) != -1) { bos.write(buffer, 0, len); } ins.close(); bos.close(); return saveFile; } catch (Error e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:InsertClobToMySqlServlet.java
public static String getClobsContentAsString(String urlAsString) throws Exception { InputStream content = null;//w ww . ja va2 s . co m try { URL url = new URL(urlAsString); URLConnection urlConn = url.openConnection(); urlConn.connect(); content = urlConn.getInputStream(); int BUFFER_SIZE = 1024; ByteArrayOutputStream output = new ByteArrayOutputStream(); int length; byte[] buffer = new byte[BUFFER_SIZE]; while ((length = content.read(buffer)) != -1) { output.write(buffer, 0, length); } return new String(output.toByteArray()); } finally { content.close(); } }
From source file:UpdateMySqlClobServlet.java
public static String getClobsContentAsString(String urlAsString) throws Exception { InputStream content = null;//from w w w . j a v a 2 s . c o m try { java.net.URL url = new java.net.URL(urlAsString); java.net.URLConnection urlConn = url.openConnection(); urlConn.connect(); content = urlConn.getInputStream(); int BUFFER_SIZE = 1024; ByteArrayOutputStream output = new ByteArrayOutputStream(); int length; byte[] buffer = new byte[BUFFER_SIZE]; while ((length = content.read(buffer)) != -1) { output.write(buffer, 0, length); } return new String(output.toByteArray()); } finally { content.close(); } }
From source file:gdv.xport.util.URLReader.java
private static String read(final URLConnection connection) throws IOException { connection.connect(); InputStream istream = connection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(istream)); try {/*from w w w.j a v a 2 s . c om*/ StringBuilder sbuf = new StringBuilder(); while (in.ready()) { String line = in.readLine(); LOG.debug(line); sbuf.append(line); sbuf.append("\n"); } return sbuf.toString().trim(); } finally { in.close(); istream.close(); } }
From source file:org.enderstone.server.uuid.ServerRequest.java
public static JSONObject parseDataFromURL(String connect) throws IOException { URL url = new URL(connect); URLConnection uc = url.openConnection(); uc.setConnectTimeout(5000);//from w ww . j ava2 s.c om uc.setReadTimeout(5000); uc.connect(); String encoding = uc.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; try (Scanner scanner = new Scanner(uc.getInputStream(), encoding)) { scanner.useDelimiter("\\A"); return new JSONObject(scanner.next()); } catch (NoSuchElementException noskin) { return new JSONObject(); } }
From source file:Main.java
public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) { if (null != bitmapUrl) { InputStream inputStream = null; try {/* ww w . ja va2 s.co m*/ URL url = new URL(bitmapUrl); URLConnection connection = url.openConnection(); connection.connect(); inputStream = connection.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read; while ((read = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, read); } inputStream.close(); byteArrayOutputStream.close(); buffer = byteArrayOutputStream.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options); int maxSize = Math.max(options.outWidth, options.outHeight); float newImageScale = 1f; if (-1 != imageSize) { newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density); } options.inJustDecodeBounds = false; options.inSampleSize = Math.round(newImageScale); return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options); } catch (Throwable e) { // pass } finally { if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { // pass } inputStream = null; } System.gc(); } } return null; }