ViewController.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.zskk.control;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import com.jfinal.core.Controller;
  14. import com.jfinal.kit.PropKit;
  15. import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
  16. import com.jfinal.plugin.activerecord.Db;
  17. import com.jfinal.plugin.activerecord.Record;
  18. import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
  19. import com.jfinal.plugin.druid.DruidPlugin;
  20. import okhttp3.FormBody;
  21. import okhttp3.MediaType;
  22. import okhttp3.OkHttpClient;
  23. import okhttp3.Request;
  24. import okhttp3.RequestBody;
  25. import okhttp3.Response;
  26. public class ViewController extends Controller {
  27. private static final MediaType JSON_CODE = MediaType.get("application/json; charset=utf-8");
  28. private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  29. /**
  30. * 在被连接数据库执行sql语句
  31. */
  32. public void executeSql() {
  33. try {
  34. List<Record> d = Db.use("connected").find(this.getPara("sqlstr"));
  35. this.renderJson(d);
  36. } catch (Exception e) {
  37. // TODO: handle exception
  38. this.renderText(e.toString());
  39. }
  40. }
  41. /**
  42. * 在被连接数据库执行sql语句
  43. */
  44. public void executeSqlPrint() {
  45. try {
  46. List<Record> d = Db.use("print").find(this.getPara("sqlstr"));
  47. this.renderJson(d);
  48. } catch (Exception e) {
  49. // TODO: handle exception
  50. this.renderText(e.toString());
  51. }
  52. }
  53. public void testConn() {
  54. try {
  55. DruidPlugin druidPluginConnected = createConnectedDruidPlugin();
  56. druidPluginConnected.start();
  57. // 配置ActiveRecord插件
  58. ActiveRecordPlugin arpConnected = new ActiveRecordPlugin("connected", druidPluginConnected);
  59. arpConnected.setDialect(new SqlServerDialect());
  60. arpConnected.start();
  61. } catch (Exception e) {
  62. // TODO: handle exception
  63. this.renderText(e.toString());
  64. }
  65. }
  66. public static DruidPlugin createConnectedDruidPlugin() {
  67. return new DruidPlugin(PropKit.get("jdbcUrl_connected"), PropKit.get("user_connected"),PropKit.get("password_connected").trim());
  68. }
  69. public static String getFileWithUrl(String url, String filename) {
  70. Request request = new Request.Builder().url(url).build();
  71. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  72. if (!response.isSuccessful())
  73. throw new IOException("Unexpected code " + response);
  74. InputStream inputStream = response.body().source().inputStream();
  75. // 本地文件夹目录(下载位置)
  76. String folder = PropKit.get("oss_localPath");
  77. // 下载文件保存位置
  78. String savepath = folder + "/" + filename;
  79. BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(savepath)));
  80. byte[] data = new byte[1024];
  81. int len;
  82. int available = inputStream.available();
  83. while ((len = inputStream.read(data)) != -1) {
  84. bufferedOutputStream.write(data, 0, len);
  85. }
  86. bufferedOutputStream.flush();
  87. bufferedOutputStream.close();
  88. inputStream.close();
  89. return savepath;
  90. } catch (IOException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. return e.toString();
  94. }
  95. }
  96. public void testdld() {
  97. try {
  98. String filePath = "http://200.200.200.163:8090/X-0-202208261038400267000310351.PNG";
  99. String fileNameStr[] = filePath.split("/");
  100. String fileName = fileNameStr[fileNameStr.length - 1];
  101. String fileStorePath = getFileWithUrl(filePath, fileName);
  102. renderText(fileStorePath);
  103. } catch (Exception e) {
  104. // TODO: handle exception
  105. renderText(e.toString());
  106. }
  107. }
  108. /**
  109. * post请求
  110. * @param url-请求地址
  111. * @param map-参数集合
  112. * @return
  113. */
  114. private static String doPost(String url, Map<String, String> map) {
  115. FormBody.Builder builder = new FormBody.Builder();
  116. for (String key : map.keySet()) {
  117. builder.add(key, map.get(key));
  118. }
  119. RequestBody formBody = builder.build();
  120. Request request = new Request.Builder().url(url).post(formBody).build();
  121. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  122. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  123. String content = response.body().string();
  124. return content;
  125. } catch (IOException e) {
  126. // TODO Auto-generated catch block
  127. e.printStackTrace();
  128. return null;
  129. }
  130. }
  131. /**
  132. * 日期字符串格式转换
  133. * @param dateStr
  134. * @return
  135. */
  136. private Date parseStringToDate(String dateStr) {
  137. if (dateStr == null) {
  138. return new Date();
  139. }
  140. SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  141. Date date = null;
  142. try {
  143. date = sdf.parse(dateStr);
  144. } catch (ParseException e) {
  145. // TODO Auto-generated catch block
  146. e.printStackTrace();
  147. }
  148. return date;
  149. }
  150. public static void main(String[] args) {
  151. Map<String,String> paramsMap=new HashMap<String,String>();
  152. paramsMap.put("institution_id", "44100001");
  153. String contentString = doPost("https://risserver3.pacsonline.cn/butt/getExam/butt/getExam", paramsMap);
  154. System.out.println(contentString);
  155. }
  156. }