java - 发送到后端时发生 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 的文件。不止于此,它会抛出一个错误..
解决方案
推荐阅读
- mysql - mysql中时间戳的默认值
- javascript - TypeScript 错误仅出现在第一次 Webpack 编译时
- deep-learning - 加载手动注释数据以训练 RNN POS 标注器
- reactjs - 如何在 react-i18next 运行时加载翻译文件
- activemq - 我可以使用 ActiveMQ 在不在同一本地网络上的两台机器之间进行通信吗?
- python - 尝试编写 Python/pandas 函数来更新特定列中等于 0 的行?
- css - 如何准确计算弹性项目的假设主要尺寸 - CSS Spec问题
- java - 嵌套继承类的 javax 验证
- python - 将列表组合成字典列表
- python - neo4j 驱动函数等价于 py2neo 函数