Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

URLConnection cannot flush

Thread view: 
cmk128@hotmail.com - 19 Oct 2007 04:24 GMT
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 (cmk128@hotmail.com)

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;
       }
   }
Esmond Pitt - 19 Oct 2007 05:26 GMT
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.
Daniel Pitts - 21 Oct 2007 03:30 GMT
> 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.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.