Java httppost file post print upload speed - java

With this java function would upload files to the server.
while the upload is in progress, upload speed and the size of the data sent How do I print?
public void PostFile() {
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
File file = new File("D:/sqldata.zip");
HttpPost httppost = new HttpPost("http://localhost/upload.php");
HttpEntity httpEntity = MultipartEntityBuilder.create()
.addBinaryBody("userfile", file, ContentType.create("application/zip"), file.getName())
.build();
httppost.setEntity(httpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
} catch (Exception e) {
System.out.println("hata: "+e);
}
}

Thank you to everyone. I solved the problem. In this way, you can print the data sent instantly sent.
main.java
public void post(String url, File sendFile) throws UnsupportedEncodingException, IOException {
long start = System.nanoTime();
HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost(url);
MultipartEntity reqEntity = new MultipartEntity(){
ProgressListener listener;
public void writeTo(final OutputStream outstream) throws IOException {
super.writeTo(new UploadStream(outstream,listener));
}
};
reqEntity.addPart("userfile", new FileBody(sendFile));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
if (response != null) {
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
}
}
UploadStream.java
public class UploadStream extends FilterOutputStream {
final ProgressListener listener;
private long transferred;
public UploadStream(final OutputStream out, final ProgressListener listener)
{
super(out);
this.listener = listener;
this.transferred = 0;
}
public void write(byte[] b, int off, int len) throws IOException
{
out.write(b, off, len);
this.transferred += len;
System.out.println(this.transferred/1024+" KB");
}
public void write(int b) throws IOException
{
out.write(b);
this.transferred++;
}
}

Related

com.sun.net.httpserver doesn't work with org.apache.http.client

I make two requests from one client to the server and client hangs on second execute of method httpclient.execute(httpPost); if I create new client for each request everything is OK. If I make POST to google(for example) everything is OK. What's the problem?
Server:
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 8080),2);
server.setExecutor(Executors.newFixedThreadPool(5));
HttpContext context = server.createContext("/", new GlobalHandler());
context.getFilters().add(new GlobalFilter());
server.createContext("/setup.exe", new GetFileHandler());
server.start();
}
private static class GlobalHandler implements HttpHandler {
#Override
public void handle(HttpExchange t) throws IOException {
Headers headers = t.getRequestHeaders();
System.out.println(headers.entrySet());
System.out.println(t.getHttpContext());
System.out.println(t.getRequestURI());
System.out.println(t.getProtocol());
System.out.println(t.getRequestMethod());
String resp = "Hello";
if (Objects.equals(t.getRequestMethod(), "POST")) {
System.out.println("POST");
Map params = (Map) t.getAttribute("parameters");
resp = params.toString();
System.out.println(params);
}
byte[] bytesToWrite = resp.getBytes("UTF-8");
t.sendResponseHeaders(200, bytesToWrite.length);
OutputStream os = t.getResponseBody();
os.write(bytesToWrite);
}
}
Client:
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1 * 1000).build();
httpclient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
for(int i= 0;i<2;i++) {
HttpPost httpPost = new HttpPost("http://google.com");
httpPost.addHeader("Connection","keep-alive");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "вг"));
nvps.add(new BasicNameValuePair("password", "аб"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
if(entity2.getContentLength() >0) {
byte[] content = new byte[(int) entity2.getContentLength()];
entity2.getContent().read(content);
System.out.println(new String(content, "UTF-8"));
}
EntityUtils.consume(entity2);
} finally {
response2.close();
}
}
httpclient.close();

HttpPost file upload status progress bar

HttpPost showing the file upload status, I want to make progressbar. How can I do.
thanks
public void post(String url, File sendFile) throws UnsupportedEncodingException, IOException {
HttpParams params = new BasicHttpParams();
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
HttpPost post = new HttpPost(url);
MultipartEntity multiEntity = new MultipartEntity();
multiEntity.addPart("userfile", new FileBody(sendFile));
post.setEntity(multiEntity);
HttpResponse response = client.execute(post);
if (response != null) {
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
}
Not sure if apache httpclient has a ready-made solution for this but you could use an InputStreamBody (instead of FileBody) and wrap the FileInputStream in something that counts how much is already read. Compare this to the size of the file to see how far along you are.

grails receive file via MultipartEntity

I use this httpclient to post a image to my grails as follows. How do I receive the file in grails?
public static String webPost(String method, File data, Context c) throws Exception {
String json = "";
if (isOnline(c) == true) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
HttpPost httppost ;
try {
httppost = new HttpPost(method);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new FileBody(data));
httppost.setEntity(entity);
response = httpclient.execute(httppost);
if (response != null) {
HttpEntity r_entity = response.getEntity();
json = EntityUtils.toString(r_entity);
Log.i("ja", json);
}
} catch (Exception e) {
throw new RuntimeException(e.getLocalizedMessage());
} finally {
httpclient = null;
response = null;
httpclient = null;
}
} else {
throw new RuntimeException("No internet connection");
}
return json;
}
My grails:
def image = request.getFile('image')
image.transferTo(new File('c:/p.png') )
Error:
groovy.lang.MissingMethodException: No signature of method: org.apache.catalina.core.ApplicationHttpRequest.getFile() is applicable for argument types: (java.lang.String) values: [image]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getInfo(), recycle()
at mclient.TestController$_closure1.doCall(TestController.groovy:10)
at mclient.TestController$_closure1.doCall(TestController.groovy)
at java.lang.Thread.run(Thread.java:662)

DefaultClientConnection automatically gets shutdown

