MCPcopy Create free account
hub / github.com/LisenCoding/cangku / CommonController

Class CommonController

boot/src/main/java/com/controller/CommonController.java:43–685  ·  view source on GitHub ↗

通用接口

Source from the content-addressed store, hash-verified

41 * 通用接口
42 */
43@RestController
44public class CommonController {
45 private static final Logger logger = LoggerFactory.getLogger(CommonController.class);
46 @Autowired
47 private CommonService commonService;
48
49
50 /**
51 * Java代码实现MySQL数据库导出
52 *
53 * @param mysqlUrl MySQL安装路径
54 * @param hostIP MySQL数据库所在服务器地址IP
55 * @param userName 进入数据库所需要的用户名
56 * @param hostPort 数据库端口
57 * @param password 进入数据库所需要的密码
58 * @param savePath 数据库文件保存路径
59 * @param fileName 数据库导出文件文件名
60 * @param databaseName 要导出的数据库名
61 * @return 返回true表示导出成功,否则返回false。
62 */
63 @IgnoreAuth
64 @RequestMapping("/beifen")
65 public R beifen(String mysqlUrl, String hostIP, String userName, String hostPort, String password, String savePath, String fileName, String databaseName) {
66 File saveFile = new File(savePath);
67 if (!saveFile.exists()) {// 如果目录不存在 
68 saveFile.mkdirs();// 创建文件夹 
69 }
70 if (!savePath.endsWith(File.separator)) {
71 savePath = savePath + File.separator;
72 }
73 PrintWriter printWriter = null;
74 BufferedReader bufferedReader = null;
75 try {
76 Runtime runtime = Runtime.getRuntime();
77 String cmd = mysqlUrl + "mysqldump -h" + hostIP + " -u" + userName + " -P" + hostPort + " -p" + password + " " + databaseName;
78 runtime.exec(cmd);
79 Process process = runtime.exec(cmd);
80 InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
81 bufferedReader = new BufferedReader(inputStreamReader);
82 printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));
83 String line;
84 while ((line = bufferedReader.readLine()) != null) {
85 printWriter.println(line);
86 }
87 printWriter.flush();
88 } catch (Exception e) {
89 e.printStackTrace();
90 return R.error("备份数据出错");
91 } finally {
92 try {
93 if (bufferedReader != null) {
94 bufferedReader.close();
95 }
96 if (printWriter != null) {
97 printWriter.close();
98 }
99 } catch (Exception e) {
100 e.printStackTrace();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected