java - 从数据库下载文件:找不到类 java.io.ByteArrayInputStream 的序列化程序,也没有发现创建 BeanSerializer 的属性
问题描述
我正在尝试从数据库下载文件。我收到一个错误:
"没有为类 java.io.ByteArrayInputStream 找到序列化程序,也没有发现创建 BeanSerializer 的属性(为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链:org.springframework.http.ResponseEntity["body"]->org.springframework .core.io.ByteArrayResource["inputStream"]) "
我的代码如下所示:
public ResponseEntity<Resource> downloadFile(String reorgId) throws IOException {
List<WorkFlowInput> rs;
try {
//Fetching File from Database
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
mapSqlParameterSource.addValue("reorgId", reorgId);
rs = namedParameterJdbcTemplate.query(downloadFile, mapSqlParameterSource,
new BeanPropertyRowMapper<WorkFlowInput>(WorkFlowInput.class));
//If File is empty
if (rs == null || rs.isEmpty()) {
ResponseMsg responseMsg = new ResponseMsg("","No file found!!");
return ResponseEntity.notFound().build();
}
//Set File Name
String fileName = rs.get(0).getFileName();
File theFile = new File(fileName);
FileOutputStream output = new FileOutputStream(System.getProperty("user.dir") + "\\src\\main\\resources\\excel\\"+theFile);
//Blob File
Blob inputFile = rs.get(0).getInputFile();
logger.info("Input File: "+ inputFile);
InputStream input = inputFile.getBinaryStream();
logger.info("InputStream input: "+ input);
int fileLength = input.available();
logger.info("fileLength = " + fileLength);
byte[] buffer = new byte[1024];
ByteArrayResource resource = new ByteArrayResource(buffer);
String contentType = null;
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
contentType = mimeTypesMap.getContentType(theFile);
return ResponseEntity
.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource)
;
} catch (SQLException e) {
logger.error(e);
e.printStackTrace();
}
return ResponseEntity.notFound().build();
}
}
解决方案
推荐阅读
- javascript - 表单验证检查后不显示模态框
- data-structures - 树中根节点的级别是多少?
- airflow - 如何从环境变量创建气流变量
- react-native - 如何在 react-native 中更新 Tab Navigator 中徽章计数器的状态
- javascript - 如何将一个值返回给调用异步代码的函数,然后而不是下一个?
- azure-devops - Azure DevOps 计划和手动之间的不同行为
- ios - 如何修复 JSON 值'
' 类型的 NSNull 不能在 iOS react-native-config 和 AsyncStorage 中转换为 NSString? - custom-keyboard - MSKLC:如何告诉 Windows 我的自定义键盘布局的第一行
- java - 如何修复依赖错误项目构建错误:依赖项云:spring-cloud-starter-openfeign:jar丢失
- ubuntu - 我在安装 phpmyadmin 时遇到错误