Skip to content

FileUpload OutOfDirectMemoryError #12

@markbanyang

Description

@markbanyang

当我在上传大文件的时候会发生OOM
下面是文件上传代码:
`

@RequestMapping(value = "/stream-upload-test")

  public ResponseEntity<String> upload(HttpServletRequest request, HttpServletResponse response)
        throws IOException, FileUploadException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        ServletFileUpload upload = new ServletFileUpload();

        Map<String, String> params = new HashMap<>();
        InputStream is = null;
        FileItemIterator iter = upload.getItemIterator(request);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            if (!item.isFormField()) {
                is = item.openStream();
                try {
                    int i = 0;
                    byte bb [] = new byte[4096];
                    while((i = is.read(bb)) != -1){
                        System.out.println(Arrays.toString(bb));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (null != is) {
                        is.close();
                    }
                }
            } else {

                String fieldName = item.getFieldName();
                String value = Streams.asString(item.openStream());
                System.out.println(fieldName + " : " + value);
            }
        }
    }
    return new ResponseEntity<String>(HttpStatus.OK);
}

`

错误信息截图如下:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions