秦芳睿
2025-03-13 d9111b072c5bbf196435be9aa7f1f849cd07252b
JAVA/SMTAIServer/src/main/java/com/smtaiserver/smtaiserver/control/SMTJavaAIControl.java
@@ -3,13 +3,8 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -97,10 +92,14 @@
               {
                  for(MultipartFile file : files)
                  {
                     String fileId = SMTStatic.newUUID();
                     String fileText="";
                     String fileSuffix = (String) SMTAIServerApp.getApp().getGlobalConfig("file_suffix", false);
                     String fileName = new File(file.getOriginalFilename()).getName();
                     byte[] fileData = readAttachFile(file);
                     String fileText = new String(fileData, "UTF-8");
                     if (fileSuffix.contains(fileName.substring(fileName.lastIndexOf(".") + 1))) {
                        fileText = SMTAIServerApp.fileTranslTxt(file);
                     }
                     String fileId = SMTStatic.newUUID();
                     db.executeSQL(
                          " INSERT INTO chat_history_attach(attach_id, attach_type, attach_title, attach_bytes)"
                        + " VALUES(?,?,?,?)", new Object[] {
@@ -1341,8 +1340,7 @@
      return tranReq.returnJson(jsonWr);
    }
   public void downloadFileByFileId(SMTAIServerRequest tranReq, HttpServletResponse response) throws Exception {
   public ModelAndView downloadFileByFileId(SMTAIServerRequest tranReq, HttpServletResponse response) throws Exception {
      String fileId = tranReq.convParamToString("file_id", true);  // 获取请求中的file_id
      SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
@@ -1352,47 +1350,28 @@
         if (dbRecords.getRecords().isEmpty()) {
            throw new Exception("文件不存在");
         }
         // 获取文件信息
         DBRecord fileRecord = dbRecords.getRecords().get(0);
         String attachTitle = fileRecord.getString("attach_title");  // 获取文件标题
         String attachBytes = fileRecord.getString("attach_bytes");
         byte[] bytes = attachBytes.getBytes();
         // 设置响应头和文件名
         String fileName = attachTitle != null ? attachTitle : "downloaded_file";
         response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
         response.setContentType("application/octet-stream");
            response.getOutputStream().write(bytes);
            response.getOutputStream().flush();
         String extension = attachTitle.substring(attachTitle.lastIndexOf(".") + 1);
         // 扩展名和 MIME 类型映射
         Object fileTypeMapping = SMTAIServerApp.getApp().getGlobalConfig("file_type_mapping", false);
         Json json = Json.read(fileTypeMapping.toString());
         Map<String, Object> mimeTypes = json.asMap();
         String mimeType = (String) mimeTypes.getOrDefault(extension.toLowerCase(), "application/octet-stream");
         // 获取文件的字节数据,假设返回的是字节数组
         byte[] bytes = (byte[]) fileRecord.getValue("attach_bytes");  // 获取字节数据
         return tranReq.returnDownloadByteArray(bytes, attachTitle, mimeType);
//         // 设置响应头和文件名
//         String fileName = attachTitle != null ? attachTitle : "downloaded_file";
//         response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
//         response.setContentType("application/octet-stream");
//         response.getOutputStream().write(bytes);  // 写入字节数据
//         response.getOutputStream().flush();
      } catch (Exception e) {
         throw new Exception("Error processing file download: " + e.getMessage());
      }
   }
   public void downloadFileByFileId(SMTAIServerRequest tranReq, HttpServletResponse response) throws Exception {
      String fileId = tranReq.convParamToString("file_id", true);  // 获取请求中的file_id
      SMTDatabase db = SMTAIServerApp.getApp().allocDatabase();
      try {
         // 查询指定file_id的文件记录
         DBRecords dbRecords = db.querySQL("SELECT * FROM ai_times.chat_history_attach WHERE attach_id=?", new Object[]{fileId});
         if (dbRecords.getRecords().isEmpty()) {
            throw new Exception("文件不存在");
         }
         // 获取文件信息
         DBRecord fileRecord = dbRecords.getRecords().get(0);
         String attachTitle = fileRecord.getString("attach_title");  // 获取文件标题
         String attachBytes = fileRecord.getString("attach_bytes");
         byte[] bytes = attachBytes.getBytes();
         // 设置响应头和文件名
         String fileName = attachTitle != null ? attachTitle : "downloaded_file";
         response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
         response.setContentType("application/octet-stream");
            response.getOutputStream().write(bytes);
            response.getOutputStream().flush();
      } catch (Exception e) {
         throw new Exception("Error processing file download: " + e.getMessage());
      }
   }
}