List of usage examples for java.io DataOutputStream write
public synchronized void write(int b) throws IOException
b
) to the underlying output stream. From source file:com.lostad.app.base.util.RequestUtil.java
/** * HTTP??????,??? /*from www. java2s .co m*/ * @param actionUrl * @param params ? key???,value? * @param files * @throws Exception */ public static String postFile(String actionUrl, Map<String, String> params, FormFile[] files, String token) throws Exception { try { String BOUNDARY = "---------7d4a6d158c9"; //? String MULTIPART_FORM_DATA = "multipart/form-data"; URL url = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true);//? conn.setDoOutput(true);//? conn.setUseCaches(false);//?Cache conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY); conn.setRequestProperty("token", token); //???? StringBuilder sb = new StringBuilder(); if (params != null) { for (Map.Entry<String, String> entry : params.entrySet()) {//? sb.append("--"); sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n"); sb.append(entry.getValue()); sb.append("\r\n"); } } DataOutputStream outStream = new DataOutputStream(conn.getOutputStream()); outStream.write(sb.toString().getBytes());//???? //?? for (FormFile file : files) { StringBuilder split = new StringBuilder(); split.append("--"); split.append(BOUNDARY); split.append("\r\n"); split.append("Content-Disposition: form-data;name=\"" + file.getFormname() + "\";filename=\"" + file.getFilname() + "\"\r\n"); split.append("Content-Type: " + file.getContentType() + "\r\n\r\n"); outStream.write(split.toString().getBytes()); outStream.write(file.getData(), 0, file.getData().length); outStream.write("\r\n".getBytes()); } byte[] end_data = ("--" + BOUNDARY + "--\r\n").getBytes();//?? outStream.write(end_data); outStream.flush(); int cah = conn.getResponseCode(); if (cah != 200) throw new RuntimeException("url"); InputStream is = conn.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } outStream.close(); conn.disconnect(); return b.toString(); } catch (Exception e) { throw e; } }
From source file:com.android.volley.toolbox.http.HurlStack.java
@SuppressWarnings("deprecation") /* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards compatibility. // If the request's post body is null, then the assumption is that the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length explicitly, // since this is handled by HttpURLConnection using the size of the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody); out.close();//from w w w . ja v a2s. c om } break; case Method.GET: // Not necessary to set the request method because connection defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; case Method.HEAD: connection.setRequestMethod("HEAD"); break; case Method.OPTIONS: connection.setRequestMethod("OPTIONS"); break; case Method.TRACE: connection.setRequestMethod("TRACE"); break; case Method.PATCH: //connection.setRequestMethod("PATCH"); //If server doesnt support patch uncomment this connection.setRequestMethod("POST"); connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:com.daidiansha.acarry.control.network.core.volley.toolbox.HurlStack.java
@SuppressWarnings("deprecation") /* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards // compatibility. // If the request's post body is null, then the assumption is that // the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length // explicitly, // since this is handled by HttpURLConnection using the size of // the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody); out.close();/*from ww w. j a v a2 s. co m*/ } break; case Method.GET: // Not necessary to set the request method because connection // defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; case Method.HEAD: connection.setRequestMethod("HEAD"); break; case Method.OPTIONS: connection.setRequestMethod("OPTIONS"); break; case Method.TRACE: connection.setRequestMethod("TRACE"); break; case Method.PATCH: // connection.setRequestMethod("PATCH"); // If server doesnt support patch uncomment this connection.setRequestMethod("POST"); connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:BihuHttpUtil.java
/** * ??HTTPJSON?// ww w.j a v a2s . c om * @param url * @param jsonStr */ public static String sendPostForJson(String url, String jsonStr) { StringBuffer sb = new StringBuffer(""); try { // URL realUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); connection.connect(); //POST DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(jsonStr.getBytes("UTF-8"));//??? out.flush(); out.close(); //?? BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } reader.close(); // connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }
From source file:com.alibaba.rocketmq.tools.command.message.QueryMsgByIdSubCommand.java
private static String createBodyFile(MessageExt msg) throws IOException { DataOutputStream dos = null; try {/* w w w.ja va 2s .c o m*/ String bodyTmpFilePath = "/tmp/rocketmq/msgbodys"; File file = new File(bodyTmpFilePath); if (!file.exists()) { file.mkdirs(); } bodyTmpFilePath = bodyTmpFilePath + "/" + msg.getMsgId(); dos = new DataOutputStream(new FileOutputStream(bodyTmpFilePath)); dos.write(msg.getBody()); return bodyTmpFilePath; } finally { if (dos != null) dos.close(); } }
From source file:com.ict.dtube.tools.command.message.QueryMsgByIdSubCommand.java
private static String createBodyFile(MessageExt msg) throws IOException { DataOutputStream dos = null; try {/*from w w w . j ava2s.c o m*/ String bodyTmpFilePath = "/tmp/dtube/msgbodys"; File file = new File(bodyTmpFilePath); if (!file.exists()) { file.mkdirs(); } bodyTmpFilePath = bodyTmpFilePath + "/" + msg.getMsgId(); dos = new DataOutputStream(new FileOutputStream(bodyTmpFilePath)); dos.write(msg.getBody()); return bodyTmpFilePath; } finally { if (dos != null) dos.close(); } }
From source file:org.apache.jackrabbit.core.persistence.util.Serializer.java
/** * Serializes the specified <code>NodeState</code> object to the given * binary <code>stream</code>. * * @param state <code>state</code> to serialize * @param stream the stream where the <code>state</code> should be * serialized to//from www . ja v a 2 s . co m * @throws Exception if an error occurs during the serialization * @see #deserialize(NodeState, InputStream) */ public static void serialize(NodeState state, OutputStream stream) throws Exception { DataOutputStream out = new DataOutputStream(stream); // primaryType out.writeUTF(state.getNodeTypeName().toString()); // parentUUID if (state.getParentId() == null) { out.write(NULL_UUID_PLACEHOLDER_BYTES); } else { out.write(state.getParentId().getRawBytes()); } // definitionId out.writeUTF(""); // mixin types Collection<Name> c = state.getMixinTypeNames(); out.writeInt(c.size()); // count for (Iterator<Name> iter = c.iterator(); iter.hasNext();) { out.writeUTF(iter.next().toString()); // name } // modCount out.writeShort(state.getModCount()); // properties (names) c = state.getPropertyNames(); out.writeInt(c.size()); // count for (Iterator<Name> iter = c.iterator(); iter.hasNext();) { Name propName = iter.next(); out.writeUTF(propName.toString()); // name } // child nodes (list of name/uuid pairs) Collection<ChildNodeEntry> collChildren = state.getChildNodeEntries(); out.writeInt(collChildren.size()); // count for (Iterator<ChildNodeEntry> iter = collChildren.iterator(); iter.hasNext();) { ChildNodeEntry entry = iter.next(); out.writeUTF(entry.getName().toString()); // name out.write(entry.getId().getRawBytes()); // uuid } }
From source file:BihuHttpUtil.java
public static String doHttpPost(String xmlInfo, String URL) { System.out.println("??:" + xmlInfo); byte[] xmlData = xmlInfo.getBytes(); InputStream instr = null;//from w w w . j a v a 2s . co m java.io.ByteArrayOutputStream out = null; try { URL url = new URL(URL); URLConnection urlCon = url.openConnection(); urlCon.setDoOutput(true); urlCon.setDoInput(true); urlCon.setUseCaches(false); urlCon.setRequestProperty("Content-Type", "text/xml"); urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length)); System.out.println(String.valueOf(xmlData.length)); DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream()); printout.write(xmlData); printout.flush(); printout.close(); instr = urlCon.getInputStream(); byte[] bis = IOUtils.toByteArray(instr); String ResponseString = new String(bis, "UTF-8"); if ((ResponseString == null) || ("".equals(ResponseString.trim()))) { System.out.println(""); } System.out.println("?:" + ResponseString); return ResponseString; } catch (Exception e) { e.printStackTrace(); return "0"; } finally { try { out.close(); instr.close(); } catch (Exception ex) { return "0"; } } }
From source file:com.androidex.volley.toolbox.HurlStack.java
@SuppressWarnings("deprecation") /* package */static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards // compatibility. // If the request's post body is null, then the assumption is that // the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length // explicitly, // since this is handled by HttpURLConnection using the size of // the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody); out.close();/*from w w w.ja v a 2s . c o m*/ } break; case Method.GET: // Not necessary to set the request method because connection // defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; case Method.HEAD: connection.setRequestMethod("HEAD"); break; case Method.OPTIONS: connection.setRequestMethod("OPTIONS"); break; case Method.TRACE: connection.setRequestMethod("TRACE"); break; case Method.PATCH: // connection.setRequestMethod("PATCH"); // If server doesnt support patch uncomment this connection.setRequestMethod("POST"); connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:nl.sidn.dnslib.message.util.DNSStringUtil.java
public static byte[] writeName(String name) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); try {/* w w w . j a va2 s . c o m*/ //write nameserver string String[] labels = StringUtils.split(name, "."); for (String label : labels) { //write label length dos.writeByte(label.length()); dos.write(label.getBytes()); } //write root with zero byte dos.writeByte(0); } catch (IOException e) { throw new RuntimeException("Error while wrting name", e); } return bos.toByteArray(); }