使用多部分数据引发异常的Java Restful web服务方法

使用多部分数据引发异常的Java Restful web服务方法,java,android,file,jax-rs,restful-architecture,Java,Android,File,Jax Rs,Restful Architecture,我正在尝试将图像从android设备上传到服务器。我的Web服务方法正在被调用,但它引发了一些异常。我在网上搜索了几个小时,但没有找到任何解决办法。我第一次使用multipart。 我已更改我的web服务方法。[已编辑] 这是我的android客户端代码 protected Boolean doInBackground(String... urls) { try{ URL urlImage = new URL("http://10.0.2.2:8080/L

我正在尝试将图像从android设备上传到服务器。我的Web服务方法正在被调用,但它引发了一些异常。我在网上搜索了几个小时,但没有找到任何解决办法。我第一次使用multipart。 我已更改我的web服务方法。[已编辑] 这是我的android客户端代码

    protected Boolean doInBackground(String... urls) {
      try{
          URL urlImage = new URL("http://10.0.2.2:8080/LostLove_services/love/Recipe/coverpic");
      HttpURLConnection urlConnectionImage = (HttpURLConnection) urlImage.openConnection();

    ImageButton coverImage = (ImageButton)findViewById(R.id.CoverPic);

     Bitmap bitmap = ((BitmapDrawable)coverImage.getDrawable()).getBitmap();


             urlConnectionImage.setUseCaches(false);
              urlConnectionImage.setDoOutput(true);
             urlConnectionImage.setDoInput(true);

            urlConnectionImage.setRequestMethod("POST");
            urlConnectionImage.setRequestProperty("Connection", "Keep-Alive");
            urlConnectionImage.setRequestProperty("Cache-Control", "no-cache");
           urlConnectionImage.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + this.boundary);
          DataOutputStream request = new DataOutputStream(urlConnectionImage.getOutputStream());

           request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
                        request.writeBytes("Content-Disposition: form-data; name=\"" + this.attachmentName + "\";filename=\"" + this.attachmentFileName + "\"" + this.crlf);
                        request.writeBytes(this.crlf);
                     /*   byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight()];
                        for (int i = 0; i < bitmap.getWidth(); ++i) {
                            for (int j = 0; j < bitmap.getHeight(); ++j) {
                                //we're interested only in the MSB of the first byte,
                                //since the other 3 bytes are identical for B&W images
                                pixels[i + j] = (byte) ((bitmap.getPixel(i, j) & 0x80) >> 7);
                            }
                        }
        */
 ByteArrayOutputStream bos = new ByteArrayOutputStream();
               bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
               byte[] bitmapdata = bos.toByteArray();

              // Log.d("pixel size",pixels.clone().toString());
             //   Log.d("real size",Integer.toString(pixels.length));
                request.write(bitmapdata);
                                             request.writeBytes(this.crlf);
                        request.writeBytes(this.twoHyphens + this.boundary + this.twoHyphens + this.crlf);
                        request.flush();
                        Log.d("imageout",Integer.toString(urlConnectionImage.getResponseCode()));
                        request.close();
                                            switch (urlConnection.getResponseCode()) {
                            case HttpURLConnection.HTTP_OK:

                               return true;

                            default:

                                return false; // abort

                        }

                        // urlConnection.connect();

                    }
                    catch (Exception e)
                    {
                        Log.d("exeJsonPublish",e.toString());
                        return false;
                    }

                }

我已经更正了我的代码。现在它正在工作。在android doInBackground方法中未正确创建位图

使用uploadFile方法,如下所示:

public Response uploadFile(
        @DefaultValue("true") @FormDataParam("enabled") boolean enabled,
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail)
{
//Your code
}
这里的逻辑是将FormDataParam设置为true

这是我的一个关于服务器和客户端代码的答案,可能会对您有所帮助


您可以使用FormDataMultiPart

@POST
@Path("/coverpic")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(FormDataMultiPart multiPart){
     FormDataBodyPart body = multiPart.getField(<your fieldname>);
     InputStream ins = ((BodyPartEntity) body.getEntity()).getInputStream();
     ///.....
     ///.....
     return Response.status(200).entity(output).build();

}
您必须使用多部分依赖项

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.18.3</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

com.sun.jersey.contribs
泽西多部分
1.18.3
文件上传
文件上传
1.3.1

在Eclipse中检查动态web模块版本是2.5还是3.0。我认为multipart只能在3.0或更高版本中工作。请告诉我版本是什么。

这没有帮助。谢谢你。我已经编辑了我的web服务方法。查看是否可以提供帮助。请查看我编辑的web服务方法。BuffuredImage正在提供空值。它正在工作。位图字节数组未正确创建。我现在已经改正了。您的代码也有帮助。您能帮助我理解,如何在与此API对应的CURL请求中传递
multiPart
的值吗?A我今天问了。它是3.0。我查过了。
multiPart.getFields().keySet().iterator()
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.18.3</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>