瀏覽代碼

创建项目

523096025 8 月之前
父節點
當前提交
a431e88007

+ 10 - 0
musk/pom.xml

@@ -63,6 +63,16 @@
             <artifactId>fastjson</artifactId>
             <version>1.2.28</version>
         </dependency>
+        <!--sftp文件上传-->
+        <dependency>
+            <groupId>com.jcraft</groupId>
+            <artifactId>jsch</artifactId>
+            <version>0.1.54</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
         <dependency>
             <groupId>io.jsonwebtoken</groupId>
             <artifactId>jjwt</artifactId>

+ 7 - 7
musk/src/main/java/com/huimv/farm/musk/common/token/TokenConstant.java

@@ -1,20 +1,20 @@
 package com.huimv.farm.musk.common.token;
- 
+
 import java.util.HashMap;
 import java.util.Map;
 
 public class TokenConstant {
- 
+
     private static Map<String, String> map=new HashMap();
- 
- 
+
+
     public static String getToken(){
             return map.get("token");
     }
- 
+
     public static void updateTokenMap(String token){
         map.put("token",token);
     }
- 
- 
+
+
 }

+ 184 - 0
musk/src/main/java/com/huimv/farm/musk/common/utils/UploadImage.java

@@ -0,0 +1,184 @@
+package com.huimv.farm.musk.common.utils;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.lang.UUID;
+import com.jcraft.jsch.*;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.*;
+import java.util.Date;
+
+@Component
+public class UploadImage {
+
+    @Value("${img.basePath}")
+    private String basePath;
+
+    @Value("${img.ip}")
+    private String ip ;
+//    private   String ip = "36.134.209.211";
+//    private   String ip = "192.168.0.13";
+
+    @Value("${img.user}")
+    private String user ;
+
+//    private   String password = "!Hm537e@1";
+
+    @Value("${img.password}")
+    private String password ;
+
+    @Value("${img.port}")
+    public Integer port ;
+
+    @Value("${img.url}")
+    public String url ;
+
+
+
+    /**
+     * 利用JSch包实现SFTP上传文件
+     * @param bytes  文件字节流
+     * @param fileName  文件名
+     * @throws Exception
+     */
+    public   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("上传成功!");
+        }
+    }
+
+    //上传压缩的图片
+    public String getImageCom(MultipartFile image) throws IOException {
+        //获取文件输入流
+        InputStream inputStream = image.getInputStream();
+        String originalFilename = image.getOriginalFilename();
+        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
+        String path = DateUtil.format(new Date(), "yyyy-MM");
+
+        try {
+            // 把图片读入到内存中
+            BufferedImage bufImg = ImageIO.read(inputStream);
+            // 压缩代码,存储图片文件byte数组
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            //防止图片变红,这一步非常重要
+            BufferedImage bufferedImage = new BufferedImage(bufImg.getWidth(), bufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
+            bufferedImage.createGraphics().drawImage(bufImg,0,0, Color.WHITE,null);
+            //先转成jpg格式来压缩,然后在通过OSS来修改成源文件本来的后缀格式
+            ImageIO.write(bufferedImage,"jpg",bos);
+            byte[] bytes = bos.toByteArray();
+            String imgname = UUID.randomUUID() + "." + filenameExtension;
+            sshSftp(bytes, path, imgname);
+            return url+ path + "/" + imgname;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "上传失败";
+        } finally {
+            inputStream.close();
+        }
+    }
+
+
+    //上传原图
+    public String uploadImg(MultipartFile image ) {
+        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();
+            String imgname = UUID.randomUUID() + "." + filenameExtension;
+            sshSftp(content, path, imgname);
+            return url + path + "/" + imgname;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "上传失败";
+        }
+    }
+
+}
+
+
+

+ 0 - 6
musk/src/main/java/com/huimv/farm/musk/config/InterceptorConfig.java

@@ -5,12 +5,6 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
-/**
- * @Description: 注册验证tocken的拦截器
- * @Author
- * @Date 2021/4/19 20:41
- * @Version V1.0
- */
 @Configuration
 public class InterceptorConfig implements WebMvcConfigurer {
     @Bean

+ 7 - 0
musk/src/main/resources/application-dev.yml

@@ -19,3 +19,10 @@ mybatis-plus:
 #    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 
+img:
+  url: https://img.ifarmcloud.com/images/
+  basePath: /home/huimv/img/
+  ip: 119.3.44.183
+  user:  huimv
+  password: "!hm123@1"
+  port: 22