public void run() { try { Thread.sleep(10); byte[] buf = getBuf(); URL url = new URL("http://127.0.0.1:" + port + "/test"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestMethod("POST"); con.setRequestProperty( "Content-Type", "Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\""); OutputStream out = con.getOutputStream(); out.write(buf); out.close(); InputStream in = con.getInputStream(); byte[] newBuf = readFully(in); in.close(); if (buf.length != newBuf.length) { System.out.println("Doesn't match"); error = true; } synchronized (lock) { ++received; if ((received % 1000) == 0) { System.out.println("Received=" + received); } } } catch (Exception e) { // e.printStackTrace(); System.out.print("."); error = true; } }
public InputStream getResourceAsStream(String path) { URL url = getResource(path); if (url == null) return null; try { return url.openStream(); } catch (IOException ioe) { throw new RuntimeException("can't getResourceAsStream [" + path + "]", ioe); } }
/** * 以阻塞的方式立即下载一个文件,该方法会覆盖已经存在的文件 * * @param task * @return "ok" if download success (else return errmessage); */ static String download(DownloadTask task) { if (!openedStatus && show) openStatus(); URL url; HttpURLConnection conn; try { url = new URL(task.getOrigin()); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/" + Math.random()); if ("www.imgjav.com".equals(url.getHost())) { // 该网站需要带cookie请求 conn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"); // conn.setRequestProperty("Cookie", "__cfduid=d219ea333c7a9b5743b572697b631925a1446093229; // cf_clearance=6ae62d843f5d09acf393f9e4eb130d9366840c82-1446093303-28800"); conn.setRequestProperty( "Cookie", "__cfduid=d6ee846b378bb7d5d173a05541f8a2b6a1446090548; cf_clearance=ea10e8db31f8b6ee51570b118dd89b7e616d7b62-1446099714-28800"); conn.setRequestProperty("Host", "www.imgjav.com"); } Path directory = Paths.get(task.getDest()).getParent(); if (!Files.exists(directory)) Files.createDirectories(directory); } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } try (InputStream is = conn.getInputStream(); BufferedInputStream in = new BufferedInputStream(is); FileOutputStream fos = new FileOutputStream(task.getDest()); OutputStream out = new BufferedOutputStream(fos); ) { int length = conn.getContentLength(); if (length < 1) throw new IOException("length<1"); byte[] binary = new byte[length]; byte[] buff = new byte[65536]; int len; int index = 0; while ((len = in.read(buff)) != -1) { System.arraycopy(buff, 0, binary, index, len); index += len; allLen += len; // allLen有线程安全的问题 ,可能会不正确。无需精确数据,所以不同步了 task.setReceivePercent(String.format("%.2f", ((float) index / length) * 100) + "%"); } out.write(binary); } catch (IOException e) { e.printStackTrace(); return e.getMessage(); } return "ok"; }
/** * Resolve IGFS profiler logs directory. * * @param igfs IGFS instance to resolve logs dir for. * @return {@link Path} to log dir or {@code null} if not found. * @throws IgniteCheckedException if failed to resolve. */ public static Path resolveIgfsProfilerLogsDir(IgniteFileSystem igfs) throws IgniteCheckedException { String logsDir; if (igfs instanceof IgfsEx) logsDir = ((IgfsEx) igfs).clientLogDirectory(); else if (igfs == null) throw new IgniteCheckedException( "Failed to get profiler log folder (IGFS instance not found)"); else throw new IgniteCheckedException( "Failed to get profiler log folder (unexpected IGFS instance type)"); URL logsDirUrl = U.resolveIgniteUrl(logsDir != null ? logsDir : DFLT_IGFS_LOG_DIR); return logsDirUrl != null ? new File(logsDirUrl.getPath()).toPath() : null; }
public synchronized void logUnavailableHost(URL url) { if (this.offlineMode) return; if (url == null) { String message = Logging.getMessage("nullValue.URLIsNull"); Logging.error(message); throw new IllegalArgumentException(message); } String hostName = url.getHost(); HostInfo hi = this.hostMap.get(hostName); if (hi != null) { if (!hi.isUnavailable()) { hi.logCount.incrementAndGet(); if (hi.isUnavailable()) // host just became unavailable this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url); } hi.lastLogTime.set(System.currentTimeMillis()); } else { hi = new HostInfo(this.attemptLimit.get(), this.tryAgainInterval.get()); hi.logCount.set(1); if (hi.isUnavailable()) // the attempt limit may be as low as 1, so handle that case here this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url); this.hostMap.put(hostName, hi); } this.lastUnavailableLogTime.set(System.currentTimeMillis()); }
public void run() { try { URL url = new URL(protocol + "://localhost:" + port + "/test1/" + f); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); if (urlc instanceof HttpsURLConnection) { HttpsURLConnection urlcs = (HttpsURLConnection) urlc; urlcs.setHostnameVerifier( new HostnameVerifier() { public boolean verify(String s, SSLSession s1) { return true; } }); urlcs.setSSLSocketFactory(ctx.getSocketFactory()); } byte[] buf = new byte[4096]; if (fixedLen) { urlc.setRequestProperty("XFixed", "yes"); } InputStream is = urlc.getInputStream(); File temp = File.createTempFile("Test1", null); temp.deleteOnExit(); OutputStream fout = new BufferedOutputStream(new FileOutputStream(temp)); int c, count = 0; while ((c = is.read(buf)) != -1) { count += c; fout.write(buf, 0, c); } is.close(); fout.close(); if (count != size) { throw new RuntimeException("wrong amount of data returned"); } String orig = root + "/" + f; compare(new File(orig), temp); temp.delete(); } catch (Exception e) { e.printStackTrace(); fail = true; } }
/** * Discover WS device on the local network with specified filter * * @param regexpProtocol url protocol matching regexp like "^http$", might be empty "" * @param regexpPath url path matching regexp like "onvif", might be empty "" * @return list of unique device urls filtered */ public static Collection<URL> discoverWsDevicesAsUrls(String regexpProtocol, String regexpPath) { final Collection<URL> urls = new TreeSet<>( new Comparator<URL>() { public int compare(URL o1, URL o2) { return o1.toString().compareTo(o2.toString()); } }); for (String key : discoverWsDevices()) { try { final URL url = new URL(key); boolean ok = true; if (regexpProtocol.length() > 0 && !url.getProtocol().matches(regexpProtocol)) ok = false; if (regexpPath.length() > 0 && !url.getPath().matches(regexpPath)) ok = false; if (ok) urls.add(url); } catch (MalformedURLException e) { e.printStackTrace(); } } return urls; }
/** * Determine if a host is reachable by attempting to resolve the host name, and then attempting to * open a connection. * * @param hostName Name of the host to connect to. * @return {@code true} if a the host is reachable, {@code false} if the host name cannot be * resolved, or if opening a connection to the host fails. */ protected static boolean isHostReachable(String hostName) { try { // Assume host is unreachable if we can't get its dns entry without getting an exception //noinspection ResultOfMethodCallIgnored InetAddress.getByName(hostName); } catch (UnknownHostException e) { String message = Logging.getMessage("NetworkStatus.UnreachableTestHost", hostName); Logging.verbose(message); return false; } catch (Exception e) { String message = Logging.getMessage("NetworkStatus.ExceptionTestingHost", hostName); Logging.verbose(message); return false; } // Was able to get internet address, but host still might not be reachable because the address // might have been // cached earlier when it was available. So need to try something else. URLConnection connection = null; try { URL url = new URL("http://" + hostName); Proxy proxy = WWIO.configureProxy(); if (proxy != null) connection = url.openConnection(proxy); else connection = url.openConnection(); connection.setConnectTimeout(2000); connection.setReadTimeout(2000); String ct = connection.getContentType(); if (ct != null) return true; } catch (IOException e) { String message = Logging.getMessage("NetworkStatus.ExceptionTestingHost", hostName); Logging.info(message); } finally { if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection).disconnect(); } return false; }
public synchronized void logAvailableHost(URL url) { if (this.offlineMode) return; if (url == null) { String message = Logging.getMessage("nullValue.URLIsNull"); Logging.error(message); throw new IllegalArgumentException(message); } String hostName = url.getHost(); HostInfo hi = this.hostMap.get(hostName); if (hi != null) { this.hostMap.remove(hostName); // host is available again firePropertyChange(NetworkStatus.HOST_AVAILABLE, null, url); } this.lastAvailableLogTime.set(System.currentTimeMillis()); }
public boolean isHostUnavailable(URL url) { if (this.offlineMode) return true; if (url == null) { String message = Logging.getMessage("nullValue.URLIsNull"); Logging.error(message); throw new IllegalArgumentException(message); } String hostName = url.getHost(); HostInfo hi = this.hostMap.get(hostName); if (hi == null) return false; if (hi.isTimeToTryAgain()) { hi.logCount.set(0); // info removed from table in logAvailableHost return false; } return hi.isUnavailable(); }
public static void main(String[] args) throws InterruptedException { for (URL url : discoverWsDevicesAsUrls()) { System.out.println("Device discovered: " + url.toString()); } }
public String what(String key, boolean oneliner) throws Exception { byte[] sha; Matcher m = SHA_P.matcher(key); if (m.matches()) { sha = Hex.toByteArray(key); } else { m = URL_P.matcher(key); if (m.matches()) { URL url = new URL(key); sha = SHA1.digest(url.openStream()).digest(); } else { File jarfile = new File(key); if (!jarfile.exists()) { reporter.error("File does not exist: %s", jarfile.getCanonicalPath()); } sha = SHA1.digest(jarfile).digest(); } } reporter.trace("sha %s", Hex.toHexString(sha)); Revision revision = library.getRevision(sha); if (revision == null) { return null; } StringBuilder sb = new StringBuilder(); Formatter f = new Formatter(sb); Justif justif = new Justif(120, 20, 70, 20, 75); DateFormat dateFormat = DateFormat.getDateInstance(); try { if (oneliner) { f.format("%20s %s%n", Hex.toHexString(revision._id), createCoord(revision)); } else { f.format("Artifact: %s%n", revision.artifactId); if (revision.organization != null && revision.organization.name != null) { f.format(" (%s)", revision.organization.name); } f.format("%n"); f.format("Coordinates\t0: %s%n", createCoord(revision)); f.format("Created\t0: %s%n", dateFormat.format(new Date(revision.created))); f.format("Size\t0: %d%n", revision.size); f.format("Sha\t0: %s%n", Hex.toHexString(revision._id)); f.format("URL\t0: %s%n", createJpmLink(revision)); f.format("%n"); f.format("%s%n", revision.description); f.format("%n"); f.format("Dependencies\t0:%n"); boolean flag = false; Iterable<RevisionRef> closure = library.getClosure(revision._id, true); for (RevisionRef dep : closure) { f.format( " - %s \t2- %s \t3- %s%n", dep.name, createCoord(dep), dateFormat.format(new Date(dep.created))); flag = true; } if (!flag) { f.format(" None%n"); } f.format("%n"); } f.flush(); justif.wrap(sb); return sb.toString(); } finally { f.close(); } }