Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Applet/servlet communication for byte transmission

843807Sep 19 2006 — edited Jul 6 2007
Hello all !

I wrote an applet to transfer binary file from web servlet (running under Tomcat 5.5) to a client (it's a signed applet) but I have a problem of interpretation of byte during transmission.

the code of the servlet is :
        response.setContentType("application/octet-stream");
        ServletOutputStream sos = response.getOutputStream();
        FileInputStream fis = new FileInputStream(new File(
                "C:\\WINDOWS\\system32\\setup.bmp"));
        byte[] b = new byte[1024];
        int nbRead = 1;
        while (nbRead > 0) {

            nbRead = fis.read(b);
            System.out.println("octets lus = " + nbRead);
            sos.write(b, 0, nbRead-1);
        }
        fis.close();
        sos.close();
et le code de l'applet qui appelle cette servlet est :
        URL selicPortal = null;
        try {
            selicPortal = new URL(
                    "http://localhost:8080/AppletTest/servlet/FileManipulation");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        URLConnection selicConnection = null;
        try {
            selicConnection = selicPortal.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        

        selicConnection.setDoInput(true);
        selicConnection.setDoOutput(true);
        selicConnection.setUseCaches(false);
        selicConnection.setRequestProperty("Content-Type",
                "application/octet-stream");
 
        try {

            InputStream in = selicConnection.getInputStream();
            FileOutputStream fos = new FileOutputStream(new File(tempDir
                    + "\\toto.bmp"));
            byte[] b = new byte[1024];
            int nbRead = in.read(b);
            while (nbRead > 0) {

                fos.write(b);
             }
            in.close();
            fos.close();
         } catch (IOException ioe) {

            ioe.printStackTrace();
        }
the file dowloaded is broken. it seems that bytes 01 00 or 00 01 are not correctly process.

Some ideas to help me please ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 3 2007
Added on Sep 19 2006
3 comments
106 views