HTTPClient and Progressbar.

C

crazytazo

PostMethod filePost = new PostMethod(ui.value.getStrEIP());
filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE,
true);

try {
Part[] parts = {
new StringPart("FILENAME", file.m_File.getName()),
new StringPart("DIRFLAG", String.valueOf(0)),
new StringPart("FILEINDEX", String.valueOf(jfidx)),
new StringPart("FILESAVEPATH", ui.value.getStrPath()),
new FilePart(file.m_File.getName(), file.m_File)
};

filePost.setRequestEntity(
new MultipartRequestEntity(parts,
filePost.getParams()));

HttpClient client = new HttpClient();

client.getHostConfiguration().setHost(ui.value.getStrEIP(), 80);
client.getHttpConnectionManager().
getParams().setConnectionTimeout(5000);

int status = client.executeMethod(filePost);

if (status == HttpStatus.SC_OK) {
System.out.println("Upload complete, response=" +
filePost.getResponseBodyAsString());
} else {
System.out.println("Upload failed, response=" +
HttpStatus.getStatusText(status));
}

} catch (Exception ex) {
System.out.println("ERROR: " + ex.getClass().getName() + "
" + ex.getMessage());
ex.printStackTrace();
} finally {
filePost.releaseConnection();
}

this is client code using apache.HTTPClient.

it's work well, but follow line is occur blocking until get response
code from server.
int status = client.executeMethod(filePost);

How to check writing bytes in this modules?
The bytes are need to represnt progressbar.
 
J

John B. Matthews

[...]
t work well, but [the] follow[ing] line block
until get [a] response code from server.

int status = client.executeMethod(filePost);

How [should I] check writing bytes in this module?
The bytes are need[ed] to repres[e]nt progress.


Consider using a SwingWorker:

<http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html>

There's a nice prime number example here with a JProgressBar:

<https://swingworker.dev.java.net/>
 
R

rabbits77

it's work well, but follow line is occur blocking until get response
code from server.
int status = client.executeMethod(filePost);

This is not what you seem to think it is.
status is a http response code and is, of course,
is blocking.
Here is a list of response codes if you are interested
http://kbs.cs.tu-berlin.de/~jutta/ht/responses.html
How to check writing bytes in this modules?
The bytes are need to represnt progressbar.

One way to do this is shown on this page which
happens to cover performance optimization
http://hc.apache.org/httpclient-3.x/performance.html
In particular I think something like this would work

HttpClient httpclient = new HttpClient();
GetMethod httpget = new GetMethod("http://www.myhost.com/");
try {
httpclient.executeMethod(httpget);
Reader reader = new InputStreamReader(
httpget.getResponseBodyAsStream(),
httpget.getResponseCharSet());

/*get the response byte by byte and update your progress bar
after each response
*/
} finally {
httpget.releaseConnection();
}
 
C

crazytazo

This is not what you seem to think it is.
status is a http response code and is, of course,
is blocking.
Here is a list of response codes if you are interestedhttp://kbs.cs.tu-berlin.de/~jutta/ht/responses.html


One way to do this is shown on this page which
happens to cover performance optimizationhttp://hc.apache.org/httpclient-3.x/performance.html
In particular I think something like this would work

   HttpClient httpclient = new HttpClient();
   GetMethod httpget = new GetMethod("http://www.myhost.com/");
   try {
     httpclient.executeMethod(httpget);
     Reader reader = new InputStreamReader(
             httpget.getResponseBodyAsStream(),
httpget.getResponseCharSet());

     /*get the response byte by byte and update your progress bar
       after each response
       */
   } finally {
     httpget.releaseConnection();
   }

Thanks rabbits77.

Your code is GET method.
Problem is POST method.

I have solved this problem by override FilePart.sendData(OutputStream
out)
 
C

crazytazo

[...]
t work well, but [the] follow[ing] line block
until get [a] response code from server.

 int status = client.executeMethod(filePost);
How [should I] check writing bytes in this module?
The bytes are need[ed] to repres[e]nt progress.

Consider using a SwingWorker:

<http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html>

There's a nice prime number example here with a JProgressBar:

<https://swingworker.dev.java.net/>


Thanks John.
It's very good!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top