ViewController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.zskk.control;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import org.apache.commons.net.ftp.FTPClient;
  13. import org.apache.commons.net.ftp.FTPFile;
  14. import org.apache.commons.net.ftp.FTPReply;
  15. import com.jfinal.core.Controller;
  16. import com.jfinal.kit.PropKit;
  17. import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
  18. import com.jfinal.plugin.activerecord.Db;
  19. import com.jfinal.plugin.activerecord.Record;
  20. import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
  21. import com.jfinal.plugin.druid.DruidPlugin;
  22. import okhttp3.FormBody;
  23. import okhttp3.MediaType;
  24. import okhttp3.OkHttpClient;
  25. import okhttp3.Request;
  26. import okhttp3.RequestBody;
  27. import okhttp3.Response;
  28. public class ViewController extends Controller {
  29. private static final MediaType JSON_CODE = MediaType.get("application/json; charset=utf-8");
  30. private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  31. /**
  32. * 在被连接数据库执行sql语句
  33. */
  34. public void executeSql() {
  35. try {
  36. List<Record> d = Db.use("connected").find(this.getPara("sqlstr"));
  37. this.renderJson(d);
  38. } catch (Exception e) {
  39. // TODO: handle exception
  40. this.renderText(e.toString());
  41. }
  42. }
  43. public void executeSqlFtp() {
  44. try {
  45. List<Record> d = Db.use("ftp").find(this.getPara("sqlstr"));
  46. this.renderJson(d);
  47. } catch (Exception e) {
  48. // TODO: handle exception
  49. this.renderText(e.toString());
  50. }
  51. }
  52. public void testConn() {
  53. try {
  54. DruidPlugin druidPluginConnected = createConnectedDruidPlugin();
  55. druidPluginConnected.start();
  56. // 配置ActiveRecord插件
  57. ActiveRecordPlugin arpConnected = new ActiveRecordPlugin("connected", druidPluginConnected);
  58. arpConnected.setDialect(new SqlServerDialect());
  59. arpConnected.start();
  60. } catch (Exception e) {
  61. // TODO: handle exception
  62. this.renderText(e.toString());
  63. }
  64. }
  65. public static DruidPlugin createConnectedDruidPlugin() {
  66. return new DruidPlugin(PropKit.get("jdbcUrl_connected"), PropKit.get("user_connected"),PropKit.get("password_connected").trim());
  67. }
  68. public void downloadFtpFile() {
  69. String remoteFileName = this.getPara("a");
  70. String fileName = "";
  71. FTPClient ftpClient = new FTPClient();
  72. int reply;
  73. try {
  74. ftpClient.connect(PropKit.get("ftp_host"), PropKit.getInt("ftp_port"));
  75. ftpClient.login(PropKit.get("ftp_user"), PropKit.get("ftp_password"));
  76. reply = ftpClient.getReplyCode();
  77. if (!FTPReply.isPositiveCompletion(reply)) {
  78. ftpClient.disconnect();
  79. return;
  80. }
  81. ftpClient.setControlEncoding("UTF-8");
  82. ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  83. ftpClient.enterLocalPassiveMode();
  84. FTPFile allFile[] = ftpClient.listFiles(remoteFileName);
  85. for (int i = 0; i < allFile.length; i++) {
  86. File localFile = new File("/home/zskk/FTP_FILE" + File.separatorChar + allFile[i].getName());
  87. OutputStream os = new FileOutputStream(localFile);
  88. //ftp中文名需要iso-8859-1字符
  89. boolean flag2 = ftpClient.retrieveFile(remoteFileName+File.separatorChar+allFile[i].getName(), os);
  90. if (!flag2) {
  91. fileName=fileName+ allFile[i].getName();
  92. System.out.println("没有找到" + remoteFileName + "---该文件");
  93. // localFile.delete();
  94. } else {
  95. System.out.println("=================== save success");
  96. }
  97. os.close();
  98. }
  99. ftpClient.logout();
  100. ftpClient.disconnect();
  101. renderText(fileName);
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. /**
  107. * post请求
  108. * @param url-请求地址
  109. * @param map-参数集合
  110. * @return
  111. */
  112. private static String doPost(String url, Map<String, String> map) {
  113. FormBody.Builder builder = new FormBody.Builder();
  114. for (String key : map.keySet()) {
  115. builder.add(key, map.get(key));
  116. }
  117. RequestBody formBody = builder.build();
  118. Request request = new Request.Builder().url(url).post(formBody).build();
  119. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  120. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  121. String content = response.body().string();
  122. return content;
  123. } catch (IOException e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();
  126. return null;
  127. }
  128. }
  129. /**
  130. * 日期字符串格式转换
  131. * @param dateStr
  132. * @return
  133. */
  134. private Date parseStringToDate(String dateStr) {
  135. if (dateStr == null) {
  136. return new Date();
  137. }
  138. SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  139. Date date = null;
  140. try {
  141. date = sdf.parse(dateStr);
  142. } catch (ParseException e) {
  143. // TODO Auto-generated catch block
  144. e.printStackTrace();
  145. }
  146. return date;
  147. }
  148. public static void main(String[] args) {
  149. Map<String,String> paramsMap=new HashMap<String,String>();
  150. paramsMap.put("institution_id", "44100001");
  151. String contentString = doPost("https://risserver3.pacsonline.cn/butt/getExam/butt/getExam", paramsMap);
  152. System.out.println(contentString);
  153. }
  154. }