gydx_2024_lyh 1 month ago
parent
commit
b8c0a0528c

+ 30 - 0
ruoyi-modules/ruoyi-system/pom.xml

@@ -85,6 +85,36 @@
             <scope>provided</scope>
         </dependency>
 
+        <dependency>
+            <groupId>com.jcraft</groupId>
+            <artifactId>jsch</artifactId>
+            <version>0.1.55</version>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-net</groupId>
+            <artifactId>commons-net</artifactId>
+            <version>3.8.0</version>
+        </dependency>
+
+
+        <!--二维码开发工具集-->
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>4.6.10</version>
+        </dependency><!-- zxing生成二维码 -->
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>core</artifactId>
+            <version>3.3.3</version>
+        </dependency>
+        <dependency>
+            <groupId>com.google.zxing</groupId>
+            <artifactId>javase</artifactId>
+            <version>3.3.3</version>
+        </dependency>
+
 
     </dependencies>
 

+ 18 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/enterprise/base/controller/SetSeq.java

@@ -0,0 +1,18 @@
+package com.ruoyi.system.enterprise.base.controller;
+
+
+import java.util.concurrent.atomic.AtomicLong;
+
+public class SetSeq {
+    private static AtomicLong counter = new AtomicLong(0); // 使用 AtomicLong提供原子操作,保证线程安全
+    public static void main(String[] args) {
+        SetSeq setseq = new SetSeq();
+        long randomString1 = setseq.createseq();
+        System.out.println(randomString1);
+    }
+    public static long createseq() {
+        return counter.getAndIncrement();
+    }
+
+
+}

+ 66 - 1
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/enterprise/base/controller/TECompareobjectRepeatController.java

@@ -1,8 +1,15 @@
 package com.ruoyi.system.enterprise.base.controller;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.util.List;
 import java.io.IOException;
 import javax.servlet.http.HttpServletResponse;
+
+import com.google.zxing.WriterException;
+import com.ruoyi.common.core.utils.uuid.UUID;
+import com.ruoyi.system.enterprise.base.util.QRCodeGenerator;
+import com.ruoyi.system.enterprise.base.util.SFTPFileUpload;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -21,6 +28,10 @@ import com.ruoyi.common.core.web.controller.BaseController;
 import com.ruoyi.common.core.web.domain.AjaxResult;
 import com.ruoyi.common.core.utils.poi.ExcelUtil;
 import com.ruoyi.common.core.web.page.TableDataInfo;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.concurrent.ConcurrentHashMap;
+
 
 /**
  * 实验项目-重复存储Controller
@@ -35,6 +46,45 @@ public class TECompareobjectRepeatController extends BaseController
     @Autowired
     private ITECompareobjectRepeatService tECompareobjectRepeatService;
 
+
+//    String base = "/usr/local/nginx/html/";
+String base ="C:\\Program Files\\north\\image\\";
+    String ip="8.130.113.97";
+    String port="20000";
+    @PostMapping(value = "/code/{id}")
+    public String code(@PathVariable("id") String id) throws IOException, WriterException {
+        System.out.println(1);
+        String path = base + id + ".png";
+
+//        String path = "/usr/local/nginx/html/files/" + id + ".png"; // 仍然使用服务器本地路径生成图片
+        System.out.println(id);
+        QRCodeGenerator.generateQRCodeImage(id, 100, 100, path);
+
+        String remoteDir = "/usr/local/nginx/html/files/";  // 远程服务器目录
+        String remoteHost = "8.130.113.97";                 // 远程服务器地址
+        String username = "root";                           // 远程服务器用户名
+        String password = "sys200888";                      // 远程服务器密码
+        int port = 22;
+        String port2 = "20000";
+        File localFile = new File(path);
+        if (!localFile.exists()) {
+            System.out.println("本地文件不存在: " + path);
+        }
+
+        try (FileInputStream fis = new FileInputStream(localFile)) {
+            // 调用工具类上传文件
+            SFTPFileUpload.uploadFile(fis, remoteDir, remoteHost, username, password, port, localFile.getName());
+            System.out.println("文件上传11");
+        } catch (IOException e) {
+            e.printStackTrace();
+            System.out.println("文件上传失败");
+        }
+
+
+        String url = "http://" + ip + ":" + port2 + "/files/files/" + id + ".png";
+        return url;
+//        return base + id + ".png"; // 返回服务器可访问的URL
+    }
     /**
      * 查询实验项目-重复存储列表
      */
@@ -75,10 +125,25 @@ public class TECompareobjectRepeatController extends BaseController
      */
     @RequiresPermissions("system.base:repeat:add")
     @Log(title = "实验项目-重复存储", businessType = BusinessType.INSERT)
