【问题标题】:415 Error while uploading png image on restful webservice在 RESTful Web 服务上上传 png 图像时出现 415 错误
【发布时间】:2014-05-06 14:50:54
【问题描述】:

我正在尝试上传 png 图像(然后从另一个客户端下载)。 我收到 Http 状态代码 415:我的错误是什么?

我已经在这里和谷歌上阅读了所有内容,但仍未解决。

这是我的服务器代码:

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

    @POST
    @Path("/upload")
    @Consumes("image/png")
    public Response uploadPng(File file) throws IOException {

        String uploadedFileLocation = 
                "C:/Users/Desktop/server/" + file.getName();
        DataInputStream diStream =
                new DataInputStream(new FileInputStream(file));
        long len = (int) file.length();
        byte[] fileBytes = new byte[(int) len];
        int read = 0;
        int numRead = 0;
        while (read < fileBytes.length && (numRead =
                diStream.read(fileBytes, read,
                        fileBytes.length - read)) >= 0) {
            read = read + numRead;
        }

        // save it
        writeToFile(diStream, uploadedFileLocation);
        System.out.println("File uploaded to : " + uploadedFileLocation);
        return Response.status(200).entity(file).build();
    }

    // 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();
        }

    }

这是客户端代码:

public class FileUploadClient {

    public static void main(String[] args) {
        try {
            File fileToUpload = new File("C:/Users/client/file.png");

            ClientConfig config = new DefaultClientConfig();
            Client client = Client.create(config);

            WebResource resource =
                    client.resource("http://localhost:8080/WS/rest/file/upload");

            ClientResponse response =
                    resource.accept("image/png").post(ClientResponse.class, fileToUpload);

            System.out.println(response.getStatus());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

编辑 它的工作原理是这样的:

public class FileUploadClient{

    public static void main(String[] args){
        try{
        File fileToUpload = new File("C:/Users/client/file.png");
        InputStream is =new FileInputStream(fileToUpload);

        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);

       WebResource resource = client.resource("http://localhost:8080/WS/rest/");

       @SuppressWarnings("resource")
   FormDataMultiPart part = new FormDataMultiPart().
          field("file", is, MediaType.APPLICATION_OCTET_STREAM_TYPE);
       String response = resource.path("file").path("upload").type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part);
       System.out.println(response);

        }
       catch(Exception e){
        e.printStackTrace();
    }
 }
}   

这是 POST。

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

    String uploadedFileLocation = "C:/Users/Desktop/server/file.png";

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);
    String output = "File uploaded to : " + uploadedFileLocation;

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

}

【问题讨论】:

  • 尝试使用不同的内容类型。还有一张不同的图片。
  • 使用不同的 png 图像或 ("image/jpeg") 和 .jpg 图像没有任何变化。
  • @Schiawo 你如何执行你的代码来上传文件?
  • 对不起,我不明白你的问题。我在 Eclipse 上做了一个像 youtube.com/watch?v=qmEVsUJh2d8 这样的动态 Web 项目,然后我运行你在 EDIT 之后看到的 main 方法。

标签: java eclipse web-services rest jersey


【解决方案1】:

我假设您使用 CXF 作为 JAX-RS 实现,那么在我看来,图像应该以内容类型作为多部分表单数据上传。

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response uploadImage(List<Attachment> attachments); 

这里的附件是一个“org.apache.cxf.jaxrs.ext.multipart.Attachment”对象。

数据可以如下读取

    for (final Attachment attachment : attachments) {
        String fileName = attachment.getDataHandler().getName();
        final InputStream inputStream = attachment.getDataHandler().getInputStream();
        ByteStreams.copy(inputStream, outputStream);
        inputStream.close();
        final byte[] image = outputStream.toByteArray();
    }

【讨论】:

  • 不客气 :) 。如果您觉得它有帮助,请您支持我的回答并标记它是正确的。这将帮助我在 stackoverflow 上获得声誉。
猜你喜欢
  • 2019-06-10
  • 2015-09-14
  • 2018-05-03
  • 2020-11-25
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多