List of usage examples for org.apache.http.util CharsetUtils get
public static Charset get(String str) throws UnsupportedEncodingException
From source file:com.knurld.alphabank.com.knurld.alphabank.request.MultipartRequest.java
public MultipartRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener, File file, Map<String, String> mStringPart, final Map<String, String> headerParams, String partName, MultipartProgressListener progLitener) { super(Method.POST, url, errorListener); this.mListener = listener; this.mFilePart = file; this.fileLength = file.length(); this.mStringPart = mStringPart; this.headerParams = headerParams; this.FILE_PART_NAME = partName; this.multipartProgressListener = progLitener; entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); try {// w ww .j a v a 2 s. c om entity.setCharset(CharsetUtils.get("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } buildMultipartEntity(); httpentity = entity.build(); }
From source file:org.lokra.seaweedfs.core.VolumeWrapper.java
/** * Upload file./*from w w w . j av a2 s. co m*/ * * @param url url * @param fid fid * @param fileName fileName * @param stream stream * @param ttl ttl * @param contentType contentType * @return The size returned is the size stored on SeaweedFS. * @throws IOException Http connection is fail or server response within some error message. */ long uploadFile(String url, String fid, String fileName, InputStream stream, String ttl, ContentType contentType) throws IOException { HttpPost request; if (ttl != null) request = new HttpPost(url + "/" + fid + "?ttl=" + ttl); else request = new HttpPost(url + "/" + fid); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(CharsetUtils.get("UTF-8")); builder.addBinaryBody("upload", stream, contentType, fileName); HttpEntity entity = builder.build(); request.setEntity(entity); JsonResponse jsonResponse = connection.fetchJsonResultByRequest(request); convertResponseStatusToException(jsonResponse.statusCode, url, fid, false, false, false, false); return (Integer) objectMapper.readValue(jsonResponse.json, Map.class).get("size"); }
From source file:net.duckling.ddl.util.RESTClient.java
public JsonObject httpUpload(String url, String dataFieldName, byte[] data, List<NameValuePair> params) { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w . j a va 2 s . c om*/ HttpPost httppost = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addBinaryBody(dataFieldName, data, ContentType.DEFAULT_BINARY, "tempfile"); for (NameValuePair hp : params) { builder.addPart(hp.getName(), new StringBody(hp.getValue(), ContentType.create("text/plain", Consts.UTF_8))); } HttpEntity reqEntity = builder.setCharset(CharsetUtils.get("UTF-8")).build(); httppost.setEntity(reqEntity); CloseableHttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } try { BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent()))); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(br); return je.getAsJsonObject(); } finally { response.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }