Java如何实现上传文件到服务器指定目录

作者:yaominghui 时间:2021-10-16 14:38:31 

前言需求

使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。

具体实现

引入的pom


<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>

<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>

建立实体类


public class ResultEntity {

private String code;

private String message;

private File file;

public ResultEntity(){}

public ResultEntity(String code, String message, File file) {
super();
this.code = code;
this.message = message;
this.file = file;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

}




public class ScpConnectEntity {
 private String userName;
 private String passWord;
 private String url;
 private String targetPath;

public String getTargetPath() {
   return targetPath;
 }

public void setTargetPath(String targetPath) {
   this.targetPath = targetPath;
 }

public String getUserName() {
   return userName;
 }

public void setUserName(String userName) {
   this.userName = userName;
 }

public String getPassWord() {
   return passWord;
 }

public void setPassWord(String passWord) {
   this.passWord = passWord;
 }

public String getUrl() {
   return url;
 }

public void setUrl(String url) {
   this.url = url;
 }

}

建立文件上传工具类

@Configuration


@Configuration
public class FileUploadUtil {

@Value("${remoteServer.url}")
 private String url;

@Value("${remoteServer.password}")
 private String passWord;

@Value("${remoteServer.username}")
 private String userName;

@Async
 public ResultEntity uploadFile(File file, String targetPath, String remoteFileName) throws Exception{
   ScpConnectEntity scpConnectEntity=new ScpConnectEntity();
   scpConnectEntity.setTargetPath(targetPath);
   scpConnectEntity.setUrl(url);
   scpConnectEntity.setPassWord(passWord);
   scpConnectEntity.setUserName(userName);

String code = null;
   String message = null;
   try {
     if (file == null || !file.exists()) {
       throw new IllegalArgumentException("请确保上传文件不为空且存在!");
     }
     if(remoteFileName==null || "".equals(remoteFileName.trim())){
       throw new IllegalArgumentException("远程服务器新建文件名不能为空!");
     }
     remoteUploadFile(scpConnectEntity, file, remoteFileName);
     code = "ok";
     message = remoteFileName;
   } catch (IllegalArgumentException e) {
     code = "Exception";
     message = e.getMessage();
   } catch (JSchException e) {
     code = "Exception";
     message = e.getMessage();
   } catch (IOException e) {
     code = "Exception";
     message = e.getMessage();
   } catch (Exception e) {
     throw e;
   } catch (Error e) {
     code = "Error";
     message = e.getMessage();
   }
   return new ResultEntity(code, message, null);
 }

private void remoteUploadFile(ScpConnectEntity scpConnectEntity, File file,
                String remoteFileName) throws JSchException, IOException {

Connection connection = null;
   ch.ethz.ssh2.Session session = null;
   SCPOutputStream scpo = null;
   FileInputStream fis = null;

try {
     createDir(scpConnectEntity);
   }catch (JSchException e) {
     throw e;
   }

try {
     connection = new Connection(scpConnectEntity.getUrl());
     connection.connect();

if(!connection.authenticateWithPassword(scpConnectEntity.getUserName(),scpConnectEntity.getPassWord())){
       throw new RuntimeException("SSH连接服务器失败");
     }
     session = connection.openSession();

SCPClient scpClient = connection.createSCPClient();

scpo = scpClient.put(remoteFileName, file.length(), scpConnectEntity.getTargetPath(), "0666");
     fis = new FileInputStream(file);

byte[] buf = new byte[1024];
     int hasMore = fis.read(buf);

while(hasMore != -1){
       scpo.write(buf);
       hasMore = fis.read(buf);
     }
   } catch (IOException e) {
     throw new IOException("SSH上传文件至服务器出错"+e.getMessage());
   }finally {
     if(null != fis){
       try {
         fis.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     if(null != scpo){
       try {
         scpo.flush();
//          scpo.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
     if(null != session){
       session.close();
     }
     if(null != connection){
       connection.close();
     }
   }
 }

private boolean createDir(ScpConnectEntity scpConnectEntity ) throws JSchException {

JSch jsch = new JSch();
   com.jcraft.jsch.Session sshSession = null;
   Channel channel= null;
   try {
     sshSession = jsch.getSession(scpConnectEntity.getUserName(), scpConnectEntity.getUrl(), 22);
     sshSession.setPassword(scpConnectEntity.getPassWord());
     sshSession.setConfig("StrictHostKeyChecking", "no");
     sshSession.connect();
     channel = sshSession.openChannel("sftp");
     channel.connect();
   } catch (JSchException e) {
     e.printStackTrace();
     throw new JSchException("SFTP连接服务器失败"+e.getMessage());
   }
   ChannelSftp channelSftp=(ChannelSftp) channel;
   if (isDirExist(scpConnectEntity.getTargetPath(),channelSftp)) {
     channel.disconnect();
     channelSftp.disconnect();
     sshSession.disconnect();
     return true;
   }else {
     String pathArry[] = scpConnectEntity.getTargetPath().split("/");
     StringBuffer filePath=new StringBuffer("/");
     for (String path : pathArry) {
       if (path.equals("")) {
         continue;
       }
       filePath.append(path + "/");
       try {
         if (isDirExist(filePath.toString(),channelSftp)) {
           channelSftp.cd(filePath.toString());
         } else {
           // 建立目录
           channelSftp.mkdir(filePath.toString());
           // 进入并设置为当前目录
           channelSftp.cd(filePath.toString());
         }
       } catch (SftpException e) {
         e.printStackTrace();
         throw new JSchException("SFTP无法正常操作服务器"+e.getMessage());
       }
     }
   }
   channel.disconnect();
   channelSftp.disconnect();
   sshSession.disconnect();
   return true;
 }

private boolean isDirExist(String directory,ChannelSftp channelSftp) {
   boolean isDirExistFlag = false;
   try {
     SftpATTRS sftpATTRS = channelSftp.lstat(directory);
     isDirExistFlag = true;
     return sftpATTRS.isDir();
   } catch (Exception e) {
     if (e.getMessage().toLowerCase().equals("no such file")) {
       isDirExistFlag = false;
     }
   }
   return isDirExistFlag;
 }
}

属性我都写在Spring的配置文件里面了。将这个类托管给spring容器。

来源:https://www.cnblogs.com/jichi/p/12158537.html

标签:Java,上传,服务器
0
投稿

猜你喜欢

  • Java采用循环链表结构求解约瑟夫问题

    2022-07-27 16:35:59
  • Android亮屏和熄屏控制实例详解

    2023-10-12 06:57:04
  • Java流处理stream使用详解

    2021-09-17 18:27:58
  • Spring AOP实现功能权限校验功能的示例代码

    2021-10-18 21:32:29
  • SpringFramework应用接入Apollo配置中心过程解析

    2021-08-30 09:03:50
  • C#实现简单串口通信

    2023-05-14 08:15:27
  • ajaxFileUpload插件,C#返回Json数据报错问题的解决方案

    2022-10-22 14:26:55
  • springboot+springsecurity如何实现动态url细粒度权限认证

    2023-10-01 04:06:39
  • SpringbootJPA分页 PageRequest过时的替代方法

    2022-03-10 11:53:13
  • C#飞机打字游戏的代码示例(winform版)

    2021-09-10 17:43:17
  • Java中Synchronized的用法解析

    2023-07-28 22:40:51
  • 深入了解Java虚拟机栈以及内存模型

    2022-02-17 13:37:50
  • Android实现简易计算功能

    2022-01-10 20:42:59
  • Java8中forEach语句循环一个List和Map

    2021-06-02 07:03:54
  • Android 使用mediaplayer播放res/raw文件夹中的音乐的实例

    2023-12-19 08:28:11
  • java 读写文件[多种方法]

    2022-10-04 09:09:00
  • java FastJson的简单用法

    2022-07-07 23:51:47
  • vscode使用官方C/C++插件无法进行代码格式化问题

    2022-07-13 06:24:47
  • 开发工具EesyCode使用方法解析

    2023-06-27 13:50:46
  • C#检查指定对象是否存在于ArrayList集合中的方法

    2023-07-28 00:45:17
  • asp之家 软件编程 m.aspxhome.com