用于Java类的Java Jax-rs Web服务cA消息正文编写器....以及未找到MIME媒体类型application / octet-stream

时间:2015-05-11 12:48:58

标签: java web-services rest jax-rs

我的web服务面临一些问题,我已经附上了我的Jax-rs文件上传代码,我收到此错误  " Java类java.lang.Byte和Java类型类java.lang.Byte的消息正文编写器,以及找不到MIME媒体类型application / octet-stream" ..

@Context
private HttpServletRequest request;

@POST
@Path("/profilePicture")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadUserProfilePicture(
        @FormDataParam("file") InputStream uploadedInputStream) {
    String uploadedFileLocation = "......";
    OutputStream out;
    try {
        System.out.println(uploadedInputStream);
        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();
        uploadStatus = -1;
    } 
}

我收到此错误,所以看看我的代码和异常并建议是否有任何问题......

SEVERE: A message body writer for Java class java.lang.Byte, and Java type class java.lang.Byte, and MIME media type application/octet-stream was not found
May 11, 2015 5:26:34 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type are:
application/octet-stream ->
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General

May 11, 2015 5:26:34 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.lang.Byte, and Java type class java.lang.Byte, and MIME media type application/octet-stream was not found

请帮助我......这是我正在使用的客户端代码

public void uploadImage() {

        try {
            String url = baseUrl + "...";
            String fileName = "E:/new.jpeg";

            File uploadFile = new File(fileName);
            WebResource resource = Client.create().resource(url);
            FormDataMultiPart form = new FormDataMultiPart();
            form.field("fileName", uploadFile.getName());
            FormDataBodyPart fdp = new FormDataBodyPart("file",
                    new FileInputStream(uploadFile),
                    MediaType.APPLICATION_OCTET_STREAM_TYPE);
            form.bodyPart(fdp);
            String response = resource.type(MediaType.MULTIPART_FORM_DATA).post(String.class, form);

            System.out.println(response);
        } catch (Exception es) {
            es.printStackTrace();
        } finally {
        }
    }

0 个答案:

没有答案