package com.zskk.service; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; import java.util.Random; import java.util.UUID; import org.apache.commons.io.FileUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.inspurcloud.oss.client.impl.OSSClientImpl; import com.inspurcloud.oss.model.ObjectMetadata; import com.jfinal.kit.PropKit; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class DataService { private static String GET_EXAM_URL = "https://risserver3.pacsonline.cn/butt/getExam"; private static String SAVE_REPORT_URL = "https://risserver3.pacsonline.cn/butt/saveReport"; private static String SAVE_ANNEX_URL = "https://risserver3.pacsonline.cn/butt/saveAnnex"; private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient(); private static final OSSClientImpl OSS_CLIENT = new OSSClientImpl(PropKit.get("oss_endpoint"), PropKit.get("oss_accessKey"), PropKit.get("oss_secretKey")); /** * 获取未出报告的检查列表 * @param instutionId * @param number */ public JSONArray getExamList(Integer number) { Map map = new HashMap(); map.put("institution_id", PropKit.get("institution_id")); map.put("num", number.toString()); String content = postWithParameters(GET_EXAM_URL, map); JSONObject jsonObject = JSON.parseObject(content); if (!jsonObject.getString("msg").equals("success")) { return null; } JSONArray jsonArray = JSON.parseArray(jsonObject.getString("data")); return jsonArray; } /** * 写入报告 * @param instutionId * @param number */ public void saveReport(Map map) { postWithParameters(SAVE_REPORT_URL, map); } /** * 保存附件 * @param instutionId * @param number */ public void saveAnnex(Map map, String filePath) { String newUrl = filePath.replaceAll("\\\\", "/") ; String fileNameStr[] = newUrl.split("/"); String fileName = fileNameStr[fileNameStr.length-1]; String fileStorePath =getFileWithUrl(newUrl, fileName); File file =new File(fileStorePath); String eTag = getFileMd5(file); String key = PropKit.get("institution_id")+"/"+eTag; Boolean eBoolean = OSS_CLIENT.doesObjectExist(PropKit.get("oss_bucketName"), key); if (!eBoolean) { OSS_CLIENT.putObject(PropKit.get("oss_bucketName"), key, file); Boolean bBoolean = OSS_CLIENT.doesObjectExist(PropKit.get("oss_bucketName"), key); if (bBoolean) { file.delete(); map.put("url", "https://annex.oss.cn-north-3.inspurcloudoss.com/"+key); map.put("name", key); postWithParameters(SAVE_ANNEX_URL, map); } } } private String getFileMd5(File file) { StringBuffer sb = new StringBuffer(""); try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(FileUtils.readFileToByteArray(file)); byte b[] = md.digest(); int d; for (int i = 0; i < b.length; i++) { d = b[i]; if (d < 0) { d = b[i] & 0xff; // 与上一行效果等同 // i += 256; } if (d < 16) sb.append("0"); sb.append(Integer.toHexString(d)); } System.out.println(sb); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } public static String postWithParameters(String url, Map map) { FormBody.Builder formbody = new FormBody.Builder(); for (Map.Entry entry : map.entrySet()) { formbody.add(entry.getKey(), entry.getValue()); } RequestBody requestBody = formbody.build(); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); try (Response response = OKHTTP_CLIENT.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String content = response.body().string(); return content; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public static String getFileWithUrl(String url, String filename) { Request request = new Request.Builder() .url(url) .build(); try (Response response = OKHTTP_CLIENT.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); InputStream inputStream = response.body().source().inputStream(); //本地文件夹目录(下载位置) String folder = PropKit.get("oss_localPath"); //下载文件保存位置 String savepath=folder+"/"+filename; BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(savepath))); byte[] data=new byte[1024]; int len; int available = inputStream.available(); while ((len=inputStream.read(data))!=-1){ bufferedOutputStream.write(data,0,len); } bufferedOutputStream.flush(); bufferedOutputStream.close(); inputStream.close(); return savepath; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } public static void main(String[] args) { // Map map = new HashMap(); // map.put("institution_id", "47600001"); // map.put("num", "10"); // postWithParameters(GET_EXAM_URL, map); // getFileWithUrl("https://annex.oss.cn-north-3.inspurcloudoss.com/111/b.pdf"); // String bucketName = "annex"; // String key = "111/a.pdf"; // File file = new File("/Users/liutao/Desktop/asdadsad.pdf"); // // String endpoint = "oss.cn-north-3.inspurcloudoss.com"; // String accessKey = "ZGEzOTY3YWQtNWI1OC00Y2MxLWJmNTgtZWVhN2M0ZjI5NTI4"; // String secretKey = "YTc1NDQwZjItOWQ1OS00N2E3LTg4YmQtZjNjNjgzNzRjODQ5"; // OSSClientImpl ossClient = new OSSClientImpl(endpoint, accessKey, secretKey); // // //简单文件上传 // ossClient.putObject(bucketName, key, file); // ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, "111/a.pdf"); // // System.out.println(objectMetadata); //创建OSSClient实例 // Boolean aBoolean = ossClient.doesObjectExist(bucketName, key); // System.out.println(aBoolean); } }