I am new to HttpClient. I am using DefaultHttpClient (which is said to be thread-safe).
In my app I have created two threads which have been scheduled to execute every 10 minutes simultaneously. Sometimes I found that the DefaultClientConnection automatically gets shutdown.
What could be the possible reasons?
Though DefaultHttpClient is thread-safe, shall I need to use PoolingClientConnectionManager in this scenario ?
You should use PoolingClientConnectionManager.
Also you must use IdleConnectionMonitorThread for monitor idle connection.
some code from my source :
private final PoolingClientConnectionManager connectionManager;
private final IdleConnectionMonitorThread connectionMonitorThread = null;
private final DefaultHttpClient httpclient;
initialization:
final HttpParams params = new BasicHttpParams();
final HttpProtocolParamBean paramsBean = new HttpProtocolParamBean(params);
paramsBean.setVersion(HttpVersion.HTTP_1_1);
paramsBean.setContentCharset("UTF-8");
paramsBean.setUseExpectContinue(false);
params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false);
params.setIntParameter("http.socket.timeout", 20000);
params.setIntParameter("http.connection.timeout", 30000);
params.setBooleanParameter("http.protocol.handle-redirects", true);
params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);
params.setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 32 * 1024);
params.setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
// params.setParameter("http.useragent", "Crawler Airupt(http://www.airupt.com/)");
params.setParameter("http.useragent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1");
params.setParameter("http.language.Accept-Language", "en-us");
params.setParameter("http.protocol.content-charset", "UTF-8");
params.setParameter("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
params.setParameter("Cache-Control", "max-age=0");
final SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, socketFactory/* SSLSocketFactory.getSocketFactory() */));
connectionManager = new PoolingClientConnectionManager(schemeRegistry);
connectionManager.setDefaultMaxPerRoute(500000);
connectionManager.setMaxTotal(2000000);
httpclient = new DefaultHttpClient(connectionManager, params);
httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
#Override
public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
final HeaderElementIterator it = new BasicHeaderElementIterator(response
.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
final HeaderElement he = it.nextElement();
final String param = he.getName();
final String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (final NumberFormatException ignore) {
}
}
}
return 30 * 1000;
}
});
httpclient.setRedirectStrategy(new DefaultRedirectStrategy());
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
#Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}
});
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
#Override
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
response.setEntity(new BufferedHttpEntity(response.getEntity()));
final HttpEntity entity = response.getEntity();
final Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
final HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
});
startConnectionMonitorThread();
few addition methods:
private synchronized void startConnectionMonitorThread() {
if (connectionMonitorThread == null) {
connectionMonitorThread = new IdleConnectionMonitorThread(connectionManager);
}
connectionMonitorThread.start();
}
private synchronized void stopConnectionMonitorThread() {
if (connectionMonitorThread != null) {
connectionMonitorThread.shutdown();
connectionManager.shutdown();
}
}
public void shutdown() {
stopConnectionMonitorThread();
final ClientConnectionManager cm = httpclient.getConnectionManager();
if (cm != null) {
httpclient.getConnectionManager().shutdown();
}
}
Using:
final HttpGet httpGet = new HttpGet(url);
final HttpResponse response = httpclient.execute(httpGet);
final StatusLine statusLine = response.getStatusLine();
final int responseCode = statusLine.getStatusCode();
if (responseCode >= 300) {
logger.error(" {}. Received statusCode {}", url, responseCode);
httpGet.abort();
//throw some exception;
}
final HttpEntity entity = response.getEntity();
if (entity == null) {
//throw some exception or ignore;
}
responseBody = EntityUtils.toString(entity);
This code/params optimized for Crawler. to fast receive a lot of pages. with using gzip(if possible) and https if require without using cookies. For addition cookies you need to add cookieStore like httpclient.setCookieStore();

Gzip decompressing returns random characters

I'm trying to download a XML file that is gzip compressed from a server for that I use the following code:
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
HttpGet response = new HttpGet(urlData);
client.addRequestInterceptor(new HttpRequestInterceptor() {
#Override
public void process(HttpRequest request, HttpContext context) {
// Add header to accept gzip content
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}
});
client.addResponseInterceptor(new HttpResponseInterceptor() {
#Override
public void process(HttpResponse response, HttpContext context) {
// Inflate any responses compressed with gzip
final HttpEntity entity = response.getEntity();
final Header encoding = entity.getContentEncoding();
if (encoding != null) {
for (HeaderElement element : encoding.getElements()) {
if (element.getName().equalsIgnoreCase("gzip")) {
response.setEntity(new InflatingEntity(response.getEntity()));
break;
}
}
}
}
});
ResponseHandler<String> responseHandler = new BasicResponseHandler();
return client.execute(response, responseHandler);
InflatingEntity method:
private static class InflatingEntity extends HttpEntityWrapper {
public InflatingEntity(HttpEntity wrapped) {
super(wrapped);
}
#Override
public InputStream getContent() throws IOException {
return new GZIPInputStream(wrappedEntity.getContent());
}
#Override
public long getContentLength() {
return -1;
}
}
If I remove everything related to Gzip compression and replace the compressed XML file from the server with a normal XML everything works fine, but after I implement the Gzip compression I get the compressed string:
Does anyone knows what is missing in my code to get the decompressed XML?
I have solved the problem, my response didn't have an entity so the code was not decompressing the response since that part of the code was not being reached, here is the modification in the responseinterceptor:
client.addResponseInterceptor(new HttpResponseInterceptor() {
#Override
public void process(HttpResponse response, HttpContext context) {
response.setEntity(new InflatingEntity(response.getEntity()));
}
});

Categories