|
@@ -1,7 +1,9 @@
|
|
|
package com.xzl.web.controller.userPortrait;
|
|
|
|
|
|
import com.xzl.common.utils.DateUtils;
|
|
|
+import com.xzl.web.service.UserPortraitService;
|
|
|
import org.aspectj.util.FileUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
@@ -10,13 +12,9 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.InputStreamReader;
|
|
|
+import java.io.*;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
@@ -31,100 +29,113 @@ import java.util.Map;
|
|
|
@RequestMapping("/user-portait")
|
|
|
public class ViewController {
|
|
|
|
|
|
- @Value("${pythonFilePath}")
|
|
|
- String pythonFilePath;
|
|
|
-
|
|
|
- @Value("${pythonPath}")
|
|
|
- String pythonPath;
|
|
|
-
|
|
|
- @GetMapping("/show")
|
|
|
- public void show(HttpServletResponse response) throws Exception {
|
|
|
- String wordcloudImageFileName = "wordcloud.png";
|
|
|
- File wordcloudImageFile = new File(pythonFilePath, wordcloudImageFileName);
|
|
|
- if (wordcloudImageFile.exists()) {
|
|
|
- InputStream inputStream = null;
|
|
|
- ServletOutputStream outputStream = null;
|
|
|
- try {
|
|
|
- inputStream = new FileInputStream(wordcloudImageFile);
|
|
|
- outputStream = response.getOutputStream();
|
|
|
- response.setHeader("Content-Disposition", String.format(
|
|
|
- "attachment; filename=\"%s\"",
|
|
|
- java.net.URLEncoder.encode(wordcloudImageFile.getName(), "UTF-8")));
|
|
|
- response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
- int count = 0;
|
|
|
- byte[] buffer = new byte[1024 * 1024];
|
|
|
- while ((count = inputStream.read(buffer)) != -1) {
|
|
|
- outputStream.write(buffer, 0, count);
|
|
|
+ @Autowired
|
|
|
+ private UserPortraitService userPortraitService;
|
|
|
+
|
|
|
+ @Value("${pythonFilePath}")
|
|
|
+ String pythonFilePath;
|
|
|
+
|
|
|
+ @Value("${pythonPath}")
|
|
|
+ String pythonPath;
|
|
|
+
|
|
|
+ @GetMapping("/show")
|
|
|
+ public void show(HttpServletResponse response) throws Exception {
|
|
|
+ String wordcloudImageFileName = "wordcloud.png";
|
|
|
+ File wordcloudImageFile = new File(pythonFilePath, wordcloudImageFileName);
|
|
|
+ if (wordcloudImageFile.exists()) {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ ServletOutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = new FileInputStream(wordcloudImageFile);
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ response.setHeader("Content-Disposition", String.format(
|
|
|
+ "attachment; filename=\"%s\"",
|
|
|
+ java.net.URLEncoder.encode(wordcloudImageFile.getName(), "UTF-8")));
|
|
|
+ response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
+ int count = 0;
|
|
|
+ byte[] buffer = new byte[1024 * 1024];
|
|
|
+ while ((count = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, count);
|
|
|
+ }
|
|
|
+ outputStream.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- outputStream.flush();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (inputStream != null) {
|
|
|
- inputStream.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/generate")
|
|
|
+ public Map generate() throws Exception {
|
|
|
+ Map rs = new HashMap();
|
|
|
+ // 先判断有没有 wordcloud.txt, 没有就生成一个,有就备份一个,然后重新生成新的内容
|
|
|
+ String wordcloudFileName = "wordcloud.txt";
|
|
|
+ File wordcloudFile = new File(pythonFilePath, wordcloudFileName);
|
|
|
+
|
|
|
+ String wordcloudImageFileName = "wordcloud.png";
|
|
|
+ File wordcloudImageFile = new File(pythonFilePath, wordcloudImageFileName);
|
|
|
+
|
|
|
+ // 如果词云文件存在,就先都备份一下,然后再生成新的
|
|
|
+ String timePrefix = DateUtils.dateTimeNow();
|
|
|
+ if (wordcloudFile.exists()) {
|
|
|
+ FileUtil.copyFile(wordcloudFile, new File(pythonFilePath, timePrefix + "-" + wordcloudFileName));
|
|
|
}
|
|
|
- if (outputStream != null) {
|
|
|
- outputStream.close();
|
|
|
+ if (wordcloudImageFile.exists()) {
|
|
|
+ FileUtil.copyFile(wordcloudImageFile, new File(pythonFilePath, timePrefix + "-" + wordcloudImageFileName));
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @PostMapping("/generate")
|
|
|
- public Map generate() throws Exception {
|
|
|
- Map rs = new HashMap();
|
|
|
- // 先判断有没有 wordcloud.txt, 没有就生成一个,有就备份一个,然后重新生成新的内容
|
|
|
- String wordcloudFileName = "wordcloud.txt";
|
|
|
- File wordcloudFile = new File(pythonFilePath, wordcloudFileName);
|
|
|
-
|
|
|
- String wordcloudImageFileName = "wordcloud.png";
|
|
|
- File wordcloudImageFile = new File(pythonFilePath, wordcloudImageFileName);
|
|
|
-
|
|
|
- // 如果词云文件存在,就先都备份一下,然后再生成新的
|
|
|
- String timePrefix = DateUtils.dateTimeNow();
|
|
|
- if (wordcloudFile.exists()) {
|
|
|
- FileUtil.copyFile(wordcloudFile, new File(pythonFilePath, timePrefix + "-" + wordcloudFileName));
|
|
|
- }
|
|
|
- if (wordcloudImageFile.exists()) {
|
|
|
- FileUtil.copyFile(wordcloudImageFile, new File(pythonFilePath, timePrefix + "-" + wordcloudImageFileName));
|
|
|
- }
|
|
|
- //TODO 获取新的内容,写入 wordcloud.txt
|
|
|
-
|
|
|
- // 调用python脚本, 生成新的词云图片
|
|
|
- String[] commands = {pythonPath, "/usr/local/python/word-cloud.py"};
|
|
|
- ProcessBuilder processBuilder = new ProcessBuilder(commands);
|
|
|
- Process process = processBuilder.start();
|
|
|
- // 等待命令执行完成
|
|
|
- int exitCode = process.waitFor();
|
|
|
- System.out.println("命令执行结果:" + exitCode);
|
|
|
- rs.put("exitCode", exitCode);
|
|
|
- return rs;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 执行Linux命令
|
|
|
- *
|
|
|
- * @param command 需要执行的Linux命令
|
|
|
- * @return 执行结果
|
|
|
- */
|
|
|
- public String executeCommand(String command) throws IOException {
|
|
|
- // 创建一个新进程
|
|
|
- Process process = Runtime.getRuntime().exec(command);
|
|
|
-
|
|
|
- // 获取进程的输入流并转换为字符串
|
|
|
- InputStream inputStream = process.getInputStream();
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- String line;
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
- result.append(line).append("\n");
|
|
|
+
|
|
|
+ //获取新的内容,写入 wordcloud.txt
|
|
|
+ List<String> wordCloudInfo = userPortraitService.getWordCloudInfo();
|
|
|
+ FileWriter out = new FileWriter(wordcloudFile);
|
|
|
+ BufferedWriter bw= new BufferedWriter(out);
|
|
|
+ for (String info : wordCloudInfo) {
|
|
|
+ bw.write(info + "\t");
|
|
|
+ bw.newLine();
|
|
|
+ }
|
|
|
+ bw.close();
|
|
|
+ out.close();
|
|
|
+
|
|
|
+ // 调用python脚本, 生成新的词云图片
|
|
|
+ String[] commands = {pythonPath, "/usr/local/python/word-cloud.py"};
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder(commands);
|
|
|
+ Process process = processBuilder.start();
|
|
|
+ // 等待命令执行完成
|
|
|
+ int exitCode = process.waitFor();
|
|
|
+ System.out.println("命令执行结果:" + exitCode);
|
|
|
+ rs.put("exitCode", exitCode);
|
|
|
+ return rs;
|
|
|
}
|
|
|
|
|
|
- // 关闭输入流和进程
|
|
|
- reader.close();
|
|
|
- process.destroy();
|
|
|
|
|
|
- return result.toString();
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 执行Linux命令
|
|
|
+ *
|
|
|
+ * @param command 需要执行的Linux命令
|
|
|
+ * @return 执行结果
|
|
|
+ */
|
|
|
+ public String executeCommand(String command) throws IOException {
|
|
|
+ // 创建一个新进程
|
|
|
+ Process process = Runtime.getRuntime().exec(command);
|
|
|
+
|
|
|
+ // 获取进程的输入流并转换为字符串
|
|
|
+ InputStream inputStream = process.getInputStream();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ result.append(line).append("\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 关闭输入流和进程
|
|
|
+ reader.close();
|
|
|
+ process.destroy();
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
}
|