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

前端 未结 6 2108
难免孤独
难免孤独 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:41

    @Consumes("multipart/form-data")
    @Produces(MediaType.TEXT_PLAIN + ";charset=utf-8")
    public String upload(MultipartFormDataInput input,  @QueryParam("videoId") String  videoId,
            @Context HttpServletRequest a_request) {
    
        String fileName = "";
        for (InputPart inputPart : input.getParts()) {
            try {
    
                MultivaluedMap header = inputPart.getHeaders();
                fileName = getFileName(header);
                // convert the uploaded file to inputstream
                InputStream inputStream = inputPart.getBody(InputStream.class, null);               
                // write the inputStream to a FileOutputStream
                OutputStream outputStream = new FileOutputStream(new File("/home/mh/Téléchargements/videoUpload.avi"));
                int read = 0;
                byte[] bytes = new byte[1024];
                while ((read = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }
                System.out.println("Done!");
            } catch (IOException e) {
                e.printStackTrace();
                return "ko";
            }
    
        }
    

提交回复
热议问题