完善代码
This commit is contained in:
104
src/main/java/com/seeyon/utils/file/FileHandlerService.java
Normal file
104
src/main/java/com/seeyon/utils/file/FileHandlerService.java
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
package com.seeyon.utils.file;
|
||||||
|
|
||||||
|
import com.seeyon.ctp.common.AppContext;
|
||||||
|
import com.seeyon.ctp.common.filemanager.manager.AttachmentManager;
|
||||||
|
import com.seeyon.ctp.common.filemanager.manager.FileManager;
|
||||||
|
import com.seeyon.ctp.common.po.filemanager.Attachment;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
public class FileHandlerService {
|
||||||
|
|
||||||
|
private AttachmentManager attachmentManager = (AttachmentManager) AppContext.getBean("attachmentManager");
|
||||||
|
private FileManager fileManager = (FileManager) AppContext.getBean("fileManager");
|
||||||
|
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir") + File.separator + "seeyontempfile";
|
||||||
|
|
||||||
|
public void handleFile(String refId, HttpServletResponse response) throws Exception {
|
||||||
|
InputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
Attachment attachment = attachmentManager.getAttachmentByFileURL(Long.valueOf(refId));
|
||||||
|
inputStream = fileManager.getFileInputStream(attachment.getFileUrl());
|
||||||
|
|
||||||
|
// ⭐ 关键:重置
|
||||||
|
response.reset();
|
||||||
|
|
||||||
|
// ⭐ 必须设置类型
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
|
||||||
|
// ⭐ 文件名处理
|
||||||
|
String encodedFileName = URLEncoder.encode(attachment.getFilename(), "UTF-8")
|
||||||
|
.replaceAll("\\+", "%20");
|
||||||
|
|
||||||
|
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
||||||
|
response.setHeader(
|
||||||
|
"Content-Disposition",
|
||||||
|
"attachment; filename=\"" + attachment.getFilename() + "\"; filename*=UTF-8''" + encodedFileName
|
||||||
|
);
|
||||||
|
|
||||||
|
// ⭐ 输出流
|
||||||
|
OutputStream outputStream = response.getOutputStream();
|
||||||
|
|
||||||
|
byte[] buffer = new byte[2048];
|
||||||
|
int len;
|
||||||
|
|
||||||
|
while ((len = inputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
outputStream.flush();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace(); // ❗不要吞异常
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (inputStream != null) inputStream.close();
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// public ResponseEntity<Resource> downloadFileStream(String refId) {
|
||||||
|
// InputStream inputStream = null;
|
||||||
|
// OutputStream ops = null;
|
||||||
|
// try {
|
||||||
|
// Attachment attachment = attachmentManager.getAttachmentByFileURL(Long.valueOf(refId));
|
||||||
|
// inputStream = fileManager.getFileInputStream(attachment.getFileUrl());
|
||||||
|
// //下载到本地临时文件夹
|
||||||
|
// // 创建输出流,将文件保存到本地
|
||||||
|
// ops = new FileOutputStream(TEMP_DIR);
|
||||||
|
// // 设置缓冲区
|
||||||
|
// byte[] buffer = new byte[4096];
|
||||||
|
// int bytesRead;
|
||||||
|
// while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
|
// ops.write(buffer, 0, bytesRead);
|
||||||
|
// }
|
||||||
|
// ops.flush();
|
||||||
|
// ops.close();
|
||||||
|
// File file = new File(TEMP_DIR);
|
||||||
|
// if (!file.exists()) {
|
||||||
|
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
|
||||||
|
// }
|
||||||
|
// // 封装文件为资源对象
|
||||||
|
// Resource resource = new FileSystemResource(file);
|
||||||
|
// String fileName = attachment.getFilename();
|
||||||
|
// // 设置响应头
|
||||||
|
// String encodeName = URLEncoder.encode(fileName, "UTF-8");
|
||||||
|
// String header = "attachment;filename=" + encodeName;
|
||||||
|
// return ResponseEntity.ok()
|
||||||
|
// .header(HttpHeaders.CONTENT_DISPOSITION, header)
|
||||||
|
// .header(HttpHeaders.CONTENT_TYPE, "application/octet-stream")
|
||||||
|
// .body(resource);
|
||||||
|
//
|
||||||
|
// }catch (Exception e) {
|
||||||
|
// e.printStackTrace(); // ❗不要吞异常
|
||||||
|
// } finally {
|
||||||
|
// try {
|
||||||
|
// if (inputStream != null) inputStream.close();
|
||||||
|
// } catch (Exception ignored) {}
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
}
|
||||||
@@ -95,6 +95,7 @@ public class FormTableExecutor {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||||
|
log.info("执行sql为: " + sqlMap.get("sql"));
|
||||||
List<Map<String, Object>> rows = agent.resultSetToList();
|
List<Map<String, Object>> rows = agent.resultSetToList();
|
||||||
|
|
||||||
return rows.stream()
|
return rows.stream()
|
||||||
@@ -125,6 +126,7 @@ public class FormTableExecutor {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||||
|
log.info("执行sql为: " + sqlMap.get("sql"));
|
||||||
Map<String, Object> row = agent.resultSetToMap();
|
Map<String, Object> row = agent.resultSetToMap();
|
||||||
if (row == null) return null;
|
if (row == null) return null;
|
||||||
|
|
||||||
@@ -161,6 +163,7 @@ public class FormTableExecutor {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||||
|
log.info("执行sql为: " + sqlMap.get("sql"));
|
||||||
List<Map<String, Object>> rows = agent.resultSetToList();
|
List<Map<String, Object>> rows = agent.resultSetToList();
|
||||||
|
|
||||||
return rows.stream()
|
return rows.stream()
|
||||||
@@ -190,6 +193,7 @@ public class FormTableExecutor {
|
|||||||
JDBCAgent agent = new JDBCAgent();
|
JDBCAgent agent = new JDBCAgent();
|
||||||
try {
|
try {
|
||||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||||
|
log.info("执行sql为: " + sqlMap.get("sql"));
|
||||||
String dbName = (String) AppContext.getCache("DATABASE_NAME");
|
String dbName = (String) AppContext.getCache("DATABASE_NAME");
|
||||||
if ("mysql".equalsIgnoreCase(dbName)) {
|
if ("mysql".equalsIgnoreCase(dbName)) {
|
||||||
return (Long) agent.resultSetToMap().get("countnum");
|
return (Long) agent.resultSetToMap().get("countnum");
|
||||||
@@ -221,6 +225,7 @@ public class FormTableExecutor {
|
|||||||
Map<String, Object> sqlMap = generateSql(param);
|
Map<String, Object> sqlMap = generateSql(param);
|
||||||
JDBCAgent agent = new JDBCAgent();
|
JDBCAgent agent = new JDBCAgent();
|
||||||
try {
|
try {
|
||||||
|
log.info("执行sql为: " + sqlMap.get("sql"));
|
||||||
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||||
@@ -243,6 +248,7 @@ public class FormTableExecutor {
|
|||||||
Map<String, Object> sqlMap = generateSql(param);
|
Map<String, Object> sqlMap = generateSql(param);
|
||||||
JDBCAgent agent = new JDBCAgent();
|
JDBCAgent agent = new JDBCAgent();
|
||||||
try {
|
try {
|
||||||
|
log.info("执行sql为: " + sqlMap.get("sql"));
|
||||||
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||||
@@ -266,6 +272,7 @@ public class FormTableExecutor {
|
|||||||
Map<String, Object> sqlMap = generateSql(param);
|
Map<String, Object> sqlMap = generateSql(param);
|
||||||
JDBCAgent agent = new JDBCAgent();
|
JDBCAgent agent = new JDBCAgent();
|
||||||
try {
|
try {
|
||||||
|
log.info("执行sql为: " + sqlMap.get("sql"));
|
||||||
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||||
@@ -358,7 +365,7 @@ public class FormTableExecutor {
|
|||||||
List<FormWhereCondition> conditions,
|
List<FormWhereCondition> conditions,
|
||||||
Integer limit,
|
Integer limit,
|
||||||
Integer offset,
|
Integer offset,
|
||||||
String orderField) {
|
String orderField,String sortType) {
|
||||||
String dbName = (String) AppContext.getCache("DATABASE_NAME");
|
String dbName = (String) AppContext.getCache("DATABASE_NAME");
|
||||||
String orderBy = (orderField == null) ? "start_date" : orderField;
|
String orderBy = (orderField == null) ? "start_date" : orderField;
|
||||||
StringBuilder sql = new StringBuilder();
|
StringBuilder sql = new StringBuilder();
|
||||||
@@ -376,12 +383,15 @@ public class FormTableExecutor {
|
|||||||
}
|
}
|
||||||
selectFields.append(",ID");
|
selectFields.append(",ID");
|
||||||
}
|
}
|
||||||
|
if(sortType == null) {
|
||||||
|
sortType = "ASC";
|
||||||
|
}
|
||||||
// ===== 2. Oracle 分页特殊处理 =====
|
// ===== 2. Oracle 分页特殊处理 =====
|
||||||
if ("oracle".equalsIgnoreCase(dbName) && limit != null && offset != null) {
|
if ("oracle".equalsIgnoreCase(dbName) && limit != null && offset != null) {
|
||||||
sql.append("SELECT * FROM ( ");
|
sql.append("SELECT * FROM ( ");
|
||||||
sql.append(" SELECT t.*, ROW_NUMBER() OVER (ORDER BY ")
|
sql.append(" SELECT t.*, ROW_NUMBER() OVER (ORDER BY ")
|
||||||
.append(orderBy)
|
.append(orderBy)
|
||||||
.append(" DESC) rn ");
|
.append(" "+ sortType +") rn ");
|
||||||
sql.append(" FROM ")
|
sql.append(" FROM ")
|
||||||
.append(tableName)
|
.append(tableName)
|
||||||
.append(" t ");
|
.append(" t ");
|
||||||
@@ -395,7 +405,7 @@ public class FormTableExecutor {
|
|||||||
sql.append("SELECT ").append(selectFields);
|
sql.append("SELECT ").append(selectFields);
|
||||||
sql.append(" FROM ").append(tableName);
|
sql.append(" FROM ").append(tableName);
|
||||||
sql.append(buildWhereClause(conditions, params));
|
sql.append(buildWhereClause(conditions, params));
|
||||||
sql.append(" ORDER BY ").append(orderBy).append(" DESC");
|
sql.append(" ORDER BY ").append(orderBy).append(" " + sortType);
|
||||||
if (limit != null && offset != null) {
|
if (limit != null && offset != null) {
|
||||||
if ("mysql".equalsIgnoreCase(dbName)) {
|
if ("mysql".equalsIgnoreCase(dbName)) {
|
||||||
sql.append(" LIMIT ").append(offset).append(",").append(limit);
|
sql.append(" LIMIT ").append(offset).append(",").append(limit);
|
||||||
@@ -612,7 +622,7 @@ public class FormTableExecutor {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, Object> generateSql(SqlBuildParam param) {
|
public static Map<String, Object> generateSql(SqlBuildParam param) {
|
||||||
|
|
||||||
if (param == null || param.getSqlType() == null) {
|
if (param == null || param.getSqlType() == null) {
|
||||||
throw new IllegalArgumentException("SqlBuildParam or SqlType cannot be null");
|
throw new IllegalArgumentException("SqlBuildParam or SqlType cannot be null");
|
||||||
@@ -632,7 +642,8 @@ public class FormTableExecutor {
|
|||||||
param.getConditions(),
|
param.getConditions(),
|
||||||
param.getLimit(),
|
param.getLimit(),
|
||||||
param.getOffset(),
|
param.getOffset(),
|
||||||
param.getOrderField()
|
param.getOrderField(),
|
||||||
|
param.getSortType()
|
||||||
);
|
);
|
||||||
case UPDATE:
|
case UPDATE:
|
||||||
return buildUpdateSql(
|
return buildUpdateSql(
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public class SqlBuildParam {
|
|||||||
private Integer pageSize;
|
private Integer pageSize;
|
||||||
private String orderField;
|
private String orderField;
|
||||||
private String countField;
|
private String countField;
|
||||||
|
private String sortType;
|
||||||
|
|
||||||
public SqlType getSqlType() {
|
public SqlType getSqlType() {
|
||||||
return sqlType;
|
return sqlType;
|
||||||
@@ -111,4 +112,12 @@ public class SqlBuildParam {
|
|||||||
public void setCountField(String countField) {
|
public void setCountField(String countField) {
|
||||||
this.countField = countField;
|
this.countField = countField;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSortType() {
|
||||||
|
return sortType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSortType(String sortType) {
|
||||||
|
this.sortType = sortType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user