List of usage examples for org.apache.http.entity.mime.content InputStreamBody InputStreamBody
public InputStreamBody(final InputStream in, final String filename)
From source file:App.App.java
public static void main(String[] args) throws IOException { File inFile = new File("C:\\Data\\LogTest.java"); FileInputStream fis = new FileInputStream(inFile); DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httppost = new HttpPost("http://localhost:8080/rest_j2ee/rest/files/upload"); MultipartEntity entity = new MultipartEntity(); entity.addPart("file", new InputStreamBody(fis, inFile.getName())); httppost.setEntity(entity);/*from w w w . j ava 2s . c o m*/ HttpResponse response = httpclient.execute(httppost); int statusCode = response.getStatusLine().getStatusCode(); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity, "UTF-8"); System.out.println("[" + statusCode + "] " + responseString); }
From source file:httpclient.UploadAction.java
public static void main(String[] args) throws FileNotFoundException { File targetFile1 = new File("F:\\2.jpg"); // File targetFile2 = new File("F:\\1.jpg"); FileInputStream fis1 = new FileInputStream(targetFile1); // FileInputStream fis2 = new FileInputStream(targetFile2); // String targetURL = // "http://static.fangbiandian.com.cn/round_server/upload/uploadFile.do"; String targetURL = "http://www.fangbiandian.com.cn/round_server/user/updateUserInfo.do"; HttpPost filePost = new HttpPost(targetURL); try {//from www . ja v a 2s. co m // ????? HttpClient client = new DefaultHttpClient(); // FormBodyPart fbp1 = new FormBodyPart("file1", new // FileBody(targetFile1)); // FormBodyPart fbp2 = new FormBodyPart("file2", new // FileBody(targetFile2)); // FormBodyPart fbp3 = new FormBodyPart("file3", new // FileBody(targetFile3)); // List<FormBodyPart> picList = new ArrayList<FormBodyPart>(); // picList.add(fbp1); // picList.add(fbp2); // picList.add(fbp3); Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("userId", "65478A5CD8D20C3807EE16CF22AF8A17"); Map<String, Object> map = new HashMap<String, Object>(); String jsonStr = JSON.toJSONString(paramMap); map.put("cid", 321); map.put("request", jsonStr); String jsonString = JSON.toJSONString(map); MultipartEntity multiEntity = new MultipartEntity(); Charset charset = Charset.forName("UTF-8"); multiEntity.addPart("request", new StringBody(jsonString, charset)); multiEntity.addPart("photo", new InputStreamBody(fis1, "2.jpg")); // multiEntity.addPart("licenseUrl", new InputStreamBody(fis2, // "1.jpg")); filePost.setEntity(multiEntity); HttpResponse response = client.execute(filePost); int code = response.getStatusLine().getStatusCode(); System.out.println(code); if (HttpStatus.SC_OK == code) { System.out.println("?"); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // do something useful BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); String str = null; while ((str = reader.readLine()) != null) { System.out.println(str); } } finally { instream.close(); } } } else { System.out.println(""); } } catch (Exception ex) { ex.printStackTrace(); } finally { filePost.releaseConnection(); } }
From source file:org.eclipse.dirigible.cli.apis.ImportProjectAPI.java
public static void importProject(RequestConfig config, String url, InputStream in, String filename, Map<String, String> headers) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.addPart(PART_NAME, new InputStreamBody(in, filename)); HttpPost postRequest = new HttpPost(url); postRequest.setEntity(entityBuilder.build()); addHeaders(postRequest, headers);//from w w w .ja v a 2 s. co m if (config != null) { postRequest.setConfig(config); } executeRequest(httpClient, postRequest); }
From source file:at.uni_salzburg.cs.ckgroup.cscpp.utils.HttpQueryUtils.java
public static String[] fileUpload(String uploadUrl, String name, byte[] byteArray) throws IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(uploadUrl); String paramName = "vehicle"; String paramValue = name;/*from ww w . jav a 2 s .c om*/ MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart(paramName, new InputStreamBody((new ByteArrayInputStream(byteArray)), "application/zip")); entity.addPart(paramName, new StringBody(paramValue, "text/plain", Charset.forName("UTF-8"))); httppost.setEntity(entity); HttpResponse httpResponse = httpclient.execute(httppost); StatusLine statusLine = httpResponse.getStatusLine(); String reason = statusLine.getReasonPhrase(); int rc = statusLine.getStatusCode(); String response = EntityUtils.toString(httpResponse.getEntity(), "UTF-8"); httpclient.getConnectionManager().shutdown(); return new String[] { String.valueOf(rc), reason, response }; }
From source file:com.ls.http.base.handler.multipart.StreamMultipartEntityPart.java
@Override public ContentBody getContentBody() { return new InputStreamBody(this.value, ContentType.DEFAULT_BINARY); }
From source file:org.opencastproject.remotetest.server.resource.IngestResources.java
public static HttpResponse addZippedMediaPackage(TrustedHttpClient client, InputStream mediaPackage) throws Exception { HttpPost post = new HttpPost(getServiceUrl() + "addZippedMediaPackage"); MultipartEntity entity = new MultipartEntity(); entity.addPart("mediaPackage", new InputStreamBody(mediaPackage, "mediapackage.zip")); post.setEntity(entity);/*from www . j ava 2 s.c o m*/ return client.execute(post); }
From source file:org.opencastproject.remotetest.server.MultiPartTest.java
@Test public void testMultiPartPost() throws Exception { String mp = "<oc:mediapackage xmlns:oc=\"http://mediapackage.opencastproject.org\" id=\"10.0000/1\" start=\"2007-12-05T13:40:00\" duration=\"1004400000\"></oc:mediapackage>"; InputStream is = null;//from ww w . j a v a 2s . c o m try { is = getClass().getResourceAsStream("/av.mov"); InputStreamBody fileContent = new InputStreamBody(is, "av.mov"); MultipartEntity mpEntity = new MultipartEntity(); mpEntity.addPart("mediaPackage", new StringBody(mp)); mpEntity.addPart("flavor", new StringBody("presentation/source")); mpEntity.addPart("userfile", fileContent); HttpPost httppost = new HttpPost(BASE_URL + "/ingest/addAttachment"); httppost.setEntity(mpEntity); HttpResponse response = httpClient.execute(httppost); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); } finally { IOUtils.closeQuietly(is); } }
From source file:org.jcommon.com.facebook.utils.FacebookRequest.java
public void run() { HttpClient httpclient = new DefaultHttpClient(); try {//from w w w . j a va 2s. c o m logger.info(Integer.valueOf("file size:" + this.in != null ? this.in.available() : 0)); HttpPost httppost = new HttpPost(this.url_); MultipartEntity reqEntity = new MultipartEntity(); FormBodyPart stream_part = new FormBodyPart(this.stream_name, new InputStreamBody(this.in, this.file_name)); reqEntity.addPart(stream_part); for (int i = 0; i < this.keys.length; i++) { reqEntity.addPart(this.keys[i], new StringBody(this.values[i])); } httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); StatusLine status_line = response.getStatusLine(); int responseCode = status_line.getStatusCode(); BufferedReader http_reader = null; if (responseCode == 200) { http_reader = new BufferedReader(new InputStreamReader(resEntity.getContent(), "utf-8")); String line = null; while ((line = http_reader.readLine()) != null) { this.sResult.append(line); } if (this.listener_ != null) this.listener_.onSuccessful(this, this.sResult); } else if (responseCode >= 400) { http_reader = new BufferedReader(new InputStreamReader(resEntity.getContent(), "utf-8")); String line = null; while ((line = http_reader.readLine()) != null) { this.sResult.append(line); } logger.info("[URL][response][failure]" + this.sResult); if (this.listener_ != null) this.listener_.onFailure(this, this.sResult); } else { this.sResult.append("[URL][response][failure][code : " + responseCode + " ]"); if (this.listener_ != null) this.listener_.onFailure(this, this.sResult); logger.info("[URL][response][failure][code : " + responseCode + " ]"); } EntityUtils.consume(resEntity); } catch (UnsupportedEncodingException e) { logger.warn("[HttpReqeust] error:" + this.url_ + "\n" + e); if (this.listener_ != null) this.listener_.onException(this, e); } catch (ClientProtocolException e) { logger.warn("[HttpReqeust] error:" + this.url_ + "\n" + e); if (this.listener_ != null) this.listener_.onException(this, e); } catch (IOException e) { logger.warn("[HttpReqeust] error:" + this.url_ + "\n" + e); if (this.listener_ != null) this.listener_.onException(this, e); } finally { try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) { } } }
From source file:org.opencastproject.remotetest.server.WorkingFileRepoRestEndpointTest.java
@Test public void testPutAndGetFile() throws Exception { // Store a file in the repository String mediapackageId = "123"; String elementId = "456"; byte[] bytesFromPost = IOUtils .toByteArray(getClass().getClassLoader().getResourceAsStream("opencast_header.gif")); InputStream in = getClass().getClassLoader().getResourceAsStream("opencast_header.gif"); String fileName = "our_logo.gif"; // Used to simulate a file upload MultipartEntity postEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); postEntity.addPart("file", new InputStreamBody(in, fileName)); HttpPost post = new HttpPost(BASE_URL + "/files/mediapackage/" + mediapackageId + "/" + elementId); post.setEntity(postEntity);//from w ww. j a v a 2 s. co m HttpResponse response = client.execute(post); HttpEntity responseEntity = response.getEntity(); String stringResponse = EntityUtils.toString(responseEntity); String expectedResponse = BASE_URL + "/files/mediapackage/" + mediapackageId + "/" + elementId + "/" + fileName; Assert.assertEquals(expectedResponse, stringResponse); // Get the file back from the repository HttpGet get = new HttpGet(BASE_URL + "/files/mediapackage/" + mediapackageId + "/" + elementId); HttpResponse getResponse = client.execute(get); byte[] bytesFromGet = IOUtils.toByteArray(getResponse.getEntity().getContent()); // Ensure that the bytes that we posted are the same we received Assert.assertTrue(Arrays.equals(bytesFromGet, bytesFromPost)); }
From source file:com.revo.deployr.client.call.project.ProjectImportCall.java
/** * Internal use only, to execute call use RClient.execute(). *//* w w w.j a va2 s .c o m*/ public RCoreResult call() { RCoreResultImpl pResult = null; try { HttpPost httpPost = new HttpPost(serverUrl + API); super.httpUriRequest = httpPost; List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("format", "json")); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip")); entity.addPart("descr", new StringBody(descr, "text/plain", Charset.forName("UTF-8"))); entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8"))); httpPost.setEntity(entity); // set any custom headers on the request for (Map.Entry<String, String> entry : httpHeaders.entrySet()) { httpPost.addHeader(entry.getKey(), entry.getValue()); } HttpResponse response = httpClient.execute(httpPost); StatusLine statusLine = response.getStatusLine(); HttpEntity responseEntity = response.getEntity(); String markup = EntityUtils.toString(responseEntity); pResult = new RCoreResultImpl(response.getAllHeaders()); pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase()); } catch (UnsupportedEncodingException ueex) { log.warn("ProjectImportCall: unsupported encoding exception.", ueex); } catch (IOException ioex) { log.warn("ProjectImportCall: io exception.", ioex); } return pResult; }