Overcoming Image Upload Issue in JSP

Overcoming Image Upload Issue in JSP

  •  
  •  
  •  
  •   
  •  

File uploading is one of the main features from Flex to Server side. The uploaded file types would be most probably documents, texts or images. Flex supports all server side script files like ASP, JSP, PHP, DotNet, Coldfusion etc.

In Universe, there are various methods to upload images in JSP through Flex.  This article will discuss on the issues faced while uploading images and how to overcome those issues.

Work-Arounds:

Filtering out the image from other contents of the file is important.

Steps for Filtering:

The first step is to get the file content length and then, convert the file data into bytearray.

(eg)    int formDataLength=request.getContentLength();

byte imageData[]=new byte[formDataLength];

Then, the image content should be extracted from bytearray because it includes Image, Content-Type, Content-Disposition, Filename etc. The following step should be done to take out the starting and ending positions of the image content from bytearray while the middle portion becomes image data as bytearray.

(eg)       String file = new String(imageData);

int pos = file.indexOf(“filename=””);

pos = file.indexOf(“n”, pos) + 1;

pos = file.indexOf(“n”, pos) + 1;

pos = file.indexOf(“n”, pos) + 1;

int startPos = ((file.substring(0, pos)).getBytes()).length;

int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

The extracted bytearray of image is written out to a file.

(eg)     FileOutputStream fos=new java.io.FileOutputStream(Filename);

fos.write(dataBytes, startPos,(endPos-startPos));

Issue Faced:

The issue faced while uploading images from Flex to JSP is throwing an index out of bound exception.

Work-Around: Image upload using third party API :

This image upload issue can be solved by using a third party API called “Java Zoom”.
The third party API is invoked with the help of <jsp:useBean>tag as follows:

(eg)

<jsp:useBean scope=”page”>

<jsp:setProperty property=”folderstore” value=”path” />

</jsp:useBean>

API Implementation:

MultipartFormDataRequest class is used  to get the image file from Flex. By using “store” method, the image file can be stored in the specific folder.

(eg)

if (MultipartFormDataRequest.isMultipartFormData(request)) {

// Uses MultipartFormDataRequest to parse the HTTP request.

MultipartFormDataRequest mrequest = new MultipartFormDataRequest(

request);

upBean.store(mrequest);

}

Where to get the  API?

Here is the download link for the API:

http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html

, , , , , ,

Open chat
1
Chat with our Experts!
Hello
Can I help you?