user UrlConnection to upload multipart

A

autogoor

I am wrting a client side program to simulate the web browser form
upload function. The goal is to send multipart content to server. I
found some code online. The basic idea is to send http header
information, then send file content as stream, and then read server
response. I found the the files are not even send to the server until
we read server response. This make my work hard, because I need to set
up a progressbar to monitor the uploading. I do not know where to put
it.

Here is my code...



URL u = new URL(urlString);
URLConnection c = u.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
c.setRequestProperty(
"Connection",
"Keep-Alive");
c.setRequestProperty(
"HTTP_REFERER",
codebase);
c.setRequestProperty(
"Content-Type",
"multipart/form-data; boundary=" + boundary);

DataOutputStream dstream = new
DataOutputStream(c.getOutputStream());
dstream.writeBytes("--" + boundary + "\r\n");

...post file and parameters
dstream.flush();
dstream.close();

//******if we do not read the server response as bellow, the files are
not upload to server.
try{
DataInputStream in = new DataInputStream(new
BufferedInputStream(c.getInputStream()));
String sIn = in.readLine();
while(sIn!=null){
if(sIn!=null){
System.out.println(sIn);
}
sIn = in.readLine();
}
}catch(Exception e){
e.printStackTrace();
}

}catch(Exception e){
e.printStackTrace();
}
}
 
R

Rhino

I am wrting a client side program to simulate the web browser form
upload function. The goal is to send multipart content to server. I
found some code online. The basic idea is to send http header
information, then send file content as stream, and then read server
response. I found the the files are not even send to the server until
we read server response. This make my work hard, because I need to set
up a progressbar to monitor the uploading. I do not know where to put
it.

Here is my code...



URL u = new URL(urlString);
URLConnection c = u.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
c.setRequestProperty(
"Connection",
"Keep-Alive");
c.setRequestProperty(
"HTTP_REFERER",
codebase);
c.setRequestProperty(
"Content-Type",
"multipart/form-data; boundary=" + boundary);

DataOutputStream dstream = new
DataOutputStream(c.getOutputStream());
dstream.writeBytes("--" + boundary + "\r\n");

...post file and parameters
dstream.flush();
dstream.close();

//******if we do not read the server response as bellow, the files are
not upload to server.
try{
DataInputStream in = new DataInputStream(new
BufferedInputStream(c.getInputStream()));
String sIn = in.readLine();
while(sIn!=null){
if(sIn!=null){
System.out.println(sIn);
}
sIn = in.readLine();
}
}catch(Exception e){
e.printStackTrace();
}

}catch(Exception e){
e.printStackTrace();
}
}
Have you had a look at the Common FileUpload package available at
http://jakarta.apache.org/commons/fileupload/? I think you'll find that it
simplifies the effort of doing file uploads in servlets (and applications)
quite a bit. I've had pretty good success with it so I can recommend it.

Rhino
 
A

autogoor

I am working with the client side. Not the server side. I am working on
how to send the http post, not how to parse it.
 
R

Rhino

I am working with the client side. Not the server side. I am working on
how to send the http post, not how to parse it.
Oops, sorry, I just saw "file upload" and missed the client-side bit. My
mistake!

Rhino
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top