syum 0 Newbie Poster

Hello,
I have been working on file transfer in java using http POST. I'm able to to send text files to a server but I'm getting difficulty in sending binary data, like images, to a server. My main problem lies on how to use InputStream.read(buf, 0, dataLength). Here is my source code:

try {

                InputStream in = request.getInputStream();
                // ServletInputStream in = request.getInputStream();
                BufferedReader r = new BufferedReader(new InputStreamReader(in));

                StringBuffer buf = new StringBuffer();
                int formDataLength = request.getContentLength(); // get content length
                String mimeType = request.getContentType();
                byte[] buf2 = new byte[formDataLength]; //allocate bytes buffer
                Writer output = null; //writer
                //BufferedWriter output=null;
                File file = null;
                String line;
                String fname1 = null;
                String fname2 = null;
                String fileName = null;
                int j = 0, k = 0;
                int i = 0;
                int offset = 0;
                String b1 = "\r\n";
                String b2 = "--";
                String b3 = "*****";
                String openBoundary = b1 + b2 + b3;
                String closeBoundary = b1 + b2 + b3 + b2 + b1;

                //extract the file name from the header info
                // while (j < 3) {
                fname1 = r.readLine();
                if (fname1.startsWith("Content-Disposition")) {
                    fname2 = fname1.substring(fname1.lastIndexOf('/') + 1);
                    fileName = fname2.substring(0, fname2.length() - 1);
                    out.println(fileName);

                    //}
                    // j++;
                }
                int hdrLen = fname1.length();
                int openBndLen = openBoundary.length();
                int closeBndLen = closeBoundary.length();
                out.println("Header Len: " + hdrLen);
                out.println("OpenBoundary Len: " + openBndLen);
                out.println("CloseBoundary Len: " + closeBndLen);

                int startPos = hdrLen + openBndLen;
                int endPos = formDataLength - closeBndLen;

                out.println("startPos: " + startPos);
                out.println("endPos: " + endPos);

                // out.println("encoding: " + request.getCharacterEncoding());
                // out.println("Info: "+request.getMethod());

                out.println("File name: " + fileName);
                try {
                    file = new File("/home/syum/Desktop/temp/ReceivedFiles/" + fileName);
                    output = new BufferedWriter(new FileWriter(file));
                    out.println("here it is ");
                } catch (Exception e) {
                    out.println("Writer error: " + e.getMessage());
                }
                if (mimeType.startsWith("text/") || formDataLength == -1) {
                    out.println("Processing non-binary file...");
                    while ((line = r.readLine()) != null) {
                        buf.append(line);
                        buf.append("\n");
                    }
                    String s = buf.toString();
                    output.write(s);
                    output.flush();
                    output.close();

                } else { //read binary data
                    int bytesRead = 0;
                    int cnt = 0;
                    int dif = 0;
                    int ratio=0;
                     
                    //offset = startPos;
                    //bytesAvailable = in.available();
                    int temp = 0;
                    //bytesRead = in.read(buf2, offset, buf2.length);
                    //ratio=formDataLength/bytesRead;
                    //out.println("Bytes Read: "+ bytesRead);
                    while (offset < formDataLength) {
                             bytesRead = in.read(buf2, offset, buf2.length - offset);
                        
                        if (bytesRead == -1) {
                            break;
                        }
                        offset += bytesRead;                         
                        out.println("Offset: " + offset);                         
                    }
                    if (offset != formDataLength) {
                        out.println("Only read " + offset + " bytes; Expected " + formDataLength + " bytes");
                    }
                    out.println("buf len: " + buf2.length);
                    try {
                        FileOutputStream fout = new FileOutputStream(file);
                        //fout.write(buf2);
                        fout.write(buf2, startPos, endPos - startPos);
                        fout.flush();
                        fout.close();
                    } catch (Exception e) {
                        out.println("buffer writing error: " + e.toString());
                    }
                    output.close();
                }
                out.println(fileName + " Received Successfully! ");
                out.println("Content Type: " + mimeType);
                out.println("Data length: " + formDataLength);
                out.println("Offset length: " + offset);

            } catch (Exception e) {
                PrintWriter responseWriter = response.getWriter();
                responseWriter.println("Poor seyoum, check your code!");
            }

When i run this program, the in.read reads a lesser byte length than expected. I tried every thing to fix this but to no avail. I'm stuck at this point and i really appreciate your help,
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.