List of usage examples for java.io OutputStreamWriter flush
public void flush() throws IOException
From source file:com.pureinfo.srm.outlay.action.SearchCheckCodeAction.java
/** * //from ww w . j a v a 2s. co m * @param strUrl * @param strPostRequest * @return */ public static String getPageContent(String strUrl, String strPostRequest) { // StringBuffer buffer = new StringBuffer(); System.setProperty("sun.net.client.defaultConnectTimeout", "5000"); System.setProperty("sun.net.client.defaultReadTimeout", "5000"); try { URL newUrl = new URL(strUrl); HttpURLConnection hConnect = (HttpURLConnection) newUrl.openConnection(); //POST if (strPostRequest.length() > 0) { hConnect.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(hConnect.getOutputStream()); out.write(strPostRequest); out.flush(); out.close(); } // BufferedReader rd = new BufferedReader(new InputStreamReader(hConnect.getInputStream())); int ch; for (int length = 0; (ch = rd.read()) > -1; length++) buffer.append((char) ch); rd.close(); hConnect.disconnect(); return buffer.toString().trim(); } catch (Exception e) { // return ":"; return null; } finally { buffer.setLength(0); } }
From source file:com.wareninja.opensource.common.wsrequest.HttpUtils.java
/** * Send an HTTP(s) request with POST parameters. * /*from w w w . ja v a 2s.c o m*/ * @param parameters * @param url * @throws UnsupportedEncodingException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException */ public static void doPost(Map<?, ?> parameters, URL url) throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException { URLConnection cnx = getConnection(url); // Construct data StringBuilder dataBfr = new StringBuilder(); Iterator<?> iKeys = parameters.keySet().iterator(); while (iKeys.hasNext()) { if (dataBfr.length() != 0) { dataBfr.append('&'); } String key = (String) iKeys.next(); dataBfr.append(URLEncoder.encode(key, "UTF-8")).append('=') .append(URLEncoder.encode((String) parameters.get(key), "UTF-8")); } // POST data cnx.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(cnx.getOutputStream()); if (LOGGING.DEBUG) Log.d(LOG_TAG, "Posting crash report data"); wr.write(dataBfr.toString()); wr.flush(); wr.close(); if (LOGGING.DEBUG) Log.d(LOG_TAG, "Reading response"); BufferedReader rd = new BufferedReader(new InputStreamReader(cnx.getInputStream())); String line; int linecount = 0; while ((line = rd.readLine()) != null) { linecount++; if (linecount <= 2) { if (LOGGING.DEBUG) Log.d(LOG_TAG, line); } } rd.close(); }
From source file:es.tid.cep.esperanza.Utils.java
public static boolean DoHTTPPost(String urlStr, String content) { try {//from w ww . j av a 2 s . c o m URL url = new URL(urlStr); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setDoOutput(true); urlConn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); OutputStreamWriter printout = new OutputStreamWriter(urlConn.getOutputStream(), Charset.forName("UTF-8")); printout.write(content); printout.flush(); printout.close(); int code = urlConn.getResponseCode(); String message = urlConn.getResponseMessage(); logger.debug("action http response " + code + " " + message); if (code / 100 == 2) { InputStream input = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); for (String line; (line = reader.readLine()) != null;) { logger.debug("action response body: " + line); } input.close(); return true; } else { logger.error("action response is not OK: " + code + " " + message); InputStream error = urlConn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(error)); for (String line; (line = reader.readLine()) != null;) { logger.error("action error response body: " + line); } error.close(); return false; } } catch (MalformedURLException me) { logger.error("exception MalformedURLException: " + me.getMessage()); return false; } catch (IOException ioe) { logger.error("exception IOException: " + ioe.getMessage()); return false; } }
From source file:Main.java
private static void writeToOutputStreamArray(String[] array, OutputStreamWriter oswriter) { for (int i = 0; i < array.length; i++) { try {//w w w . j av a2s . com oswriter.append(array[i] + " "); } catch (IOException e) { e.printStackTrace(); } } try { oswriter.append("\n"); oswriter.flush(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
private static void writeToOutputStreamArray(String[] array, OutputStreamWriter oswriter) { for (int i = 0; i < array.length; i++) { try {// ww w. j ava 2 s. c o m oswriter.append(array[i] + " "); } catch (IOException e) { e.printStackTrace(); } } try { oswriter.append("\n\n"); oswriter.flush(); } catch (IOException e) { e.printStackTrace(); } }
From source file:io.lavagna.service.LavagnaExporter.java
private static void writeEntry(String entryName, Object toSerialize, ZipOutputStream zf, OutputStreamWriter osw) { try {/*ww w . j av a 2 s. c o m*/ zf.putNextEntry(new ZipEntry(entryName)); Json.GSON.toJson(toSerialize, osw); osw.flush(); zf.flush(); zf.closeEntry(); } catch (IOException ioe) { throw new IllegalStateException("error while serializing entry " + entryName, ioe); } }
From source file:com.mingsoft.weixin.http.HttpClientConnectionManager.java
/** * ?/*from www .j av a2 s . c om*/ * @param postUrl ? * @param param ? * @param method * @return null */ public static String request(String postUrl, String param, String method) { URL url; try { url = new URL(postUrl); HttpURLConnection conn; conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(30000); // ??) conn.setReadTimeout(30000); // ????) conn.setDoOutput(true); // post??http?truefalse conn.setDoInput(true); // ?httpUrlConnectiontrue conn.setUseCaches(false); // Post ? conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestMethod(method);// "POST"GET conn.setRequestProperty("Content-Length", param.length() + ""); String encode = "utf-8"; OutputStreamWriter out = null; out = new OutputStreamWriter(conn.getOutputStream(), encode); out.write(param); out.flush(); if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { return null; } // ?? BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line = ""; StringBuffer strBuf = new StringBuffer(); while ((line = in.readLine()) != null) { strBuf.append(line).append("\n"); } in.close(); out.close(); return strBuf.toString(); } catch (MalformedURLException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
private static void saveStr(String path, String xml, boolean isAppend) { OutputStream out = null;// w w w .j a v a2 s.c o m OutputStreamWriter outwriter = null; try { File file = new File(path); if (!file.exists()) { file.createNewFile(); } out = new FileOutputStream(path, isAppend); outwriter = new OutputStreamWriter(out, "UTF-8"); outwriter.write(xml); outwriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (outwriter != null) { outwriter.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static int doShellCommand(String cmd, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { Process proc = null;/*from w w w . j a v a2s . c o m*/ int exitCode = -1; if (runAsRoot) proc = Runtime.getRuntime().exec("su"); else proc = Runtime.getRuntime().exec("sh"); OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); // TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); out.write(cmd); out.write("\n"); out.flush(); out.write("exit\n"); out.flush(); if (waitFor) { final char buf[] = new char[10]; // Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } // Consume the "stderr" reader = new InputStreamReader(proc.getErrorStream()); read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } exitCode = proc.waitFor(); } return exitCode; }
From source file:CopyUtils.java
/** * Serialize chars from a <code>String</code> to bytes on an <code>OutputStream</code>, and * flush the <code>OutputStream</code>. * @param input the <code>String</code> to read from * @param output the <code>OutputStream</code> to write to * @throws IOException In case of an I/O problem *//*from w ww. j a v a 2 s. c o m*/ public static void copy(String input, OutputStream output) throws IOException { StringReader in = new StringReader(input); OutputStreamWriter out = new OutputStreamWriter(output); copy(in, out); // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here. out.flush(); }