Connecting Tech Pros Worldwide Help | Site Map

Image Upload using JSP as server side langauge

Newbie
 
Join Date: Nov 2007
Posts: 2
#1: Nov 28 '07
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         String contentType = request.getContentType ();
  3.  
  4.         if ((contentType != null) && (contentType.indexOf ("multipart/form-data") >= 0)) {
  5.             DataInputStream in = new DataInputStream (request.getInputStream ());
  6.             int formDataLength = request.getContentLength ();
  7.  
  8.             byte dataBytes[] = new byte[formDataLength];
  9.  
  10.  
  11.             int byteRead = 0;
  12.             int totalBytesRead = 0;
  13.             while (totalBytesRead < formDataLength) {
  14.                 byteRead = in.read (dataBytes, totalBytesRead, formDataLength);
  15.                 totalBytesRead += byteRead;
  16.             }
  17.  
  18.             String file = new String (dataBytes);
  19.  
  20.  
  21.             String saveFile = file.substring (file.indexOf ("filename=\"") + 10);
  22.  
  23.  
  24.             saveFile = saveFile.substring (0, saveFile.indexOf ("\n"));
  25.  
  26.  
  27.             saveFile = saveFile.substring (saveFile.lastIndexOf ("\\") + 1,saveFile.indexOf ("\""));
  28.  
  29.  
  30.  
  31.  
  32.             int lastIndex = contentType.lastIndexOf ("=");
  33.             String boundary = contentType.substring (lastIndex + 1,contentType.length ());
  34.  
  35.  
  36.  
  37.             int pos;
  38.             pos = file.indexOf ("filename=\"");
  39.  
  40.  
  41.             pos = file.indexOf ("\n", pos) + 1;
  42.  
  43.  
  44.  
  45.             pos = file.indexOf ("\n", pos) + 1;
  46.  
  47.  
  48.  
  49.             pos = file.indexOf ("\n", pos) + 1;
  50.  
  51.  
  52.  
  53.             int boundaryLocation = file.indexOf (boundary, pos) - 4;
  54.  
  55.  
  56.  
  57.             int startPos = ((file.substring (0, pos)).getBytes ()).length;
  58.             int endPos = ((file.substring (0, boundaryLocation)).getBytes ()).length;
  59.  
  60.             String[] fileName = saveFile.split ("\\.");
  61.             String file_extension = fileName[1];
  62.             imageName += "."+file_extension ;
  63.  
  64.  
  65.             FileOutputStream fileOut = new FileOutputStream (imageName);
  66.  
  67.  
  68.             fileOut.write (dataBytes, startPos, (endPos - startPos));
  69.             fileOut.flush ();
  70.             fileOut.close ();
  71.  
  72.         }
  73.  
Can some one please tell me why this is so? i couldn't figure it out
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 28 '07

re: Image Upload using JSP as server side langauge


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.
Newbie
 
Join Date: Nov 2007
Posts: 2
#3: Nov 29 '07

re: Image Upload using JSP as server side langauge


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.
Reply