/** * 以阻塞的方式立即下载一个文件,该方法会覆盖已经存在的文件 * * @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"; }
private byte[] downloadByteArray(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Cookie", cookies); if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) return (null); InputStream in = conn.getInputStream(); byte[] buf = new byte[conn.getContentLength()]; int read, offset = 0; while ((read = in.read(buf, offset, buf.length - offset)) != -1) { offset += read; } return (buf); }
private boolean checkServer(int port, boolean ssl) { try { URL url = new URL("http://127.0.0.1:" + port); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int length = conn.getContentLength(); StringBuffer text = new StringBuffer(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int size = 256; char[] buf = new char[size]; int len; while ((len = isr.read(buf, 0, size)) != -1) text.append(buf, 0, len); isr.close(); if (programName.equals("ISN")) return !shutdown(port, ssl); return true; } catch (Exception ex) { return false; } }
SOAPMessage doGet(URL endPoint) throws SOAPException { boolean isFailure = false; URL url = null; HttpURLConnection httpConnection = null; int responseCode = 0; try { /// Is https GET allowed?? if (endPoint.getProtocol().equals("https")) initHttps(); // Process the URL JaxmURI uri = new JaxmURI(endPoint.toString()); String userInfo = uri.getUserinfo(); url = endPoint; if (dL > 0) d("uri: " + userInfo + " " + url + " " + uri); // TBD // Will deal with https later. if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) { log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https"); throw new IllegalArgumentException( "Protocol " + url.getProtocol() + " not supported in URL " + url); } httpConnection = (HttpURLConnection) createConnection(url); httpConnection.setRequestMethod("GET"); httpConnection.setDoOutput(true); httpConnection.setDoInput(true); httpConnection.setUseCaches(false); httpConnection.setFollowRedirects(true); httpConnection.connect(); try { responseCode = httpConnection.getResponseCode(); // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { isFailure = true; } else if ((responseCode / 100) != 2) { log.log( Level.SEVERE, "SAAJ0008.p2p.bad.response", new String[] {httpConnection.getResponseMessage()}); throw new SOAPExceptionImpl( "Bad response: (" + responseCode + httpConnection.getResponseMessage()); } } catch (IOException e) { // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds! responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { isFailure = true; } else { throw e; } } } catch (SOAPException ex) { throw ex; } catch (Exception ex) { log.severe("SAAJ0012.p2p.get.failed"); throw new SOAPExceptionImpl("Get failed", ex); } SOAPMessage response = null; if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { try { MimeHeaders headers = new MimeHeaders(); String key, value; // Header field 0 is the status line so we skip it. int i = 1; while (true) { key = httpConnection.getHeaderFieldKey(i); value = httpConnection.getHeaderField(i); if (key == null && value == null) break; if (key != null) { StringTokenizer values = new StringTokenizer(value, ","); while (values.hasMoreTokens()) headers.addHeader(key, values.nextToken().trim()); } i++; } InputStream httpIn = (isFailure ? httpConnection.getErrorStream() : httpConnection.getInputStream()); // If no reply message is returned, // content-Length header field value is expected to be zero. // java SE 6 documentation says : // available() : an estimate of the number of bytes that can be read // (or skipped over) from this input stream without blocking // or 0 when it reaches the end of the input stream. if ((httpIn == null) || (httpConnection.getContentLength() == 0) || (httpIn.available() == 0)) { response = null; log.warning("SAAJ0014.p2p.content.zero"); } else { response = messageFactory.createMessage(headers, httpIn); } httpIn.close(); httpConnection.disconnect(); } catch (SOAPException ex) { throw ex; } catch (Exception ex) { log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp", ex); throw new SOAPExceptionImpl("Unable to read response: " + ex.getMessage()); } } return response; }
SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException { boolean isFailure = false; URL url = null; HttpURLConnection httpConnection = null; int responseCode = 0; try { if (endPoint.getProtocol().equals("https")) // if(!setHttps) initHttps(); // Process the URL JaxmURI uri = new JaxmURI(endPoint.toString()); String userInfo = uri.getUserinfo(); url = endPoint; if (dL > 0) d("uri: " + userInfo + " " + url + " " + uri); // TBD // Will deal with https later. if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) { log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https"); throw new IllegalArgumentException( "Protocol " + url.getProtocol() + " not supported in URL " + url); } httpConnection = (HttpURLConnection) createConnection(url); httpConnection.setRequestMethod("POST"); httpConnection.setDoOutput(true); httpConnection.setDoInput(true); httpConnection.setUseCaches(false); httpConnection.setInstanceFollowRedirects(true); if (message.saveRequired()) message.saveChanges(); MimeHeaders headers = message.getMimeHeaders(); Iterator it = headers.getAllHeaders(); boolean hasAuth = false; // true if we find explicit Auth header while (it.hasNext()) { MimeHeader header = (MimeHeader) it.next(); String[] values = headers.getHeader(header.getName()); if (values.length == 1) httpConnection.setRequestProperty(header.getName(), header.getValue()); else { StringBuffer concat = new StringBuffer(); int i = 0; while (i < values.length) { if (i != 0) concat.append(','); concat.append(values[i]); i++; } httpConnection.setRequestProperty(header.getName(), concat.toString()); } if ("Authorization".equals(header.getName())) { hasAuth = true; log.fine("SAAJ0091.p2p.https.auth.in.POST.true"); } } if (!hasAuth && userInfo != null) { initAuthUserInfo(httpConnection, userInfo); } OutputStream out = httpConnection.getOutputStream(); message.writeTo(out); out.flush(); out.close(); httpConnection.connect(); try { responseCode = httpConnection.getResponseCode(); // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { isFailure = true; } // else if (responseCode != HttpURLConnection.HTTP_OK) // else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207)) else if ((responseCode / 100) != 2) { log.log( Level.SEVERE, "SAAJ0008.p2p.bad.response", new String[] {httpConnection.getResponseMessage()}); throw new SOAPExceptionImpl( "Bad response: (" + responseCode + httpConnection.getResponseMessage()); } } catch (IOException e) { // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds! responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { isFailure = true; } else { throw e; } } } catch (SOAPException ex) { throw ex; } catch (Exception ex) { log.severe("SAAJ0009.p2p.msg.send.failed"); throw new SOAPExceptionImpl("Message send failed", ex); } SOAPMessage response = null; if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { try { MimeHeaders headers = new MimeHeaders(); String key, value; // Header field 0 is the status line so we skip it. int i = 1; while (true) { key = httpConnection.getHeaderFieldKey(i); value = httpConnection.getHeaderField(i); if (key == null && value == null) break; if (key != null) { StringTokenizer values = new StringTokenizer(value, ","); while (values.hasMoreTokens()) headers.addHeader(key, values.nextToken().trim()); } i++; } InputStream httpIn = (isFailure ? httpConnection.getErrorStream() : httpConnection.getInputStream()); byte[] bytes = readFully(httpIn); int length = httpConnection.getContentLength() == -1 ? bytes.length : httpConnection.getContentLength(); // If no reply message is returned, // content-Length header field value is expected to be zero. if (length == 0) { response = null; log.warning("SAAJ0014.p2p.content.zero"); } else { ByteInputStream in = new ByteInputStream(bytes, length); response = messageFactory.createMessage(headers, in); } httpIn.close(); httpConnection.disconnect(); } catch (SOAPException ex) { throw ex; } catch (Exception ex) { log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp", ex); throw new SOAPExceptionImpl("Unable to read response: " + ex.getMessage()); } } return response; }