Image Upload using JSP as server side langauge

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wazzz
    New Member
    • Nov 2007
    • 2

    Image Upload using JSP as server side langauge

    Hi,

    I am using the following code to upload an image, i am able to upload the image and save in DB on Windows but the code fails somehow on Linux machine. Here is the code:

    Code:
     
            String contentType = request.getContentType ();
                 
            if ((contentType != null) && (contentType.indexOf ("multipart/form-data") >= 0)) {
                DataInputStream in = new DataInputStream (request.getInputStream ());
                int formDataLength = request.getContentLength ();
                
                byte dataBytes[] = new byte[formDataLength];
                
              
                int byteRead = 0;
                int totalBytesRead = 0;
                while (totalBytesRead < formDataLength) {
                    byteRead = in.read (dataBytes, totalBytesRead, formDataLength);
                    totalBytesRead += byteRead;
                }
                
                String file = new String (dataBytes);
                
            
                String saveFile = file.substring (file.indexOf ("filename=\"") + 10);
                
             
                saveFile = saveFile.substring (0, saveFile.indexOf ("\n"));
                
                        
                saveFile = saveFile.substring (saveFile.lastIndexOf ("\\") + 1,saveFile.indexOf ("\""));
                
             
                
        
                int lastIndex = contentType.lastIndexOf ("=");
                String boundary = contentType.substring (lastIndex + 1,contentType.length ());
                
            
                
                int pos;
                pos = file.indexOf ("filename=\"");
                
             
                pos = file.indexOf ("\n", pos) + 1;
                
                      
                
                pos = file.indexOf ("\n", pos) + 1;
                
                          
                
                pos = file.indexOf ("\n", pos) + 1;
                
           
                
                int boundaryLocation = file.indexOf (boundary, pos) - 4;
                
            
                
                int startPos = ((file.substring (0, pos)).getBytes ()).length;
                int endPos = ((file.substring (0, boundaryLocation)).getBytes ()).length;
                
                String[] fileName = saveFile.split ("\\.");
                String file_extension = fileName[1];
                imageName += "."+file_extension ;
                
        
                FileOutputStream fileOut = new FileOutputStream (imageName);
                
                          
                fileOut.write (dataBytes, startPos, (endPos - startPos));
                fileOut.flush ();
                fileOut.close ();
                
            }
    Can some one please tell me why this is so? i couldn't figure it out
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Unix system use the forward slash / for path separators.
    Read this page and you should get all that you need.
    Pay special attention to file.separator.
    Last edited by r035198x; Nov 28 '07, 08:52 AM. Reason: code tags bug had struck again

    Comment

    • Wazzz
      New Member
      • Nov 2007
      • 2

      #3
      Thanx for the reply but thats not the issue Image is being saved at the location specified in Linux as well. The problem is in Linux somehow the endPos (end Position is not being identified correctly). so when writing the byte array, program throws index out of bound position.

      Comment

      • Nie powiem
        New Member
        • Jan 2011
        • 1

        #4
        Try this:

        String file = new String(dataByte s,"ISO-8859-1");

        int startPos = ((file.substrin g(0, pos)).getBytes("ISO-8859-1")).length;

        int endPos = ((file.substrin g(0, boundaryLocatio n)).getBytes("ISO-8859-1")).length;

        Comment

        • Bhavesh
          New Member
          • Jul 2012
          • 1

          #5
          thanks Nie powiem

          Thanks u very much Nie powiem . u save my lot of time ..




          Originally posted by Nie powiem
          Try this:

          String file = new String(dataByte s,"ISO-8859-1");

          int startPos = ((file.substrin g(0, pos)).getBytes("ISO-8859-1")).length;

          int endPos = ((file.substrin g(0, boundaryLocatio n)).getBytes("ISO-8859-1")).length;

          Comment

          • saumendra0022
            New Member
            • Oct 2012
            • 1

            #6
            Thank you sir....Your post helped and saved me.......again thanks...really it worked....

            Comment

            Working...