1 18 19 package sync4j.syncclient.common; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 import java.net.MalformedURLException ; 26 27 34 35 public class HttpTools { 36 37 38 46 public static byte[] downloadPackage(String fileUrl) 47 throws IOException { 48 byte[] content = null; 49 50 int size = -1; 51 52 URL url = new URL (fileUrl); 53 54 URLConnection urlConn = url.openConnection(); 55 url.openStream(); 56 size = urlConn.getContentLength(); 57 content = new byte[size]; 58 59 InputStream iStream = url.openStream(); 60 int read = -1; 61 int offset = 0; 62 int toRead = size; 63 while ( (read = iStream.read(content, offset,toRead)) != -1) { 64 toRead = toRead - read; 65 offset = offset + read; 66 } 67 iStream.close(); 68 return content; 69 70 } 71 72 79 public static boolean isHTTPURL(String url) { 80 if (!(url.startsWith("http://") || url.startsWith("https://"))) { 81 return false; 82 } 83 84 try { 85 new URL (url); 86 return true; 87 } catch (MalformedURLException e) {} 88 89 return false; 90 } 91 92 93 } | Popular Tags |