Explorar o código

优化图片上传

夏文涛 hai 7 meses
pai
achega
336e4186d4

+ 1 - 0
pom.xml

@@ -109,6 +109,7 @@
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
+            <version>8.0.25</version>
         </dependency>
         <!-- lombok -->
         <dependency>

+ 8 - 2
src/main/java/com/template/controller/WelcomeFileController.java

@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.InputStream;
+import java.util.Properties;
 
 /**
  * <p>
@@ -51,7 +52,9 @@ public class WelcomeFileController implements WelcomeFileControllerAPI {
         try {
             session = jsch.getSession(user, host, port);
             session.setPassword(password);
-            session.setConfig("StrictHostKeyChecking", "no");
+            Properties config = new Properties();
+            config.put("StrictHostKeyChecking", "no");
+            session.setConfig(config);
             session.connect(30000);
             System.out.println("连接成功");
             channelSftp = (ChannelSftp) session.openChannel("sftp");
@@ -72,6 +75,7 @@ public class WelcomeFileController implements WelcomeFileControllerAPI {
             channelSftp.put(inputStream, name);
             //  从远程服务器上下载文件
             //channelSftp.get(downpath, spath);
+            result.setUrl("https://chtech.ncjti.edu.cn/welcome/homeimage/" + name);
         } catch (JSchException e) {
             e.printStackTrace();
         } finally {
@@ -81,7 +85,9 @@ public class WelcomeFileController implements WelcomeFileControllerAPI {
             if (session != null) {
                 session.disconnect();
             }
-            result.setUrl("https://chtech.ncjti.edu.cn/welcome/homeimage/" + name);
+        }
+        if(!org.springframework.util.StringUtils.hasText(result.getUrl())){
+            return CommonResult.fail("图片上传失败");
         }
         return CommonResult.ok(result);
     }