|
|
|
@ -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 PPT文件下载URL
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|