-    @PostMapping
+    @PostMapping("/add")
     public AjaxResult add(@RequestBody TECompareobjectRepeat tECompareobjectRepeat)
     {
+        SetSeq setSeq = new SetSeq();
+
+        tECompareobjectRepeat.setSeq(setSeq.createseq());
+        tECompareobjectRepeat.setId(UUID.fastUUID().toString());
+        // 使用 code 方法生成二维码路径并设置到 qcode 属性
+        try {
+            String qrCodePath = code(tECompareobjectRepeat.getId());
+            tECompareobjectRepeat.setQcode(qrCodePath);
+        } catch (IOException | WriterException e) {
+            e.printStackTrace();
+            return AjaxResult.error("生成二维码失败");
+        }
+        System.out.println("测试一下"+tECompareobjectRepeat);
+
         return toAjax(tECompareobjectRepeatService.insertTECompareobjectRepeat(tECompareobjectRepeat));
+
     }
 
     /**

+ 62 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/enterprise/base/controller/UploadFileController.java

@@ -0,0 +1,62 @@
+package com.ruoyi.system.enterprise.base.controller;
+
+import com.ruoyi.common.core.web.domain.AjaxResult;
+import com.ruoyi.system.enterprise.base.util.SFTPFileUpload;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+@RestController
+@RequestMapping("/uploadFile")
+public class UploadFileController {
+
+    @PostMapping
+    public AjaxResult uploadFile(MultipartFile file) {
+        // 设置远程服务器的相关参数
+        String remoteDir = "/usr/local/nginx/html/files/";  // 远程服务器目录
+        String remoteHost = "8.130.113.97";    // 远程服务器地址
+        String username = "root";            // 远程服务器用户名
+        String password = "sys200888";           // 远程服务器密码
+        int port = 22;                           // SFTP 端口,通常为22
+
+        try {
+            // 调用工具类上传文件,直接使用MultipartFile的输入流
+            SFTPFileUpload.uploadFile(file.getInputStream(), remoteDir, remoteHost, username, password, port, file.getOriginalFilename());            // 返回成功信息
+            return AjaxResult.success("文件上传成功");
+        } catch (IOException e) {
+            e.printStackTrace();
+            return AjaxResult.error("文件上传失败");
+        }
+    }
+
+    @PostMapping("/local")
+    public AjaxResult uploadFileByPath(@RequestParam String localFilePath) {
+        // 设置远程服务器的相关参数
+        String remoteDir = "/usr/local/nginx/html/files/";  // 远程服务器目录
+        String remoteHost = "8.130.113.97";                 // 远程服务器地址
+        String username = "root";                           // 远程服务器用户名
+        String password = "sys200888";                      // 远程服务器密码
+        int port = 22;                                      // SFTP 端口
+
+        File localFile = new File(localFilePath);
+        if (!localFile.exists()) {
+            return AjaxResult.error("本地文件不存在: " + localFilePath);
+        }
+
+        try (FileInputStream fis = new FileInputStream(localFile)) {
+            // 调用工具类上传文件
+            SFTPFileUpload.uploadFile(fis, remoteDir, remoteHost, username, password, port, localFile.getName());
+            return AjaxResult.success("文件上传成功");
+        } catch (IOException e) {
+            e.printStackTrace();
+            return AjaxResult.error("文件上传失败");
+        }
+    }
+
+}

+ 2 - 2
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/enterprise/base/service/impl/TECompareobjectRepeatServiceImpl.java

@@ -54,10 +54,10 @@ public class TECompareobjectRepeatServiceImpl implements ITECompareobjectRepeatS
     @Override
     public int insertTECompareobjectRepeat(TECompareobjectRepeat tECompareobjectRepeat)
     {
-        tECompareobjectRepeat.setId(UUID.fastUUID().toString());
+//        tECompareobjectRepeat.setId(UUID.fastUUID().toString());
+
         return tECompareobjectRepeatMapper.insertTECompareobjectRepeat(tECompareobjectRepeat);
     }
-
     /**
      * 修改实验项目-重复存储
      * 

+ 24 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/enterprise/base/util/QRCodeGenerator.java

@@ -0,0 +1,24 @@
+package com.ruoyi.system.enterprise.base.util;
+
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.WriterException;
+import com.google.zxing.client.j2se.MatrixToImageWriter;
+import com.google.zxing.common.BitMatrix;
+import com.google.zxing.qrcode.QRCodeWriter;
+
+import java.io.IOException;
+import java.nio.file.FileSystems;
+import java.nio.file.Path;
+
+public class QRCodeGenerator {
+    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
+        QRCodeWriter qrCodeWriter = new QRCodeWriter();
+
+        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
+
+        Path path = FileSystems.getDefault().getPath(filePath);
+
+        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
+
+    }
+}

+ 45 - 0
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/enterprise/base/util/SFTPFileUpload.java

@@ -0,0 +1,45 @@
+package com.ruoyi.system.enterprise.base.util;
+
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.Session;
+
+
+import java.io.InputStream;
+
+public class SFTPFileUpload {
+
+    public static void uploadFile(InputStream inputStream, String remoteDir, String remoteHost, String username, String password, int port, String fileName) {
+        Session session = null;
+        ChannelSftp channelSftp = null;
+        try {
+            // 创建JSch对象
+            JSch jsch = new JSch();
+            // 创建会话
+            session = jsch.getSession(username, remoteHost, port);
+            session.setPassword(password);
+            session.setConfig("StrictHostKeyChecking", "no");
+            session.connect();
+
+            // 打开SFTP通道
+            channelSftp = (ChannelSftp) session.openChannel("sftp");
+            channelSftp.connect();
+
+            // 上传文件
+            System.out.println("准备上传文件: " + fileName);
+            channelSftp.put(inputStream, remoteDir + fileName);
+            System.out.println("上传到远程路径: " + remoteDir + fileName);
+            System.out.println("文件上传成功!");
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (channelSftp != null) {
+                channelSftp.disconnect();
+            }
+            if (session != null) {
+                session.disconnect();
+            }
+        }
+    }
+
+}