How to avoid OutOfMemoryError when uploading a large file using Jersey client

前端 未结 6 2103
难免孤独
难免孤独 2020-11-28 08:14

I am using Jersey client for http-based request. It works well if the file is small but run into error when I post a file with size of 700M:

Exception in thr         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 08:55

    You could use streams.Try something like this on the client:

    InputStream fileInStream = new FileInputStream(fileName);
    String sContentDisposition = "attachment; filename=\"" + fileName.getName()+"\"";
    WebResource fileResource = a_client.resource(a_sUrl);       
    ClientResponse response = fileResource.type(MediaType.APPLICATION_OCTET_STREAM)
                            .header("Content-Disposition", sContentDisposition)
                            .post(ClientResponse.class, fileInStream);      
    

    with resource like this on the server:

    @PUT
    @Consumes("application/octet-stream")
    public Response putFile(@Context HttpServletRequest a_request,
                             @PathParam("fileId") long a_fileId,
                             InputStream a_fileInputStream) throws Throwable
    {
        // Do something with a_fileInputStream
        // etc
    

提交回复
热议问题