1
<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='}
                        }
                    })

接口:

@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?

4

1 回答 1

0

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

于 2014-09-16T01:16:01.110 回答