English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
最近、SFTPでファイルアップロードのために資料を調べて、自分で少しまとめました。今後の参照のために便利です。具体的なコードは以下の通りです:
/** * ファイルをサーバーにアップロードします * * @param filePath * ファイルパス * @param channelSftp * channelSftpオブジェクト * @return */ public static boolean uploadFile(String filePath, ChannelSftp channelSftp) { OutputStream outstream = null; InputStream instream = null; boolean successFlag = false; try { File isfile = new File(filePath); if (isfile.isFile()) { outstream = channelSftp.put(isfile.getName()); File file = new File(filePath); if (file.exists()) { instream = new FileInputStream(file); byte b[] = new byte[1024]; int n; while ((n = instream.read(b)) != -1) { outstream.write(b, 0, n); } outstream.flush(); } successFlag = true; } } e.printStackTrace(); } try { if (instream != null) { instream.close(); } if (outstream != null) { outstream.close(); } } catch (IOException e) { e.printStackTrace(); } } return successFlag; } private static Session initJschSession() throws JSchException { int ftpPort = 0; String ftpHost = ""; String port = "00"; //sftpのポート番号 String ftpUserName = ""; //ユーザー名 String ftpPassword = ""; //リンクのパスワード String privateKey = ""; // String passphrase = ""; if (port != null && !port.equals("")) { ftpPort = Integer.valueOf(port); } JSch jsch = new JSch(); // JSchオブジェクトの作成 if (StringUtils.isNotBlank(privateKey) && StringUtils.isNotBlank(passphrase)) { jsch.addIdentity(privateKey, passphrase); } if (StringUtils.isNotBlank(privateKey) && StringUtils.isBlank(passphrase)) { jsch.addIdentity(privateKey); } jsch.getSession(ftpUserName, ftpHost, ftpPort); Session session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // ユーザー名、ホストIP、ポートからSessionオブジェクトを取得 if (StringUtils.isNotBlank(ftpPassword)) { session.setPassword(ftpPassword); // パスワードの設定 } return session; } /** * ChannelSftpリンクの取得 * * @param timeout * 超時時間 * @return ChannelSftpオブジェクトを返します * @throws JSchException */ public static ChannelSftp getChannelSftp(Session session, int timeout) throws JSchException { Channel channel = null; Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); // Sessionオブジェクトにpropertiesを設定 session.setTimeout(timeout); // timeout時間の設定 session.connect(); // セッションを通じてリンクを確立します channel = session.openChannel("sftp"); // SFTPチャネルを開きます channel.connect(); // SFTPチャネルの接続を確立します return (ChannelSftp) channel; } /** * SFTPリンクを切断します * * @param session * セッション * @param channel * チャネル */ public static void closeConnection(Channel channel, Session session) { try { if (session != null) { session.disconnect(); //セッションリンクを閉じます } if (channel != null) { channel.disconnect(); //接続を切ります } } e.printStackTrace(); } }
ここに表示されるユーザー名とパスワードはすべて自分で設定されています。この方法は簡単に封装されていますので、使用が便利です。
以上の内容は編集者が皆さんに紹介したJavaでSFTPを使用してサーバーにファイルをアップロードする簡単な方法です。皆さんの助けになれば幸いです。何かご不明な点がございましたら、コメントを残してください。編集者は迅速に回答いたします。また、呐喊教程サイトへのサポートに感謝しています!
声明:本文の内容はインターネットから取得しており、著作権者に帰属します。インターネットユーザーにより自発的に貢献し、自己でアップロードされています。本サイトは所有権を持ちません。人工編集は行われていません。また、関連する法的責任を負いません。著作権侵害が疑われる内容を見つけた場合は、メールでnotice#wにご連絡ください。3codebox.com(メールを送信する際、#を@に置き換えてください。報告を行い、関連する証拠を提供してください。一旦確認が取れましたら、本サイトは即座に侵害される内容を削除します。)