Browse Source

微信公众号,附件上传

sxb 1 year ago
parent
commit
eebd17d084

+ 6 - 1
collect-fees/collect-fees-service/pom.xml

@@ -94,7 +94,12 @@
             <artifactId>ojdbc6</artifactId>
             <version>${oracle.version}</version>
         </dependency>
-
+        <!--使用Apache Commons Net库来创建FTP业务接口并上传图片文件 -->
+        <dependency>
+            <groupId>commons-net</groupId>
+            <artifactId>commons-net</artifactId>
+            <version>3.6</version>
+        </dependency>
         <dependency>
             <groupId>com.tofly</groupId>
             <artifactId>common-log</artifactId>

+ 20 - 0
collect-fees/collect-fees-service/src/main/java/com/tofly/fees/common/Ftp/FtpConfig.java

@@ -0,0 +1,20 @@
+package com.tofly.fees.common.Ftp;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+@Component
+@Getter
+@Setter
+public class FtpConfig {
+    @Value("${ftp.server}")
+    private String server;
+    @Value("${ftp.port}")
+    private int port;
+    @Value("${ftp.userName}")
+    private String userName;
+    @Value("${ftp.password}")
+    private String password;
+    // 省略getter和setter
+}

+ 64 - 0
collect-fees/collect-fees-service/src/main/java/com/tofly/fees/common/Ftp/FtpService.java

@@ -0,0 +1,64 @@
+package com.tofly.fees.common.Ftp;
+
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPClient;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+@Component
+public class FtpService {
+    private static final String FTP_SERVER = "192.168.2.233";
+    private static final int FTP_PORT = 21;
+    private static final String FTP_USERNAME = "ftpuser";
+    private static final String FTP_PASSWORD = "ftpuser";
+    public void uploadFile(MultipartFile file, String remoteFilePath) throws IOException {
+        FTPClient ftpClient = new FTPClient();
+        String localFilePath = "/opt/ftp/fptpath/" + file.getOriginalFilename();
+        try {
+            ftpClient.connect(FTP_SERVER, FTP_PORT);
+            ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
+            ftpClient.enterLocalPassiveMode();
+            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
+            File convertedFile = convertMultiPartToFile(file);
+            FileInputStream inputStream = new FileInputStream(convertedFile);
+            ftpClient.storeFile(remoteFilePath, inputStream);
+          //  ftpClient.storeFile(remoteFilePath, new FileInputStream(localFilePath));
+            inputStream.close();
+            convertedFile.delete();
+        } finally {
+            if (ftpClient.isConnected()) {
+                ftpClient.logout();
+                ftpClient.disconnect();
+            }
+        }
+    }
+    public File downloadFile(String remoteFilePath, String localFilePath) throws IOException {
+        FTPClient ftpClient = new FTPClient();
+        try {
+            ftpClient.connect(FTP_SERVER, FTP_PORT);
+            ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
+            ftpClient.enterLocalPassiveMode();
+            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
+            File downloadedFile = new File(localFilePath);
+            FileOutputStream outputStream = new FileOutputStream(downloadedFile);
+            ftpClient.retrieveFile(remoteFilePath, outputStream);
+            outputStream.close();
+            return downloadedFile;
+        } finally {
+            if (ftpClient.isConnected()) {
+                ftpClient.logout();
+                ftpClient.disconnect();
+            }
+        }
+    }
+    private File convertMultiPartToFile(MultipartFile file) throws IOException {
+        File convertedFile = new File(file.getOriginalFilename());
+        FileOutputStream fileOutputStream = new FileOutputStream(convertedFile);
+        fileOutputStream.write(file.getBytes());
+        fileOutputStream.close();
+        return convertedFile;
+    }
+}

+ 1 - 0
collect-fees/collect-fees-service/src/main/java/com/tofly/fees/financialmgt/controller/BmPriceController.java

@@ -12,6 +12,7 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
 import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 import springfox.documentation.annotations.ApiIgnore;
 

+ 61 - 2
collect-fees/collect-fees-service/src/main/java/com/tofly/fees/wechatofficalacctmgt/controller/WxGhsqController.java

@@ -8,11 +8,15 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.tofly.common.core.entity.ResultRespone;
 import com.tofly.common.log.annotation.ToFlyAppLog;
