523096025 пре 2 година
родитељ
комит
a547e6ae81

+ 14 - 0
admin/pom.xml

@@ -89,6 +89,20 @@
             <artifactId>mybatis-plus-generator</artifactId>
             <version>3.2.0</version>
         </dependency>
+        <!--//ftp上传-->
+        <dependency>
+            <groupId>commons-net</groupId>
+            <artifactId>commons-net</artifactId>
+            <version>3.3</version>
+        </dependency>
+        <!--sftp文件上传-->
+        <dependency>
+            <groupId>com.jcraft</groupId>
+            <artifactId>jsch</artifactId>
+            <version>0.1.54</version>
+        </dependency>
+
+
     </dependencies>
 
     <build>

+ 33 - 0
admin/src/main/java/com/huimv/farm/damsubsidy/SubsidyApplication.java

@@ -1,8 +1,14 @@
 package com.huimv.farm.damsubsidy;
 
+import org.apache.catalina.Context;
+import org.apache.catalina.connector.Connector;
+import org.apache.tomcat.util.descriptor.web.SecurityCollection;
+import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
+import org.springframework.context.annotation.Bean;
 
 @SpringBootApplication
 @MapperScan("com.huimv.farm.damsubsidy.mapper")
@@ -12,6 +18,33 @@ public class SubsidyApplication {
         SpringApplication.run(SubsidyApplication.class, args);
     }
 
+    @Bean
+    public TomcatServletWebServerFactory servletContainer() {
+        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
+            @Override
+            protected void postProcessContext(Context context) {
+                SecurityConstraint constraint = new SecurityConstraint();
+                constraint.setUserConstraint("CONFIDENTIAL");
+                SecurityCollection collection = new SecurityCollection();
+                collection.addPattern("/*");
+                constraint.addCollection(collection);
+                context.addConstraint(constraint);
+            }
+        };
+        tomcat.addAdditionalTomcatConnectors(httpConnector());
+        return tomcat;
+    }
 
+    @Bean
+    public Connector httpConnector() {
+        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
+        connector.setScheme("http");
+        //Connector监听的http的默认端口号
+        connector.setPort(10011);
+        connector.setSecure(false);
+        //监听到http的端口号后转向到的https的端口号,也就是项目配置的port
+        connector.setRedirectPort(8010);
+        return connector;
+    }
 
 }

+ 104 - 0
admin/src/main/java/com/huimv/farm/damsubsidy/common/utils/UploadImage.java

@@ -0,0 +1,104 @@
+package com.huimv.farm.damsubsidy.common.utils;
+
+import com.jcraft.jsch.*;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.io.OutputStream;
+
+@Component
+public class UploadImage {
+
+    private static  String basePath = "/home/huimv/img/";
+
+    private static  String ip = "139.9.167.178";
+
+    private static  String user = "huimv";
+
+    private static  String password = "!hm123@1";
+
+    public static  Integer port = 22;
+
+
+    /**
+     * 利用JSch包实现SFTP上传文件
+     * @param bytes  文件字节流
+     * @param fileName  文件名
+     * @throws Exception
+     */
+    public static void sshSftp(byte[] bytes,String path,String fileName) throws Exception{
+
+
+        // 服务器保存路径
+        String filepath = basePath +path ;
+        Session session = null;
+        Channel channel = null;
+
+        JSch jSch = new JSch();
+
+        if(port <=0){
+            //连接服务器,采用默认端口
+            session = jSch.getSession(user, ip);
+        }else{
+            //采用指定的端口连接服务器
+            session = jSch.getSession(user, ip ,port);
+        }
+
+        //如果服务器连接不上,则抛出异常
+        if (session == null) {
+            throw new Exception("session is null");
+        }
+
+        //设置登陆主机的密码
+        session.setPassword(password);//设置密码
+        //设置第一次登陆的时候提示,可选值:(ask | yes | no)
+        session.setConfig("userauth.gssapi-with-mic","no");
+        session.setConfig("StrictHostKeyChecking", "no");
+        //设置登陆超时时间
+        session.connect(30000);
+
+        OutputStream outstream = null;
+        try {
+            //创建sftp通信通道
+            channel = (Channel) session.openChannel("sftp");
+            channel.connect(1000);
+            ChannelSftp sftp = (ChannelSftp) channel;
+
+            //进入服务器指定的文件夹
+            sftp.cd(basePath);
+
+            SftpATTRS attrs = null;
+            try {
+                attrs = sftp.stat(filepath);
+            } catch (Exception e) {
+                // TODO: handle exception
+            }
+            if (attrs == null) {
+                sftp.mkdir(filepath);
+                System.out.println(("创建子目录:" + filepath));
+                sftp.cd(filepath);
+            }
+
+            //以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换一下流就可以了
+            outstream = sftp.put(fileName);
+            outstream.write(bytes);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            //关流操作
+            if (outstream != null) {
+                outstream.flush();
+                outstream.close();
+            }
+            if (session != null) {
+                session.disconnect();
+            }
+            if (channel != null) {
+                channel.disconnect();
+            }
+            System.out.println("上传成功!");
+        }
+    }
+}
+

