Greetings,

I'm running Tomcat with Apache and I have a problem of "missing bytes".
Let me explain. My applet needs to send binary data to a servlet. It
appears that if I send more than about 1k bytes at once Tomcat throws an
exception. I need to get the applet-servlet communications ASAP, so i
decided to send the data in 1k chunks before trying to trace why the
exception is thrown. As is seen in the code below I'm setting the
Content-Length header in the applet to 1024 but when in the servlet I
check that header it is few bytes short. Can someone explain why?

Applet
------
   // data in byte array called "data" is compressed
   ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFSZ);
   GZIPOutputStream gos = new GZIPOutputStream(baos);
   gos.write(data, 0, data.length);
   gos.finish();
   byte[] zip = baos.toByteArray();
   showStatus("ZIP size: " + zip.length);

   url = new URL(...);
   int from = 0;
   int to = 0;
   byte[] buffer = new byte[BUFFSZ];
   while (from < zip.length) {
       to = Math.min(zip.length, to+BUFFSZ);
       System.arraycopy(zip, from, buffer, 0, to-from);
       send(buffer);
       from = to;
   }
.......
   private void send(byte[] b) {
       try {
           URLConnection con = url.openConnection();
           con.setUseCaches(false);
           con.setDoOutput(true);
           con.setDoInput(true);
           con.setRequestProperty("Content-Type",
"application/octet-stream");
           con.setRequestProperty("Content-Length",
String.valueOf(b.length));
           con.getOutputStream().write(b);
           con.getOutputStream().flush();
           BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
           String s;
           while ((s = in.readLine()) != null)
           System.out.println(s);
       } catch (IOException e) {
           e.printStackTrace();
       }
..........

Servlet
-------
    public void doPost(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        if
(req.getHeader("Content-Type").equals("application/octet-stream")) {
            try {
                ServletInputStream sis = req.getInputStream();
                int n =
Integer.parseInt(req.getHeader("Content-Length"));
                FileOutputStream fos = new FileOutputStream(fn, true);
                out.println("Received content-length: " + i);
                byte[] buffer = new byte[n];
                int sent = 0;
                while ((n = sis.readLine(buffer, 0, n)) != -1){
                    fos.write(buffer, 0, n);
                    sent += n;
                }
                out.println("Received " + sent + " bytes");
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace(out);
            }

            ......


I have also tried reading a one character at a time from BufferedReader
seems to hang(!)
    BufferedReader br = req.getBufferedReader();
    for (i = 0; i < Integer/parseInt(req.getHeader("Content-Length"));
i++)
        int b = (int) br.read()

I also tried reading Content-Length bytes from ServletInputStream but
that also doesn't work using SertvletInputStream.readLine(buffer, 0,
contlen) but that always reads less that Content-Length.

So, how does one do it with servlets? TIA,
--
Aaron Stromas    | "Tick-tick-tick!!!... ja, Pantani is weg..."
Oracle Corp      |                             BRTN commentator
+1 703.708.68.21 |                              L'Alpe d'Huez
                                            1995 Tour de France

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to