java - Restlet Image Upload not getting ImageInfo -
i have simple html form ajax call restlet resource upload image.
when sanselon.getimageinfo following error:
caused by: org.apache.sanselan.imagereadexception: couldn't read magic numbers guess format. @ org.apache.sanselan.sanselan.guessformat(sanselan.java:147) @ org.apache.sanselan.sanselan.getimageparser(sanselan.java:596) .... i'm @ loss. i'm getting file name. appreciated.
the following html:
<html> <body> <form> <table> <tr> <td colspan="2">file upload</td> </tr> <tr> <th>select file </th> <td><input id="filetoupload" name="filetoupload" type="file" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="submit"/> </td> </tr> </table> </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 resource post 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 factory disk-based file items diskfileitemfactory factory = new diskfileitemfactory(); factory.setsizethreshold(1000240); // 2/ create new file upload handler based on restlet // fileupload extension parse restlet requests , // generates fileitems. restletfileupload upload = new restletfileupload(factory); // 3/ request parsed handler generates // list of fileitems fileitemiterator fileiterator = upload.getitemiterator(entity); //filewriter fstream = null; // process uploaded item called "filetoupload" // , return boolean found = false; while (fileiterator.hasnext() && !found) { fileitemstream fi = fileiterator.next(); if (fi.getfieldname().equals("filetoupload")) { found = true; // consume stream immediately, otherwise stream // 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 no entity. setstatus(status.client_error_bad_request); } } return result; } it failing on following line of code:
imageinfo imageinfo = sanselan.getimageinfo(fi.openstream(), fi.getname());
i think code on point consume stream of uploaded stream 3 times:
- the first time completing stringbuilder (which totally useless in case, because image file binary stuff, not character-based stuff),
- the second time getting image info using sanselan,
- the third time saving image using fileuploadservice
as can read socket once, there issue. can remove first block of code? allow imageinfo, think. i'm little bit more reserved regarding "save" operation, think fail.
Comments
Post a Comment