123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- 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.util.HashMap;
- import java.util.Map;
- import java.io.OutputStream;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPReply;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.jfinal.kit.PropKit;
- import okhttp3.FormBody;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.Response;
- import com.amazonaws.AmazonServiceException;
- import com.amazonaws.SdkClientException;
- import com.amazonaws.auth.AWSCredentialsProvider;
- import com.amazonaws.auth.AWSStaticCredentialsProvider;
- import com.amazonaws.auth.BasicAWSCredentials;
- import com.amazonaws.client.builder.AwsClientBuilder;
- import com.amazonaws.services.s3.AmazonS3;
- import com.amazonaws.services.s3.AmazonS3ClientBuilder;
- import com.amazonaws.services.s3.model.CannedAccessControlList;
- import com.amazonaws.services.s3.model.PutObjectRequest;
- 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 CREATE_REGISTER_URL = "https://risserver3.pacsonline.cn/butt/register";
- private static String SAVE_FILE_URL = "https://risserver3.pacsonline.cn/butt/saveFile";
-
- private static String SAVE_ANNEX_URL = "https://risserver3.pacsonline.cn/butt/saveAnnex";
-
- private static String UPDATE_PATIENT_URL = "https://risserver3.pacsonline.cn/butt/saveExam";
-
- private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
-
- /**
- * 获取未出报告的检查列表
- * @param instutionId
- * @param number
- */
- public JSONArray getExamList(Integer number) {
- Map <String,String> map = new HashMap<String,String>();
- 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 <String,String> map) {
- postWithParameters(SAVE_REPORT_URL, map);
-
- }
-
- /**
- * 更新患者信息
- *
- * @param instutionId
- * @param number
- */
- public void updatePatientInfo(Map<String, String> map) {
- postWithParameters(UPDATE_PATIENT_URL, map);
- }
-
- /**
- * 创建登记信息
- *
- * @param instutionId
- * @param number
- */
- public String createRegisterInfo(Map<String, String> map) {
- String content = postWithParameters(CREATE_REGISTER_URL, map);
- JSONObject jsonObject = JSON.parseObject(content);
- if (!jsonObject.getString("msg").equals("success")) {
- return null;
- }
- String data = jsonObject.getString("data");
- return data;
- }
-
- /**
- * 保存附件
- *
- * @param instutionId
- * @param number
- * @throws FileNotFoundException
- */
- public void saveAnnex(Map<String, String> map, String filePath) {
- AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(PropKit.get("oss_endpoint"), PropKit.get("region"));
- BasicAWSCredentials credentials = new BasicAWSCredentials(PropKit.get("oss_accessKey"), PropKit.get("oss_secretKey"));
- AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
- AmazonS3 client = AmazonS3ClientBuilder.standard()
- .withEndpointConfiguration(endpointConfiguration)
- .withCredentials(credentialsProvider).build();
-
- String fileNameStr[] = filePath.split("=");
- String fileName = fileNameStr[fileNameStr.length - 2];
- String fileStorePath = getFileWithUrl(filePath, fileName);
- File file = new File(fileStorePath);
- String key = PropKit.get("institution_id") + "/" + fileName;
-
- boolean exists = client.doesObjectExist(PropKit.get("oss_bucketName"), key);
- if (!exists) {
- PutObjectRequest request = new PutObjectRequest(PropKit.get("oss_bucketName"), key, file);
- request.setCannedAcl(CannedAccessControlList.PublicRead);
- client.putObject(request);
- boolean bexists = client.doesObjectExist(PropKit.get("oss_bucketName"), key);
- if (!bexists) {
- PutObjectRequest request2 = new PutObjectRequest(PropKit.get("oss_bucketName"), key, file);
- request.setCannedAcl(CannedAccessControlList.PublicRead);
- client.putObject(request);
- }
- }
- client.shutdown();
- map.put("url", "https://annex.eos.jinan-4.cmecloud.cn/" + key);
- map.put("name", key);
- postWithParameters(SAVE_ANNEX_URL, map);
-
- file.delete();
- }
-
- public static String postWithParameters(String url, Map<String, String> map) {
-
- FormBody.Builder formbody = new FormBody.Builder();
-
- for (Map.Entry<String, String> 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 "";
- }
- }
- //ftp://FTPUser:ftpuser@188.188.2.1:6161/20240726/1AFB48FCF39241AA8DE61F85EE6D847F/Report/Report.jpg"
- public String downloadFtpFile(String remoteFileName, String fileName) {
- FTPClient ftpClient = new FTPClient();
- int reply;
-
- try {
- ftpClient.connect(PropKit.get("ftp_host"), PropKit.getInt("ftp_port"));
- ftpClient.login(PropKit.get("ftp_user"), PropKit.get("ftp_password"));
- reply = ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftpClient.disconnect();
- return null;
- }
- ftpClient.setControlEncoding("UTF-8");
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- ftpClient.enterLocalPassiveMode();
- File localFile = new File("/home/zskk/FTP_FILE" + File.separatorChar + fileName);
- OutputStream os = new FileOutputStream(localFile);
- //ftp中文名需要iso-8859-1字符
- boolean flag2 = ftpClient.retrieveFile(new String(remoteFileName.getBytes("GBK"), "iso-8859-1"), os);
- if (!flag2) {
- System.out.println("没有找到" + remoteFileName + "---该文件");
- localFile.delete();
- } else {
- System.out.println("=================== save success");
- }
- os.close();
- ftpClient.logout();
- ftpClient.disconnect();
- return "/home/zskk/FTP_FILE" + File.separatorChar + fileName;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- public static void main(String[] args) {
- Map <String,String> map = new HashMap<String,String>();
- map.put("institution_id", "47600001");
- map.put("num", "10");
- postWithParameters(GET_EXAM_URL, map);
- }
- }
|