+import com.tofly.fees.common.Ftp.FtpConfig;
+import com.tofly.fees.common.Ftp.FtpService;
 import com.tofly.fees.wechatofficalacctmgt.entity.WxGhsq;
 import com.tofly.fees.wechatofficalacctmgt.service.WxGhsqService;
 import lombok.AllArgsConstructor;
 import org.apache.commons.net.ftp.FTP;
 import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPReply;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -25,6 +29,9 @@ import org.springframework.web.bind.annotation.ModelAttribute;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 /**
  * 微信-更名过户申请
@@ -33,13 +40,26 @@ import java.util.Arrays;
  * @date Thu Jul 06 00:00:00 CST 2023
  */
 @RestController
-@AllArgsConstructor
+/*@AllArgsConstructor*/
 @RequestMapping("/api/wechatofficalacctmgt/wxghsq")
 @Api(tags="微信-更名过户申请接口")
 public class WxGhsqController {
 
-    private final  WxGhsqService wxGhsqService;
 
+
+   // private final  WxGhsqService wxGhsqService;
+@Autowired
+private  WxGhsqService wxGhsqService;
+
+
+    @Value("${ftp.server}")
+    private String server;
+    @Value("${ftp.port}")
+    private int port;
+    @Value("${ftp.userName}")
+    private String userName;
+    @Value("${ftp.password}")
+    private String password;
     /**
     * 分页查询
     * @param page 分页对象
@@ -54,6 +74,45 @@ public class WxGhsqController {
 
 
 
+    @PostMapping("/upload")
+    @ApiOperation(value = "附件上传")
+    public ResultRespone uploadFile(@RequestParam("file") MultipartFile file) {
+        FTPClient ftpClient = new FTPClient();
+        try {
+            ftpClient.setControlEncoding("UTF-8");
+            // 设置FTP服务器的连接信息
+            FtpConfig ftp=new FtpConfig();
+           // ftp.getPort();
+            ftpClient.connect("192.168.2.233", 21);
+            ftpClient.login("ftpuser", "ftpuser");
+
+            // 检查连接是否成功
+            int replyCode = ftpClient.getReplyCode();
+            if (!FTPReply.isPositiveCompletion(replyCode)) {
+                return ResultRespone.failed("FTP连接失败");
+            }
+
+            // 设置文件上传的路径
+            String remoteFilePath = "/ftppath";
+           String fileName = file.getOriginalFilename();
+            String remoteFileName = remoteFilePath + "/" + fileName;
+            // 上传文件到FTP服务器
+            InputStream inputStream = file.getInputStream();
+            ftpClient.storeFile(remoteFileName, inputStream);
+            inputStream.close();
+            // 关闭FTP连接
+            ftpClient.logout();
+            ftpClient.disconnect();
+            return ResultRespone.success("上传成功");
+        } catch (IOException e) {
+            e.printStackTrace();
+            return ResultRespone.failed("上传失败");
+        }
+
+    }
+
+
+
     /**
      * 通过id查询微信-更名过户申请
      * @param id id

+ 6 - 1
collect-fees/collect-fees-service/src/main/resources/application.yml

@@ -53,7 +53,12 @@ user:
   logo: /base/logo/
   account : /sys/widgets/UserManagement
   logFile : /base/logFile
-
+ftp:
+  enable: true
+  server: 192.168.2.233
+  port: 21
+  userName: ftpuser
+  password: ftpuser
 api:
   doc:
     title: 营收系统相关业务接口    #API文档标题

+ 6 - 1
collect-fees/collect-fees-service/src/main/resources/bootstrap.yml

@@ -1,4 +1,9 @@
 spring:
+#  http:
+#    encoding:
+#      charset: utf-8
+#      enabled: true
+#      force: true
   cloud:
     config:
       enabled: false
@@ -36,4 +41,4 @@ logging:
     com.alibaba.nacos.client.config.impl: WARN
 # DEV配置 为这个服务配置服务清单,可以是一个或N个
 tofly-base.ribbon.listOfServers: http://localhost:10102
-tofly-auth.ribbon.listOfServers: http://localhost:10101
+tofly-auth.ribbon.listOfServers: http://localhost:10101

+ 2 - 0
hnls-admin/hnls-admin-service/src/main/java/com/tofly/base/controller/UserController.java

@@ -67,6 +67,8 @@ import java.util.stream.Collectors;
 @Slf4j
 public class UserController {
 
+    @Value("${user.logo}")
+    private String avatarPath1;
     @Value("${user.avatar}")
     private String avatarPath;
     @Value("${xrty.ftp.excelmb}")