try entity.writeTo(bos);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); FileBody fileBody = new FileBody(new File(fileName)); MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT); multipartEntity.addPart("file", fileBody); connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue()); OutputStream out = connection.getOutputStream(); try { multipartEntity.writeTo(out); } finally { out.close(); } int status = connection.getResponseCode(); ...
@Override public void writeTo(final OutputStream outstream) throws IOException { super.writeTo(new CountingOutputStream(outstream, this.listener)); }
@Override public byte[] getBody() throws AuthFailureError { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { mEntity.writeTo(bos); } catch (IOException e) { VinciLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request.", e); } return bos.toByteArray(); }
File f = new File(path); BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() ); String filename=path.substring(path.lastIndexOf("/")+1); // create a multipart message MultipartEntity multipartContent = new MultipartEntity(); // send the file inputstream as data InputStreamBody isb = new InputStreamBody(new FileInputStream(f), "image/jpeg", filename); // add key value pair. The key "imageFile" is arbitrary multipartContent.addPart("imageFile", isb); multipartContent.writeTo(out); out.flush(); out.close();
File f = new File(path); BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() ); String filename=path.substring(path.lastIndexOf("/")+1); // create a multipart message MultipartEntity multipartContent = new MultipartEntity(); // send the file inputstream as data InputStreamBody isb = new InputStreamBody(new FileInputStream(f), "image/jpeg", filename); // add key value pair. The key "imageFile" is arbitrary multipartContent.addPart("imageFile", isb); multipartContent.writeTo(out); out.flush(); out.close();
MultipartEntity reqEntity = new MultipartEntity(); // add your ContentBody fields as normal... // Now, pull out the contents of everything you've added and set it as the payload ByteArrayOutputStream bos = new ByteArrayOutputStream((int)reqEntity.getContentLength()); reqEntity.writeTo(bos); oAuthReq.addPayload(bos.toByteArray()); // Finally, set the Content-type header (with the boundary marker): Header contentType = reqEntity.getContentType(); oAuthReq.addHeader(contentType.getName(), contentType.getValue()); // Sign and send like normal: service.signRequest(new Token(oAuthToken, oAuthSecret), oAuthReq); Response oauthResp = oAuthReq.send();
@Override public Entity build() { try { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); apacheMultipartEntity.writeTo(outputStream); final InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Header header = apacheMultipartEntity.getContentType(); final Map<String, String> headers = Maps.newHashMap(); headers.put(header.getName(), header.getValue()); return new MultiPartEntity(headers, inputStream); } catch (IOException e) { throw new RuntimeException(e); } } }
ServletResponse httpResponse = ctx.getResponse(); ResponseFacade rf = (ResponseFacade) httpResponse; httpResponse.setCharacterEncoding("UTF-8"); httpResponse.setContentType("multipart/mixed"); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "SEPERATOR_STRING",Charset.forName("UTF-8")); entity.addPart("json", new StringBody(CMD + "#" + content, "text/plain", Charset.forName("UTF-8"))); entity.addPart("image", new ByteArrayBody(data, "image/jpeg", "file")); httpResponse.setContentLength((int) entity.getContentLength()); entity.writeTo(httpResponse.getOutputStream()); ctx.complete();
URL url = new URL(baseURL+"/projects/"+projectId+"/CSVUpload?authenticity_token="+URLEncoder.encode(authToken, "UTF-8")); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); MultipartEntity entity = new MultipartEntity(); entity.addPart("utf8", new StringBody("\u2713", "text/plain", Charset.forName("UTF-8"))); entity.addPart("csv", new FileBody(csvToUpload, "text/csv")); connection.setRequestProperty("Content-Type", entity.getContentType().getValue()); OutputStream out = connection.getOutputStream(); try { entity.writeTo(out); } finally { out.close(); } connection.getResponseCode();
private void addMultiparts(List<MultiPartEntity> multiParts, HttpURLConnection conn) throws IOException { MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT); for (MultiPartEntity part : multiParts) { multipartEntity.addPart(part.getPartName(), part.getEntity()); } conn.setDoOutput(true); conn.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue()); //try to write to the output stream of the connection OutputStream outStream = conn.getOutputStream(); multipartEntity.writeTo(outStream); outStream.close(); }
private void addMultiparts(List<MultiPartEntity> multiParts, HttpURLConnection conn) throws IOException { MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT); for (MultiPartEntity part : multiParts) { multipartEntity.addPart(part.getPartName(), part.getEntity()); } conn.setDoOutput(true); conn.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue()); //try to write to the output stream of the connection OutputStream outStream = conn.getOutputStream(); multipartEntity.writeTo(outStream); outStream.close(); }
entity.writeTo(httpcon.getOutputStream()); os.close(); httpcon.connect();
conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue()); OutputStream os = conn.getOutputStream(); reqEntity.writeTo(os); os.close(); conn.connect();
conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue()); OutputStream os = conn.getOutputStream(); reqEntity.writeTo(os); os.close(); conn.connect();
/* You will have done this bit earlier to authorize the user OAuthService service = new ServiceBuilder().provider(TwitterApi.SSL.class).apiKey("[YOUR API KEY]").apiSecret("[YOUR SECRET]").callback("twitter://callback").build(); Token accessToken = Do you oauth authorization as normal */ OAuthRequest request = new OAuthRequest(Verb.POST, "https://upload.twitter.com/1/statuses/update_with_media.json"); MultipartEntity entity = new MultipartEntity(); try { entity.addPart("status", new StringBody("insert vacuous statement here")); entity.addPart("media", new FileBody(new File("/path/of/your/image/file"))); ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); request.addPayload(out.toByteArray()); request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue()); service.signRequest(accessToken, request); Response response = request.send(); if (response.isSuccessful()) { // you're all good } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
entity.writeTo(out);
try entity.writeTo(bos);
try entity.writeTo(bos);
try entity.writeTo(bos);