WelcomeFileController.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.template.controller;
  2. import com.jcraft.jsch.*;
  3. import com.template.api.WelcomeFileControllerAPI;
  4. import com.template.model.result.CommonResult;
  5. import com.template.model.vo.UploadFileVo;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.io.InputStream;
  9. import java.util.Properties;
  10. /**
  11. * <p>
  12. * 前端控制器
  13. * </p>
  14. *
  15. * @author ceshi
  16. * @since 2025-06-13
  17. */
  18. @RestController
  19. public class WelcomeFileController implements WelcomeFileControllerAPI {
  20. /**
  21. * 文件上传
  22. *
  23. * @param file 上传的文件
  24. * @return
  25. * @throws Exception
  26. */
  27. @Override
  28. public CommonResult uploadFile(MultipartFile file) throws Exception {
  29. UploadFileVo result = new UploadFileVo();
  30. String name = com.template.common.utils.StrUtils.getRandomName(file.getOriginalFilename());
  31. String user = "root"; //SFTP服务器用户名
  32. String password = "Waimai2024#"; // SFTP服务器密码
  33. String host = "172.16.40.126"; // SFTP服务器地址
  34. int port = 22; // SFTP 服务器端口
  35. String remoteDirPath = "/home/image"; // 远程目录路径
  36. //从MultipartFile对象中获取流
  37. InputStream inputStream = null;
  38. inputStream = file.getInputStream();
  39. //String downpath="/book/《Python爬虫开发与项目实战》.pdf";
  40. //String spath="C:/Users/F1339769/Desktop";
  41. JSch jsch = new JSch();
  42. Session session = null;
  43. ChannelSftp channelSftp = null;
  44. try {
  45. session = jsch.getSession(user, host, port);
  46. session.setPassword(password);
  47. Properties config = new Properties();
  48. config.put("StrictHostKeyChecking", "no");
  49. session.setConfig(config);
  50. session.connect(30000);
  51. System.out.println("连接成功");
  52. channelSftp = (ChannelSftp) session.openChannel("sftp");
  53. channelSftp.connect();
  54. // 创建远程目录
  55. String[] dirArr = remoteDirPath.split("/");
  56. String path = "";
  57. for (int i = 0; i < dirArr.length; i++) {
  58. path = path + "/" + dirArr[i];
  59. try {
  60. channelSftp.cd(path);
  61. } catch (SftpException e) {
  62. channelSftp.mkdir(path);
  63. channelSftp.cd(path);
  64. }
  65. }
  66. // 文件上传到远程服务器上
  67. channelSftp.put(inputStream, name);
  68. // 从远程服务器上下载文件
  69. //channelSftp.get(downpath, spath);
  70. result.setUrl("https://chtech.ncjti.edu.cn/welcome/homeimage/" + name);
  71. } catch (JSchException e) {
  72. e.printStackTrace();
  73. } finally {
  74. if (channelSftp != null) {
  75. channelSftp.disconnect();
  76. }
  77. if (session != null) {
  78. session.disconnect();
  79. }
  80. }
  81. if(!org.springframework.util.StringUtils.hasText(result.getUrl())){
  82. return CommonResult.fail("图片上传失败");
  83. }
  84. return CommonResult.ok(result);
  85. }
  86. }