javaspringspring-mvcheap-memoryapache-commons-fileupload

spring file upload, java heap size error


I have some strange error during file upload.
File size is ~300mb..
When i try to upload it, i catch "out of memory error: java heap space". I tried to increase heap size up to 3gb, but it's still not enough.
Configuration is:
Spring java config, spring jdbctemplate, mysql.
When i try to upload same file using other project (spring xml config, upload with executefunction, oracle), it takes 900mb from heap and uploads successfully.
Any ideas what is going on or any hints on how to find the problem?

  @RequestMapping(value = {"/add_file"}, method = {RequestMethod.POST}/*,
        params = {"file","path","parent_id"}*/)
public String addFile(
        @RequestParam("file") MultipartFile file,
        @RequestParam("parent_id") int parent_id,
        @RequestParam("path") String path,
        @RequestParam("id") int id
) {
    try {
        if (id == -1) fileDAO.addFile(file.getOriginalFilename(), file.getBytes(), parent_id);
        else fileDAO.updateFile(id, file.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "redirect:" + path;
}
/* dao function */
        @Override
    public void addFile(String name, byte[] blob, int folder_id) {
        if (folder_id == 0) {
            jdbcTemplate.update("insert into file(name, content) VALUES(?,?)",
                    new Object[]{name, new SqlLobValue(blob)},
                    new int[]{Types.VARCHAR, Types.BLOB});
        } 
}

update: commented addFile content. Now it takes ~500mb for the whole process (for uploading file into controller, i suppose)

update2: Changed db to oracle - it works. So the problem is in mysql properties..?


Solution

  • Use MultipartFile.getInputStream() and MultipartFile.getSize() and the matching SqlLobValue constructor.