main
HuangHai 1 month ago
parent 8a6220cac4
commit bfb527d47b

@ -53,9 +53,9 @@ public class AiGenerate {
* Word
*/
private static void generateWordReport(String analysisPrompt, String[] regions) {
// 生成输出文件路径
// 生成输出文件名(不包含路径
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String outputPath = "教育资源对比分析报告_" + timestamp + ".docx";
String fileName = "教育资源对比分析报告_" + timestamp + ".docx";
System.out.println("开始调用DeepSeek进行数据分析...");
@ -76,14 +76,18 @@ public class AiGenerate {
try {
// 生成Word文档
WordGenerator.generateWordDocument(fullResponse.toString(), outputPath, regions);
System.out.println("Word分析报告已保存到: " + outputPath);
String wordFilePath = WordGenerator.generateWordDocument(fullResponse.toString(), fileName, regions);
System.out.println("Word分析报告已保存到: " + wordFilePath);
// 第三步生成PPT
System.out.println("\n\n=== 开始生成PPT ===");
String uid = "dsideal";
String apiToken = PptAIKit.createApiToken(uid, null);
PptGenerator.generatePptPresentation(fullResponse.toString(), regions, apiToken);
String pptFilePath = PptGenerator.generatePptPresentation(fullResponse.toString(), regions, apiToken);
if (pptFilePath != null) {
System.out.println("PPT演示文稿已保存到: " + pptFilePath);
}
} catch (Exception e) {
System.err.println("保存文件时出错: " + e.getMessage());

@ -4,9 +4,12 @@ import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* PPT - PPT稿
@ -14,15 +17,24 @@ import java.nio.charset.StandardCharsets;
public class PptGenerator {
private static final String PPT_API_URL = "https://open.docmee.cn/api/ppt/generatePptx";
// 指定保存目录
private static final String SAVE_DIRECTORY = "D:\\dsWork\\YunNanDsBase\\WebRoot\\upload";
/**
* PPT稿
* @param analysisContent Word
* @param regions
* @param token API访
* @return PPT
*/
public static void generatePptPresentation(String analysisContent, String[] regions, String token) {
public static String generatePptPresentation(String analysisContent, String[] regions, String token) {
try {
// 确保保存目录存在
File saveDir = new File(SAVE_DIRECTORY);
if (!saveDir.exists()) {
saveDir.mkdirs();
}
// 将分析内容转换为Markdown格式
String markdownContent = convertToMarkdown(analysisContent, regions);
@ -32,13 +44,74 @@ public class PptGenerator {
if (pptFileUrl != null && !pptFileUrl.trim().isEmpty()) {
System.out.println("PPT生成成功");
System.out.println("PPT文件下载地址: " + pptFileUrl);
// 下载PPT文件到本地
String localFilePath = downloadPptFile(pptFileUrl, regions);
if (localFilePath != null) {
System.out.println("PPT文件已下载到: " + localFilePath);
return localFilePath;
} else {
System.err.println("PPT文件下载失败");
return null;
}
} else {
System.err.println("PPT生成失败");
return null;
}
} catch (Exception e) {
System.err.println("生成PPT时出错: " + e.getMessage());
e.printStackTrace();
return null;
}
}
/**
* PPT
* @param fileUrl PPTURL
* @param regions
* @return
*/
private static String downloadPptFile(String fileUrl, String[] regions) {
try {
// 生成本地文件名
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fileName = generateReportTitle(regions).replace("教育资源配置对比分析报告", "教育资源对比分析报告") + "_" + timestamp + ".pptx";
String localFilePath = SAVE_DIRECTORY + File.separator + fileName;
// 下载文件
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(30000); // 30秒连接超时
connection.setReadTimeout(60000); // 60秒读取超时
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(localFilePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
long totalBytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
System.out.println("文件下载完成,大小: " + totalBytesRead + " 字节");
return localFilePath;
}
} else {
System.err.println("下载失败HTTP状态码: " + responseCode);
return null;
}
} catch (Exception e) {
System.err.println("下载PPT文件时出错: " + e.getMessage());
e.printStackTrace();
return null;
}
}

@ -5,6 +5,7 @@ import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -14,14 +15,27 @@ import java.util.Date;
*/
public class WordGenerator {
// 指定保存目录
private static final String SAVE_DIRECTORY = "D:\\dsWork\\YunNanDsBase\\WebRoot\\upload";
/**
* Word
* @param analysisResult AI
* @param outputPath
* @param fileName
* @param regions
* @return
* @throws Exception
*/
public static void generateWordDocument(String analysisResult, String outputPath, String[] regions) throws Exception {
public static String generateWordDocument(String analysisResult, String fileName, String[] regions) throws Exception {
// 确保保存目录存在
File saveDir = new File(SAVE_DIRECTORY);
if (!saveDir.exists()) {
saveDir.mkdirs();
}
// 构建完整的输出路径
String outputPath = SAVE_DIRECTORY + File.separator + fileName;
XWPFDocument document = new XWPFDocument();
try {
@ -45,6 +59,9 @@ public class WordGenerator {
document.write(out);
}
System.out.println("Word文档已保存到: " + outputPath);
return outputPath;
} finally {
document.close();
}

Loading…
Cancel
Save