Restlet Image上传没有得到ImageInfo

时间:2015-09-30 16:47:49

标签: java ajax restlet image-upload

我目前有一个简单的HTML表单,使用AJAX调用Restlet Resource来上传图像。

当我执行Sanselon.getImageInfo时,出现以下错误:

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)
        ....

我很茫然。我正在获取文件名。任何帮助将不胜感激。

以下是我的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>

以下是我的资源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;
}

以下代码行失败:

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

1 个答案:

答案 0 :(得分:0)

我认为您的代码可以使用已上传的流的流三次:

  • 第一次完成一个StringBuilder(在你的情况下完全没用,因为图像文件是二进制的东西,而不是基于字符的东西),
  • 第二次使用sanselan获取图片信息,
  • 第三次使用fileUploadService
  • 保存图像

由于我们只能读取套接字一次,因此存在问题。 你能删除第一个代码块吗?我想这会让你得到ImageInfo。关于&#34;保存&#34;我有点保留。操作,我认为它会失败。