【问题标题】:File upload with Jersey - FormDataContentDisposition is null使用 Jersey 上传文件 - FormDataContentDisposition 为空
【发布时间】:2017-09-18 01:27:41
【问题描述】:

我很清楚有很多与这个相同的问题,但没有一个对我有帮助,因为我的 HTML 文件名与 JavaScript 和 Java 中的相同。这是 JavaScript 代码:

        var fd = new FormData();
        fd.append('firstName', $scope.user.firstName);
        fd.append('lastName', $scope.user.lastName);
        fd.append('phoneNumber', $scope.user.phoneNumber);
        fd.append('email', $scope.user.email);
        fd.append('username', $scope.user.username);
        fd.append('password', $scope.user.password);
        fd.append('file', $scope.user.profilePicture);
        fd.append('file', $scope.user.profilePicture.name);
        console.log("Image:" + $scope.user.profilePicture);
        console.log("Image name:" + $scope.user.profilePicture.name);

当我 console.log("Image name:" + $scope.user.profilePicture.name); 时,一切似乎都很好,但是当我发送请求时,我得到了 null。这是Java代码:

    @Path("/register")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response registerUser(
            @FormDataParam("firstName") String firstName,
            @FormDataParam("lastName") String lastName,
            @FormDataParam("phoneNumber") String phoneNumber,
            @FormDataParam("email") String email,
            @FormDataParam("username") String username,
            @FormDataParam("password") String password,
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDescription,
            @Context ServletContext servletContext) throws JsonGenerationException, JsonMappingException, IOException {


        String uploadedFileLocation = servletContext.getRealPath("/resources/images")+"/"+fileDescription.getFileName();

        writeToFile(uploadedInputStream, uploadedFileLocation);

        String imgPath = "http://localhost:8080/WebProjekat/resources/images/" + fileDescription.getFileName();
        User user = new User(username, password,firstName,lastName,phoneNumber,email,imgPath);

        if(userService.addUser(user)) {
            return Response.status(Status.CREATED)
                    .entity(user)
                    .build();
        }else 
            return Response.status(Status.NOT_ACCEPTABLE)
                    .entity(user)
                    .build();

    }

不知何故,FormDataContentDisposition fileDescription 始终为空。这是 HTML 代码:

<div class="form-group">

    <img style="max-height: 250px; max-width: 200px;" src="app/image_resources/guest.png" id="profileImg"></img>

    <input type="button" class="btn btn-primary" style="width: 100%;height: 30px; display: flex;align-items: center;justify-content: center;" value="Load Image" onclick="$('#loadProfileImg').click();" />

    <input type="file" accept="image/*" class="form-control" id="loadProfileImg" ng-required="true" onchange="angular.element(this).scope().readURL(this);" style="display:none;" name="file" />

如你所见,我已经命名了

<input type='file'>

就像我在 JavaScript 中做的关键一样。我还将它包含在我的 web.xml 中:

<init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param> 

我们将不胜感激任何形式的帮助。提前谢谢你们!

更新:我已经修复了null问题,我不小心写了:

fd.append('file', $scope.user.profilePicture);
        fd.append('file', $scope.user.profilePicture.name);

代替:

fd.append('file', $scope.user.profilePicture, $scope.user.profilePicture.name);

但遗憾的是,我还有另一个问题。现在我的 img 路径是正确的,但我得到 404 NOT FOUND 即使我在要上传的文件夹中看到图像名称,但我看不到图像本身。我在另外 2 个不同的对象上使用此代码,它工作正常。有什么想法吗?

【问题讨论】:

    标签: javascript java rest jersey


    【解决方案1】:

    读取 url 附加表单数据。

    <input type="file" class="form-control" id="file" ng-model="file"
           onchange="angular.element(this).scope().getTheFiles(this)" /> 
    

    控制器

    var formdata = new FormData();
    $scope.getTheFiles = function(element) {
        $scope.$apply(function($scope) {
            $scope.files = element.files;
            for (var i = 0; i < element.files.length; i++) {
                formdata.append('files', element.files[i]);
            }
        });
    };
    

    接下来发送网址

    Java 端获取类似@FormDataParam("files") List&lt;FormDataBodyPart&gt; 文件的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-25
      • 2015-06-07
      • 2015-12-07
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多