Restlet Image Upload not getting ImageInfo

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Restlet Image Upload not getting ImageInfo

Oliver Gottwald
This post has NOT been accepted by the mailing list yet.
I currently have a simple HTML form with AJAX call to Restlet Resource to upload a image.

When I do the Sanselon.getImageInfo I get the following error:

Caused by: org.apache.sanselan.ImageReadException: Couldn't read magic numbers to guess format.
        at org.apache.sanselan.Sanselan.guessFormat(Sanselan.java:147)
        at org.apache.sanselan.Sanselan.getImageParser(Sanselan.java:596)
        ....

I'm at a loss. I'm getting the file name. Any help would be greatly appreciated.
The following is my html:

<html>
<body>
<form>

 
   
 
File Upload
Select File <input id="fileToUpload" name="fileToUpload" type="file" />
    <input type="submit" value="submit"/> 
 
</form>
<script> $("form").submit(function(evt){ evt.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: 'http://localhost:8080/api/v1/fileupload', type: 'POST', data: formData, async: false, cache: false, contentType: false, enctype: 'multipart/form-data', processData: false, success: function (response) { alert(response); } }); return false; }); </script></body>
</html>

The following Is my resource Post with restlet:

@Post
public Representation accept(Representation entity) throws Exception {
    Representation result = null;
    if (entity != null) {
        if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
            // 1/ Create a factory for disk-based file items
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(1000240);

            // 2/ Create a new file upload handler based on the Restlet
            // FileUpload extension that will parse Restlet requests and
            // generates FileItems.
            RestletFileUpload upload = new RestletFileUpload(factory);

            // 3/ Request is parsed by the handler which generates a
            // list of FileItems
            FileItemIterator fileIterator = upload.getItemIterator(entity);

            //FileWriter fstream = null;

            // Process only the uploaded item called "fileToUpload"
            // and return back
            boolean found = false;
            while (fileIterator.hasNext() && !found) {
                FileItemStream fi = fileIterator.next();
                if (fi.getFieldName().equals("fileToUpload")) {
                    found = true;
                    // consume the stream immediately, otherwise the stream
                    // will be closed.
                    StringBuilder sb = new StringBuilder("media type: ");
                    sb.append(fi.getContentType()).append("\n");
                    sb.append("file name : ");
                    sb.append(fi.getName()).append("\n");
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(fi.openStream()));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                    }
                    result = new StringRepresentation(sb.toString(), MediaType.TEXT_PLAIN);
                    ImageInfo imageInfo =        Sanselan.getImageInfo(fi.openStream(), fi.getName());
                    System.out.println(imageInfo);
                    ImageStatus imageStatus = fileUploadService.saveImage(fi);

                }
            }
        } else {
            // POST request with no entity.
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        }
    }
    return result;
}

It is failing on the following line of code:

ImageInfo imageInfo = Sanselan.getImageInfo(fi.openStream(), fi.getName());