-5

I want to upload image file on server using servlet and i am not using HTML5. I have seen many questions of uploading image on stackoverflow but most of the answers are using PHP. I have tried it by reading image file at client side in java script using FileReader.readAsDataURL() method and then send this to server side and again make a .jpg file by decrypting it by BASE64. But the file made is not readable by computer. So please help me to solve this problem. Some other method for uploading are also appreciated. Please Answer me regarding JAVA JAVA JAVA. I also want to use AJAX :) Thank you.

I have done uploading Image file using servlet. It is working fine. I am trying to upload image using AJAX. I am calling same servlet through the AJAX request. But its not working. I am using common- fileupload.jar and common-io.jar for the same. Snippet looks like: List items = new FileUpload(new DiskFileItemFactory()) .parseRequest(request);
This code not working with Ajax I am sending dataForm object as data.

data = new FormData();
jQuery.each($('#file')[0].files, function(i, file) {
data.append('file-'+i, file);
});

The ajax request looks like:

$.ajax({
type : "POST",
data: data,
cache: false,
url : "/uploadImage/upload", 
contentType: false,    
processData: false,
success : function() {
alert("Done..!!");
}
});
Amit
  • 59
  • 3
  • 9
  • Please show some code. Btw: searching the web for 'java file upload' should help... – home Nov 29 '12 at 06:16
  • 2
    possible duplicate of [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet). Note that an image, in the end, is a file. – Luiggi Mendoza Nov 29 '12 at 06:18
  • Try this example : http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet – Carlos León Nov 29 '12 at 06:18
  • Side note: consider if you need Java OR Javascript+AJAX, you have very mixed set of tags. – Alexei Levenkov Nov 29 '12 at 06:23

1 Answers1

0

1) Create a html form first to sent multiparty request.you can see the blow link to create a html form to upload file http://www.caucho.com/resin-3.0/jsp/tutorial/multipart.xtp

2) Create a java dynamic web project and add a servert into it. take a look this sample http://helloworldprograms.blogspot.com/2010/12/dynamic-web-project-eclipse.html

3) create a servlet and use this code for uploading i am use com.oreilly.servlet.MultipartRequest library

private void uploadImage(HttpServletResponse response,HttpServletRequest request) throws IOException
{
    String dirName = request.getSession().getServletContext().getRealPath("/temp/");
    String fileName = null;
    MultipartRequest multi=null;

    try{
        multi= new MultipartRequest(request, dirName,1024*1024, "ISO-8859-1");
    }catch (IOException ex){
        ex.printStackTrace();

    }
    try{     
        Enumeration files = multi.getFileNames();

        while (files.hasMoreElements()){
            String name = (String) files.nextElement();
            fileName = multi.getFilesystemName(name);
        }
        String filePath = dirName +  System.getProperty("file.separator") + fileName;
        File clientImage = new File(filePath);



    } catch (Exception ioe){
        ioe.printStackTrace();
        logger.info("\nFile does not exist or cant delete\n");
    }       
}
Amir Qayyum Khan
  • 473
  • 2
  • 15
  • I have done uploading Image file using servlet. It is working fine. I am trying to upload image using AJAX. I am calling same servlet through the AJAX request. But its not working. – Amit Nov 29 '12 at 10:26
  • check out this link they are posting image via ajax http://css-tricks.com/ajax-image-uploading/ – Amir Qayyum Khan Nov 29 '12 at 11:50