如何使用REST角度上传图像?

时间:2014-05-15 15:40:49

标签: angularjs rest angular-file-upload

<input id="uploadImageId" type="file" accept="image/*" onChange="angular.element(this).scope().uploadPic(this);">

控制器:

$scope.uploadPic = function(input) {
    if (input.files && input.files[0]) {
            ContactService.uploadContactImage.upload({
                fileName : input.files[0].name
            }, input.files[0], function(data) {

            });
        }
    }
};

服务

            uploadContactImage : $resource('/services/api/contacts/uploadContactImage/:fileName', 
                    {
                        fileName : '@fileName'
                    }, {
                        upload : {
                            method : 'POST',
                            headers: {'Content-Type': 'multipart/form-data; boundary='}
                        }
                    })

API:

@POST
@Path("/uploadContactImage/{fileName}")
@Produces("application/json")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Map<String, Object> uploadContactImage(
                @PathParam("fileName") String fileName,
                @FormDataParam("file") InputStream uploadedInputStream,
                @FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
                @Context HttpServletRequest httpServletRequest) throws GaException {

    BufferedImage image = ImageIO.read(uploadedInputStream);
    ImageIO.write(image, "jpg", new File("/../../fileName));

}

输出:

java.lang.IllegalArgumentException: image == null!
at          javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
at javax.imageio.ImageIO.write(ImageIO.java:1520)

请指导我 我错过了任何在标题中传递的参数吗?或者我如何在angularjs中传递@FormDataParam?

1 个答案:

答案 0 :(得分:0)

我的解决方案是包括mimepull-1.9.4.jar。 http://mvnrepository.com/artifact/org.jvnet.mimepull/mimepull/1.9.4