Java使用SFTP上傳文件到服務(wù)器的簡(jiǎn)單使用
最近用到SFTP上傳文件查找了一些資料后自己做了一點(diǎn)總結(jié),方便以后的查詢。具體代碼如下所示:
/** * 將文件上傳到服務(wù)器 * * @param filePath * 文件路徑 * @param channelSftp * channelSftp對(duì)象 * @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; } } catch (Exception e) { e.printStackTrace(); } finally { 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的端口號(hào) String ftpUserName = ""; //用戶名 String ftpPassword = ""; //鏈接的密碼 String privateKey = ""; // String passphrase = ""; if (port != null && !port.equals("")) { ftpPort = Integer.valueOf(port); } JSch jsch = new JSch(); // 創(chuàng)建JSch對(duì)象 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); // 根據(jù)用戶名,主機(jī)ip,端口獲取一個(gè)Session對(duì)象 if (StringUtils.isNotBlank(ftpPassword)) { session.setPassword(ftpPassword); // 設(shè)置密碼 } return session; } /** * 獲取ChannelSftp鏈接 * * @param timeout * 超時(shí)時(shí)間 * @return 返回ChannelSftp對(duì)象 * @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對(duì)象設(shè)置properties session.setTimeout(timeout); // 設(shè)置timeout時(shí)間 session.connect(); // 通過(guò)Session建立鏈接 channel = session.openChannel("sftp"); // 打開(kāi)SFTP通道 channel.connect(); // 建立SFTP通道的連接 return (ChannelSftp) channel; } /** * 斷開(kāi)sftp鏈接 * * @param session * 會(huì)話 * @param channel * 通道 */ public static void closeConnection(Channel channel, Session session) { try { if (session != null) { session.disconnect(); //關(guān)閉session鏈接 } if (channel != null) { channel.disconnect(); //斷開(kāi)連接 } } catch (Exception e) { e.printStackTrace(); } }
這里的用戶名密碼都是自己設(shè)置,這里的方法進(jìn)行了簡(jiǎn)單的封裝,方便使用。
以上所述是小編給大家介紹的Java使用SFTP上傳文件到服務(wù)器的簡(jiǎn)單使用,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java用單向環(huán)形鏈表來(lái)解決約瑟夫環(huán)Josepfu問(wèn)題
如果把單鏈表的最后一個(gè)節(jié)點(diǎn)的指針指向鏈表頭部,而不是指向NULL,那么就構(gòu)成了一個(gè)單向循環(huán)鏈表,通俗講就是把尾節(jié)點(diǎn)的下一跳指向頭結(jié)點(diǎn)2021-10-10關(guān)于ObjectUtils.isEmpty()?和?null?的區(qū)別
這篇文章主要介紹了關(guān)于ObjectUtils.isEmpty()?和?null?的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java?從json提取數(shù)組并轉(zhuǎn)換為list的操作方法
這篇文章主要介紹了Java?從json提取出數(shù)組并轉(zhuǎn)換為list,使用getJSONArray()獲取到j(luò)sonarray后,再將jsonArray轉(zhuǎn)換為字符串,最后將字符串解析為L(zhǎng)ist列表,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10解決eclipse中console控制選項(xiàng)不見(jiàn)了的方法
eclipse是一款用于編譯java語(yǔ)言的程序,利用這款軟件我們可以制作很多有趣的小程序,也可以制作一些大型的軟件項(xiàng)目,有的用戶在使用eclipse的時(shí)候會(huì)遇到console消失的情況,所以本文給大家介紹了解決eclipse中console控制選項(xiàng)不見(jiàn)了的方法,需要的朋友可以參考下2024-03-03springboot跨域訪問(wèn)cros與@CrossOrigin注解詳析
這篇文章主要給大家介紹了關(guān)于springboot跨域訪問(wèn)cros與@CrossOrigin注解的相關(guān)資料,跨域是指不同域名之間相互訪問(wèn),它是瀏覽器的同源策略造成的,是瀏覽器對(duì)JavaScript施加的安全限制,需要的朋友可以參考下2023-12-12Java實(shí)現(xiàn)動(dòng)態(tài)代理的實(shí)例代碼
代理模式是常用的java設(shè)計(jì)模式,他的特征是代理類與委托類有同樣的接口,代理類主要負(fù)責(zé)為委托類預(yù)處理消息、過(guò)濾消息、把消息轉(zhuǎn)發(fā)給委托類,以及事后處理消息等,這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)動(dòng)態(tài)代理的相關(guān)資料,需要的朋友可以參考下2021-09-09Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動(dòng)刷新配置的教程
這篇文章主要介紹了Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動(dòng)刷新配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09如何使用mybatis-generator自動(dòng)生成代碼
這篇文章主要介紹了如何使用mybatis-generator自動(dòng)生成代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10