Просмотр исходного кода

设备程序管理上传文件到机床

yejian 3 лет назад
Родитель
Сommit
b933fce82f

+ 11 - 0
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/productionResourceCenter/service/impl/ProgramServiceImpl.java

@@ -104,6 +104,7 @@ public class ProgramServiceImpl extends SuperServiceImpl<ProgramMapper, Program>
         JSONObject jsonObject = new JSONObject();
         //上传接口
         Productionresource productionresource = productionresourceMapper.selectById(data.getResourceId());
+        Module resourceModule = moduleService.getById(productionresource.getModuleId());
         List<Map<String, String>> fileList = new ArrayList<Map<String, String>>();
         Map<String, String> m = new HashMap<>();
 //        m.put("fileName", data.getSubmittedFileName());
@@ -123,7 +124,17 @@ public class ProgramServiceImpl extends SuperServiceImpl<ProgramMapper, Program>
         HttpEntity<String> formEntity = new HttpEntity<String>(jsonParam, headers);
         //调用接口
 //        String returnData = restTemplate.postForObject(UrlConfConstant.plcURL, formEntity, String.class);
+        String localFolder = uploadFolder + "0000/cncFile/" + productionresource.getId();
+        String fileName = data.getFilePath().substring(data.getFilePath().lastIndexOf("/") + 1);
+        String localFilePathName = localFolder + "/" + fileName;
+        // TODO 西门子程序scp上传
+        String ip = productionresource.getIp();
+        String port = "22";
+        String userName = "manufact";
+        String password = "SUNRISE";
+        Scpclient scpclient = new Scpclient(ip,Integer.valueOf(port),userName,password);
 
+        scpclient.uploadFileIO(localFilePathName,fileName,resourceModule.getCncFilePath());
         Program program = BeanPlusUtil.toBean(data, Program.class);
         //根据编码规则
         save(program);

+ 1 - 1
imcs-admin-boot/imcs-common/pom.xml

@@ -76,7 +76,7 @@
         <dependency>
             <groupId>ch.ethz.ganymed</groupId>
             <artifactId>ganymed-ssh2</artifactId>
-            <version>build210</version>
+            <version>262</version>
         </dependency>
     </dependencies>
 

+ 53 - 4
imcs-admin-boot/imcs-common/src/main/java/com/github/zuihou/common/util/Scpclient.java

@@ -1,12 +1,10 @@
 package com.github.zuihou.common.util;
 
+import ch.ethz.ssh2.*;
+
 import java.io.*;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import ch.ethz.ssh2.*;
 
 public class Scpclient {
 
@@ -125,6 +123,57 @@ public class Scpclient {
         bos.close();
     }
 
+    private byte[] readFileBytes(InputStream is){
+        byte[] data = null;
+        try {
+            //严谨起见,一定要加上这个判断,不要返回data[]长度为0的数组指针
+            if(is.available()==0){
+                return data;
+            }
+            data = new byte[is.available()];
+            is.read(data);
+            is.close();
+            return data;
+        } catch (IOException e) {
+            return data;
+        }
+    }
+
+    public void uploadFileIO(String localFile, String remoteFileName, String remoteTargetDirectory) {
+        Connection connection = null;
+        SCPOutputStream os = null;
+        try {
+
+            // 创建一个连接实例
+            connection = new Connection(ip, port);
+            // 连接
+            connection.connect();
+            // 认证
+            boolean isAuthenticated = connection.authenticateWithPassword(username, password);
+            // 创建一个会话
+            if(isAuthenticated){
+                SCPClient scpClient = connection.createSCPClient();
+                File localFileObj = new File(localFile);
+                FileInputStream fileInputStream = new FileInputStream(localFileObj);
+                byte[] localFileByte = readFileBytes(fileInputStream);
+                os = scpClient.put(remoteFileName,localFileByte.length,remoteTargetDirectory,null);
+                os.write(localFileByte,0,localFileByte.length);
+                os.flush();
+            }
+        } catch (Exception e){
+            throw new RuntimeException(e);
+        } finally {
+            if(null != os){
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            connection.close();
+        }
+    }
+
     public void remove(String remoteFilePath, String fileName) {
         Connection connection = null;
         Session sess = null;