首页 > 解决方案 > 发送到后端时发生 I/O 错误。[文件上传]

问题描述

我正在尝试将 500mb 或 1Gb 的文件上传到我的 postgres 数据库中。并得到以下错误: -


{
    "timestamp": "2021-08-30T09:53:10.414+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "org.springframework.dao.DataAccessResourceFailureException: PreparedStatementCallback; An I/O error occurred while sending to the backend.; nested exception is org.postgresql.util.PSQLException: An I/O error occurred while sending to the backend.",
    "path": "/api/upload/0/blob/0001"
}

这是我的代码:-

public void upload(HttpServletRequest request) throws Exception {
        try (InputStream is = getInputStream(request)) {
            if (is != null) {
                service.save("0001",is); //directly storing in database
            }
        } catch (Exception e) {
            throw new Exception(e.toString());
        }

    }

    private InputStream getInputStream(final HttpServletRequest request) throws IOException, FileUploadException {
        final ServletFileUpload upload = new ServletFileUpload();
        final FileItemIterator iterator = upload.getItemIterator(request);
        InputStream is = null;
        while (iterator.hasNext()) {
            final FileItemStream item = iterator.next();
            if (!item.isFormField()) {
                is = item.openStream();
            }
            break;
        }
        return is;
    }

我正在使用 springJdbcTemplate

jdbcTemplate.update(connection -> {
PreparedStatement stmt = connection.prepareStatement(insertSql);
            stmt.setString(1, id);
            stmt.setBinaryStream(2, is);
            return stmt;
        }, keyHolder);

这段代码完美地适用于 100MB 的文件。不止于此,它会抛出一个错误..

标签: javapostgresqlspring-bootfile

解决方案


推荐阅读