java - XLSX 文件损坏:JDBC 文件上传和下载
问题描述
上传和下载正常,但我收到错误“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。”
文件也成功打开。我猜文件上传有问题。甲骨文数据库
这是上传部分
mapSqlParameterSource.addValue("inputFile", new SqlLobValue(uploadfiles, (int) fileMetaData.getSize()),
OracleTypes.BLOB);
这里是下载部分
public Response 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 Response.ok(responseMsg).build();
}
//Set File Name
String fileName = rs.get(0).getFileName();
String path = System.getProperty("user.dir") + "\\src\\main\\resources\\excel\\"+fileName;
FileOutputStream output = new FileOutputStream(path);
//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];
while (input.read(buffer) > 0) {
output.write(buffer);
}
output.close();
File theFile = new File(path);
logger.info("Saved to file " + theFile.getAbsolutePath());
String contentType = null;
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
contentType = mimeTypesMap.getContentType(theFile);
ResponseBuilder response = Response.ok((Object) theFile);
response.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
return response.build();
} catch (SQLException e) {
logger.error(e);
e.printStackTrace();
}
ResponseMsg responseMsg = new ResponseMsg("","No file found!!");
return Response.ok(responseMsg).build();
}
解决方案
推荐阅读
- php - 如何从 ftp 服务器获取文件并复制到自己的服务器上
- symfony - 控制器是其他文件夹和模板路径时的配置不充分
- android - 关闭应用程序进程后如何保存我的搜索栏位置?
- javascript - 如何在 sql 请求中使用 promise
- python - Dataframe 从内存大小差异很大的文件夹中读取图像
- javascript - 无法下载 png 格式的二维码
- javascript - 无法从异步方法返回值
- eclipse - Scala 编译器无法为项目设置应用安装
- java - 登录 DTO 在 Spring Controller 中不起作用
- php - 如何根据日期时间搜索计算特殊日和正常日的总金额