List of usage examples for org.apache.http.entity FileEntity getContentLength
public long getContentLength()
From source file:com.googlecode.osde.internal.igoogle.IgHostingUtil.java
/** * Uploads a file to iGoogle./*from w w w. jav a 2 s . co m*/ * * @throws IgException */ public static void uploadFile(IgCredentials igCredentials, String sourceFileRootPath, String sourceFileRelativePath, String hostingFolder) throws IgException { // Prepare HttpPost. String httpPostUrl = URL_IG_GADGETS_FILE + igCredentials.getPublicId() + hostingFolder + sourceFileRelativePath + "?et=" + igCredentials.getEditToken(); logger.fine("httpPostUrl: " + httpPostUrl); HttpPost httpPost = new HttpPost(httpPostUrl); File sourceFile = new File(sourceFileRootPath, sourceFileRelativePath); String httpContentType = isTextExtensionForGadgetFile(sourceFileRelativePath) ? HTTP_PLAIN_TEXT_TYPE : HTTP.DEFAULT_CONTENT_TYPE; httpPost.setHeader(HTTP.CONTENT_TYPE, httpContentType); FileEntity fileEntity = new FileEntity(sourceFile, httpContentType); logger.fine("fileEntity length: " + fileEntity.getContentLength()); httpPost.setEntity(fileEntity); // Add cookie headers. Cookie PREF must be placed before SID. httpPost.addHeader(IgHttpUtil.HTTP_HEADER_COOKIE, "PREF=" + igCredentials.getPref()); httpPost.addHeader(IgHttpUtil.HTTP_HEADER_COOKIE, "SID=" + igCredentials.getSid()); // Execute request. HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse; try { httpResponse = httpClient.execute(httpPost); } catch (ClientProtocolException e) { throw new IgException(e); } catch (IOException e) { throw new IgException(e); } // Verify if the file is created. StatusLine statusLine = httpResponse.getStatusLine(); logger.fine("statusLine: " + statusLine); if (HttpStatus.SC_CREATED != statusLine.getStatusCode()) { String response = IgHttpUtil.retrieveHttpResponseAsString(httpClient, httpPost, httpResponse); throw new IgException("Failed file-upload with status line: " + statusLine.toString() + ",\nand response:\n" + response); } }
From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java
@Test @SuppressWarnings("deprecation") public void testFileEntity() throws Exception { File file = new File("src/test/resources/README.txt"); FileEntity entity = new FileEntity(file, "text/plain; charset=\"UTF-8\""); System.out.println(entity.getContentLength() + ""); System.out.println(entity.getContentType() + ""); String content = parseEntity(entity); System.out.println(content);/*from www. ja va 2 s. c om*/ }
From source file:org.apache.http.contrib.benchmark.HttpBenchmark.java
private void prepare() { // prepare http params params = getHttpParams(socketTimeout, useHttp1_0); host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); // Prepare requests for each thread request = new HttpRequest[threads]; if (postFile != null) { FileEntity entity = new FileEntity(postFile, contentType); contentLength = entity.getContentLength(); if (postFile.length() > 100000) { entity.setChunked(true);// w w w .j a v a 2 s . co m } for (int i = 0; i < threads; i++) { BasicHttpEntityEnclosingRequest httppost = new BasicHttpEntityEnclosingRequest("POST", url.getPath()); httppost.setEntity(entity); request[i] = httppost; } } else if (doHeadInsteadOfGet) { for (int i = 0; i < threads; i++) { request[i] = new BasicHttpRequest("HEAD", url.getPath()); } } else { for (int i = 0; i < threads; i++) { request[i] = new BasicHttpRequest("GET", url.getPath()); } } if (!keepAlive) { for (int i = 0; i < threads; i++) { request[i].addHeader(new DefaultHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE)); } } if (headers != null) { for (int i = 0; i < headers.length; i++) { String s = headers[i]; int pos = s.indexOf(':'); if (pos != -1) { Header header = new DefaultHeader(s.substring(0, pos).trim(), s.substring(pos + 1)); for (int j = 0; j < threads; j++) { request[j].addHeader(header); } } } } }