|
|
@@ -1,5 +1,6 @@
|
|
|
package com.ruoyi.web.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import com.ruoyi.common.core.content.VistorContextHolder;
|
|
|
@@ -10,6 +11,7 @@ import com.ruoyi.common.exception.CustomException;
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
import com.ruoyi.common.utils.SecurityUtils;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
+import com.ruoyi.common.utils.http.HttpUtils;
|
|
|
import com.ruoyi.dz.domain.DzCards;
|
|
|
import com.ruoyi.dz.service.IDzCardsService;
|
|
|
import com.ruoyi.enums.CardTimeStatus;
|
|
|
@@ -28,6 +30,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.ZoneId;
|
|
|
import java.util.Date;
|
|
|
@@ -198,8 +201,63 @@ public class CommService {
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ public void downloadRemote(HttpServletResponse response, Long paperId,String url) {
|
|
|
+ try {
|
|
|
+ // 获取试卷信息
|
|
|
+ LearnPaper paper = paperService.selectLearnPaperById(paperId);
|
|
|
+ if (paper == null) {
|
|
|
+ throw new RuntimeException("试卷不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取题目列表
|
|
|
+ List<LearnQuestions> questions = learnQuestionsService.selectQuestionByPaperId(paperId);
|
|
|
+
|
|
|
+ // 将题目列表转换为 JSON 字符串
|
|
|
+ String quesJson = JSONArray.toJSONString(questions);
|
|
|
+
|
|
|
+ // 远程服务器地址
|
|
|
+ String remoteUrl = (StringUtils.isEmpty(url)?"https://www.dz1kt.com/prod-api":url)+"/learn/paper/downloadPaper";
|
|
|
+
|
|
|
+ // 构建请求参数
|
|
|
+ String params = "paperName=" + java.net.URLEncoder.encode(paper.getPaperName(), StandardCharsets.UTF_8.toString())
|
|
|
+ + "&ques=" + java.net.URLEncoder.encode(quesJson, StandardCharsets.UTF_8.toString());
|
|
|
+
|
|
|
+ // 设置响应头
|
|
|
+ response.setHeader("Pragma", "No-cache");
|
|
|
+ response.setHeader("Cache-Control", "No-cache");
|
|
|
+ response.setHeader("Expires", "0");
|
|
|
+ response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"",
|
|
|
+ new String((paper.getPaperName() + ".docx").getBytes("utf-8"), "ISO-8859-1")));
|
|
|
+ response.setHeader("Content-Type", "application/octet-stream");
|
|
|
+
|
|
|
+ // 调用远程服务器并将响应流写入到本地响应
|
|
|
+ HttpUtils.sendGetStream(remoteUrl, params, response.getOutputStream());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("下载失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 通用:下载试卷(外部传入数据),不依赖本地数据
|
|
|
+ * @param response
|
|
|
+ * @param paperName
|
|
|
+ * @param questions
|
|
|
+ */
|
|
|
+ public void download(HttpServletResponse response, String paperName,List<LearnQuestions> questions){
|
|
|
+ try {
|
|
|
+ byte[] data = DownloadPaperUtils.download(paperName, questions);
|
|
|
|
|
|
+ response.setHeader("Pragma", "No-cache");
|
|
|
+ response.setHeader("Cache-Control", "No-cache");
|
|
|
+ response.setHeader("Expires", "0");
|
|
|
+ response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", new String((paperName + ".docx").getBytes("utf-8"), "ISO-8859-1")));
|
|
|
+ response.setHeader("Content-Type", "application/octet-stream");
|
|
|
+ IOUtils.write(data, response.getOutputStream());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage(), e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|