URLConnection cannot flush

C

cmk128

Hi
I am using the following function to post a file to the web server.
But i found out the DataOutputStream::flush() cannot flush the data,
it seem to cache the whole query before send it out. Some people have
discovered this problem before : http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026745
But how to fix it? I need to add a progress bar to my app, so i need
to know how many byte have been send out.

thanks
from Peter ([email protected])

private boolean uploadFile(File file, String relativePath, int
rowNumber) {
try {
URL u = new URL("http://microsoft.com/test.php");
URLConnection c = u.openConnection();
// post multipart data
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
// set some request headers
c.setRequestProperty("Connection", "Keep-Alive");
c.setRequestProperty("Cache-Control", "no-cache");
c.setRequestProperty("HTTP_REFERER",
"http://pvs.kingofcoder.com/
test.php");
c.setRequestProperty("Content-Type",
"multipart/form-data; charset=utf-8;
boundary=****4353");
DataOutputStream dstream = new
DataOutputStream(c.getOutputStream());

// write currentpath
dstream.writeBytes("--****4353\r\n");
dstream.writeBytes(
"Content-Disposition: form-data; name=\"currentPath
\"\r\n\r\n");
//
dstream.writeBytes(URLEncoder.encode(this.getParameter("currentpath"),
"UTF-8"));
String currentPath = (getParameter("currentPath") ==
null) ? "" :
getParameter("currentPath");
dstream.writeBytes(currentPath);
dstream.writeBytes("\r\n");

// write relativePath
dstream.writeBytes("--****4353\r\n");
dstream.writeBytes(
"Content-Disposition: form-data; name=
\"relativePath\"\r\n\r\n");
dstream.writeBytes(relativePath);
dstream.writeBytes("\r\n");

// write filename
dstream.writeBytes("--****4353\r\n");
dstream.writeBytes(
"Content-Disposition: form-data; name=
\"uploadedFile\"; filename=\"" +
file.getName() +
"\"\r\nContent-Type: application/octet-stream\r\n\r
\n");

//write file content
FileInputStream fi = new FileInputStream(file);
byte[] bt = new byte[102400];
int cnt = fi.read(bt);
int numOfByteSent = cnt;
while (cnt == bt.length) {
dstream.write(bt, 0, cnt);
dstream.flush();
cnt = fi.read(bt);
numOfByteSent += cnt;
pMainTableModel.getStatus().set(rowNumber,

String.valueOf(numOfByteSent /
file.length()));
System.out.println(">" + numOfByteSent);
pMainTableModel.fireTableDataChanged();
}
dstream.write(bt, 0, cnt);
dstream.flush();
pMainTableModel.getStatus().set(rowNumber, "100");

dstream.writeBytes("\r\n--****4353--\r\n\r\n");
dstream.flush();
dstream.close();
fi.close();
// end write file content

try {
DataInputStream in =
new DataInputStream(c.getInputStream());
String sIn;
while ((sIn = in.readLine()) != null) {
if (sIn != null) {
System.out.println(sIn);
}
if (sIn.equals("upload success")) {
return true;
}
}
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
} catch (Exception ex2) {
ex2.printStackTrace();
return false;
}
}
 
E

Esmond Pitt

Set the chunk size. Otherwise the entire output stream is cached in
memory so that the Content-Length header can be set, and sent in one
write, making your progress bar futile.
 
D

Daniel Pitts

Esmond said:
Set the chunk size. Otherwise the entire output stream is cached in
memory so that the Content-Length header can be set, and sent in one
write, making your progress bar futile.
Or look into using Apache Commons httpclient. It gives you a lot more
fine-grained control, and tends to be more performent than the built in
http support.
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top