Restlet Image Upload not getting ImageInfo

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

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(
        at org.apache.sanselan.Sanselan.getImageParser(

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


File Upload
Select File <input id="fileToUpload" name="fileToUpload" type="file" />
    <input type="submit" value="submit"/> 
<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>

The following Is my resource Post with restlet:

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();

            // 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 =;
                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("file name : ");
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(fi.openStream()));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                    result = new StringRepresentation(sb.toString(), MediaType.TEXT_PLAIN);
                    ImageInfo imageInfo =        Sanselan.getImageInfo(fi.openStream(), fi.getName());
                    ImageStatus imageStatus = fileUploadService.saveImage(fi);

        } else {
            // POST request with no entity.
    return result;

It is failing on the following line of code:

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