+ 76 - 0
admin/src/main/java/com/huimv/farm/damsubsidy/controller/ImagesTest.java

@@ -0,0 +1,76 @@
+package com.huimv.farm.damsubsidy.controller;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.lang.UUID;
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.crypto.SecureUtil;
+import cn.hutool.crypto.digest.MD5;
+import cn.hutool.crypto.symmetric.AES;
+import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
+import cn.hutool.crypto.symmetric.SymmetricCrypto;
+import com.huimv.farm.damsubsidy.common.utils.UploadImage;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.*;
+import java.util.Date;
+
+@CrossOrigin
+@RestController
+@RequestMapping("/image")
+public class ImagesTest {
+
+    @Value("${img.port}")
+    public static  Integer port;
+    @PostMapping("/test")
+    public  String test( @RequestParam("image") MultipartFile image ){
+        String imgname = "成功";
+        String originalFilename = image.getOriginalFilename();
+        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
+        String path = DateUtil.format(new Date(),"yyyy-MM");
+        try {
+            InputStream inputStream = image.getInputStream();
+            BufferedInputStream in = new BufferedInputStream(inputStream);
+            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
+
+            byte[] temp = new byte[1024];
+            int size = 0;
+            while ((size = in.read(temp)) != -1) {
+                out.write(temp, 0, size);
+            }
+            in.close();
+            byte[] content = out.toByteArray();
+            imgname =UUID.randomUUID()+"."+filenameExtension;
+            UploadImage.sshSftp(content,path, imgname);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        String content = "http://139.9.167.178/images"+path +"/"+imgname;
+
+        return content;
+//        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
+//        System.out.println(key);
+
+//构建
+//        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
+//加密为16进制表示
+      //  aes.encryptHex(content);
+        //        System.out.println(encryptHex);
+////解密为字符串
+//        String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
+//        System.out.println(decryptStr);
+//        return aes.encryptHex(content);
+    }
+
+//    public static void main(String[] args) {
+//
+//        String content = "http://139.9.167.178/images";
+//        MD5 md5 = MD5.create();
+//        String s = md5.digestHex16(content);
+//        System.out.println(s);
+//    }
+
+
+}

BIN
admin/src/main/resources/9847401_subsidy.ifarmcloud.com.jks


+ 13 - 0
admin/src/main/resources/META-INF/additional-spring-configuration-metadata.json

@@ -0,0 +1,13 @@
+{
+  "properties": [
+    {
+      "name": "img.basepath",
+      "type": "java.lang.String",
+      "description": "Description for img.basepath."
+    },
+    {
+      "name": "img.port",
+      "type": "java.lang.String",
+      "description": "Description for img.port."
+    }
+  ] }

+ 5 - 1
admin/src/main/resources/application-dev.yml

@@ -1,5 +1,9 @@
 server:
   port: 8010
+  ssl:
+    key-store: classpath:9847401_subsidy.ifarmcloud.com.jks
+    key-store-type: jks
+    key-store-password: mwasg0g9
 
 spring:
   application:
@@ -45,4 +49,4 @@ spring:
 mybatis-plus:
   configuration:
 #    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
-    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

+ 7 - 0
admin/src/main/resources/application.properties

@@ -1,3 +1,10 @@
 spring.profiles.active=dev
 #spring.profiles.active=prod
 
+img.basepath="/home/huimv/img/"
+img.ip="139.9.167.178"
+img.user="huimv"
+img.password="!hm123@1"
+img.port=22
+
+