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();
}
}
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();
}
}