邮递员使用球衣 2.0 对多部分/表单数据图像上传提出 400 错误请求 [英] Postman throwing 400 Bad request for multipart/form-data image upload with jersey 2.0

查看:36
本文介绍了邮递员使用球衣 2.0 对多部分/表单数据图像上传提出 400 错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网址:http://localhost:8080/RESTfulExample/rest/file/upload方法:发布

回复:

相同的代码正在处理 html 表单,但在邮递员中它抛出 400 BAD REQUEST,我在谷歌上查找解决方案,发现边界丢失,如何解决?因为我必须通过 Jquery 和 rest 客户端从多个客户端(如移动应用程序和 Web 客户端)接收文件.

The same code is working with html forms but in postman it's throwing 400 BAD REQUEST, I looked up on google for solution and found that boundary is missing, How to resolve it ? As I have to recieve files from multiple clients like mobile application and web clients via Jquery and rest client.

@Path("/file")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) {

        try {
            String uploadedFileLocation = "/home/nash/" + fileDetail.getFileName();

            // save it
            writeToFile(uploadedInputStream, uploadedFileLocation);

            String output = "File uploaded to : " + uploadedFileLocation;
            System.out.println("File uploaded..........");

            return Response.status(200).entity(output).build();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception " + e);
            return null;
        }

    }

    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {

        try {
            OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}

推荐答案

请按以下步骤操作:

  1. 添加 jersey-multipart 依赖项.

在您的应用程序类中(或在 web.xml 中)启用 MultipartFeature.class.

In Your Application Class (or in web.xml) enable MultipartFeature.class.

请勿在您的邮递员请求中添加 Content-Type 标头.

DO NOT Add Content-Type header in your postman request.

对我来说,上述步骤有效.请让我知道这是否对您有帮助.

For me the above steps worked. Do let me know if that helped you or not.

这篇关于邮递员使用球衣 2.0 对多部分/表单数据图像上传提出 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