javaweb上傳檔案到伺服器中
採用的是stfp協議連線。package com.pzedu.infrastructure.common.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Vector; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; /** * sftp 工具類 * * @author weird */ public final class Sftps { private static final Logger log = LoggerFactory.getLogger(Sftps.class); private Session sshSession; private ChannelSftp sftp; /** * 連線sftp伺服器 * @param host * @param port * @param username * @param password * @return * @throws Exception */ public ChannelSftp connect(String host, int port, String username, String password) throws Exception { JSch jsch = new JSch(); sshSession = jsch.getSession(username, host, port); log.debug("Session created."); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); log.debug("Session connected."); log.debug("Opening Channel."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; log.debug("Connected to " + host + "."); return sftp; } /** * 連線sftp伺服器 * @param host * @param port * @param username * @param privateKey * @param passphrase * @return * @throws Exception */ public ChannelSftp connect(String host, int port, String username, String privateKey ,String passphrase) throws Exception { JSch jsch = new JSch(); //設定金鑰和密碼 if (!StringUtils.isEmpty(privateKey)) { if (!StringUtils.isEmpty(passphrase)) { //設定帶口令的金鑰 jsch.addIdentity(privateKey, passphrase); } else { //設定不帶口令的金鑰 jsch.addIdentity(privateKey); } } sshSession = jsch.getSession(username, host, port); log.debug("Session created."); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); log.debug("Session connected."); log.debug("Opening Channel."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; log.debug("Connected to " + host + "."); return sftp; } public void portForwardingL(int lport, String rhost, int rport) throws Exception { int assinged_port = sshSession.setPortForwardingL(lport, rhost, rport); System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport); } /** * 斷開連線 */ public void disconnect() { if (sftp != null) sftp.disconnect(); if (sshSession != null) sshSession.disconnect(); } /** * 上傳檔案 * * @param directory * 上傳的目錄 * @param uploadFile * 要上傳的檔案 * @param sftp */ public void upload(String directory, String uploadFile) throws Exception { sftp.cd(directory); File file = new File(uploadFile); sftp.put(new FileInputStream(file), file.getName()); } public void upload(String directory, File file) throws Exception { sftp.cd(directory); sftp.put(new FileInputStream(file), file.getName()); System.out.println("upload file "+file.getAbsolutePath() + " to host " + sshSession.getHost()); } public void uploadDir(File src, String dst) throws Exception { if (!exist(dst)) { sftp.mkdir(dst); } if (src.isFile()) { upload(dst, src); } else { for (File file : src.listFiles()) { if (file.isDirectory()) { uploadDir(file, dst + "/" + file.getName()); } upload(dst, file); } } } /** * 目錄是否查詢 * @param path * @return * @throws SftpException */ public boolean exist(String path) throws SftpException { String pwd = sftp.pwd(); try { sftp.cd(path); } catch (SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { return false; } else { throw e; } } finally { sftp.cd(pwd); } return true; } /** * 下載檔案 * @param directory * @param downloadFile * @param saveFile * @throws Exception */ public void download(String directory, String downloadFile, String saveFile) throws Exception { sftp.cd(directory); File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } /** * 下載檔案 * @param directory * @param downloadFile * @param saveFile * @throws Exception */ public void download(String directory, String downloadFile, File saveFile) throws Exception { sftp.cd(directory); sftp.get(downloadFile, new FileOutputStream(saveFile)); System.out.println("download file "+directory + "/" +downloadFile + " from host " + sshSession.getHost()); } /** * 下載檔案 * @param src * @param dst * @throws Exception */ @SuppressWarnings("unchecked") public void downloadDir(String src, File dst) throws Exception { try { sftp.cd(src); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } dst.mkdirs(); Vector<LsEntry> files = sftp.ls(src); for (LsEntry lsEntry : files) { if (lsEntry.getFilename().equals(".") || lsEntry.getFilename().equals("..")) { continue; } if (lsEntry.getLongname().startsWith("d")) { downloadDir(src + "/" + lsEntry.getFilename(), new File(dst, lsEntry.getFilename())); } else { download(src, lsEntry.getFilename(), new File(dst, lsEntry.getFilename())); } } } /** * 刪除檔案 * @param directory * @param deleteFile * @throws SftpException */ public void delete(String directory, String deleteFile) throws SftpException { sftp.cd(directory); sftp.rm(deleteFile); } /** * 列出目錄下的檔案 * @param directory * @return * @throws SftpException */ public Vector listFiles(String directory) throws SftpException { return sftp.ls(directory); } public static void main(String[] args) throws Exception { Sftps sf = new Sftps(); String host = "192.168.56.101"; int port = 22; String username = "root"; String password = "123"; String privateKey = "C:/Users/Administrator/Desktop/seRT_SSH_key/front/Identity"; String passphrase = "h$VgBrx3nhwH#2!h6zs0uGhzcCX8dTPa"; String src = "/usr/local/apache-tomcat-7.0.63/webapps/pzedu"; sf.connect(host, port, username, privateKey, passphrase); sf.portForwardingL(1194, "10.169.97.248", 1194); // ChannelSftp sftp = sf.connect(host, port, username, password); // sf.upload(directory, uploadFile, sftp); // sf.download(directory, downloadFile, saveFile, sftp); // sf.delete(directory, deleteFile, sftp); // try { //// Vector files = sf.listFiles("/root"); // sf.downloadDir(src, new File("C:/staticsfile")); //// sf.uploadDir(new File("C:/staticsfile"), "/root/temp"); // System.out.println("finished"); // sftp.disconnect(); // } catch (Exception e) { // e.printStackTrace(); // } // sf.disconnect(); } public Session getSshSession() { return sshSession; } public ChannelSftp getSftp() { return sftp; } }
相關推薦
javaweb上傳檔案到伺服器中
package com.pzedu.infrastructure.common.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Prope
Javaweb----上傳檔案,excle表格錄入資料庫的表中
上傳檔案,excle表格錄入資料庫的表中 一開始遙望資料庫中錄入測試資料,但由於要做考試系統,所以用到了一個東西,那就是能不能把表格裡的東西錄入到資料庫中呢?經過仔細的研究,終於找到一種方式了。下面來
javaWeb上傳檔案(jsp上傳檔案)
jsp頁面: 使用者名稱: 檔案: 後臺servlet頁面 package com.upload.servlet; import java.io.File; import java.io.IOException; i
第10步 (1)logback.xml日誌配置(2) ftp(上傳檔案)伺服器配置(3) idea注入和自動編譯配置(4)專案提交gitee(5)fe助手和restlet client
****************************************************************************************************************************************** 1.直接複製&
上傳檔案伺服器搭建
有需求需要搭建一個上傳檔案伺服器,最開始考慮使用Nginx+upload模組,後臺php處理,後來嘗試了一下tomcat+spring方法,都可以滿足需求。 Nginx + upload module 網上的配置文章比較多,這裡只是記錄一下出現的坑吧。
tp5上傳至伺服器中出現各類錯誤的解決方法
1.連線Mysql提示1045 access denied for user 'root'@'localhost' using password yes的解決方法檢查上傳至伺服器中的tp5中的database.php中的連線資料庫的密碼是否正確2.連線Mysql提示Can’t
java httpclient 上傳檔案 伺服器 servlet 接收
1、新建一個普通的java專案,然後新建個包和類,程式碼如下: package com.busymonkey; import java.io.File; import java.io.IOException; import org.apache.http
利用SFTP將本地的整個資料夾上傳到伺服器中
情景:利用sftp中將本地資料夾/home/sentiment/Data (內包含子資料夾) ,複製到伺服器的/home/work/Demo目錄下 命令: put 步驟: 1.首先定位到遠端/home/work/目錄下: cd /home/work/ 2.在word
不使用ASP.NET中的伺服器控制元件將如何上傳檔案?
遇到檔案的上傳時,可能會有大部分的開發者喜歡使用伺服器控制元件,雖然很方便,但是卻不能很好的控制,不具靈活性。 現給出例子,使用html標籤語言靈活的控制檔案的上傳。 1、html部分 <input type="file" id="uploadFile" name="uploa
阿里雲輕量應用伺服器 怎麼控制怎麼上傳檔案怎麼安裝JDK和Tomcat完成JavaWeb的部署
你是否遇到過這些問題,自己的javaweb專案本地執行一切正常,但是一轉移到阿里伺服器之類的.就出現以下問題. 1 jsp無法解析java類 2 Only a type can be imported.
在Android瀏覽器中通過WebView呼叫相機拍照/選擇檔案 上傳到伺服器
最近做的一個專案中,有這樣一個要求,在瀏覽器中呼叫系統的拍照功能或者選擇檔案,然後將檔案上傳到伺服器,類似修改頭像。 簡單而言,就是在一個html頁面中有這樣一段程式碼 <input class="filePrew" type="file" capture=
Javaweb 檔案上傳到伺服器
在我們的Java web開發中,很多情況下要向伺服器上傳檔案,那麼今天我們就來學習以下如何向伺服器傳送檔案: 前期準備: jar包:向伺服器上傳檔案需要用到一些jar包,這裡用的是Apache的包commons-fileupload和commons-io 沒有這兩個jar包的
使用Xftp上傳檔案到VirtualBox中的Centos7伺服器
一、windows上安裝Xftp 5 二、Centos7上安裝Xftp服務 檢查是否安裝了Xftp服務:rpm -qa|grep vsftpd 出現如上提示,則表示已安裝,如果沒有出現則需要
Javaweb上傳圖片或檔案到伺服器,Tomcat伺服器重新部署專案,會刪除原來上傳的檔案的問題
解決辦法!!! 你按這幾個步驟去做,就能解決問題: 1.把/tomcat/webapps/目錄清空,刪掉該資料夾的所有內容, 2.修改/tomcat/conf/web.xml檔案: <servlet> <servle
在WinForm中通過HTTP協議向伺服器端上傳檔案
相信用ASP.NET寫一個上傳檔案的網頁,大家都會寫,但是有沒有人想過通過在WinForm中通過HTTP協議上傳檔案呢? 有些人說要向伺服器端上傳檔案,用FTP協議不是很簡單嗎?效率又高,為什麼還要使用HTTP協議那麼麻煩呢?這裡面有幾個原因: (1)FTP伺服器的部署相對麻
javaweb專案將檔案上傳至資料庫中的大小限制
首先將struts.xml中新增上配置 <!-- 配置檔案上傳大小 --><constant name="struts.multipart.maxSize" value="671088640"></constant>64M 然後上傳大檔案出現: Struts Prob
php中對上傳到伺服器的檔案重新命名
原始碼:$path = './upfiles/'. $_FILES['file']['name'];$filename = basename($path);// basename($path):返回上傳檔案的基本的檔名,如:檔名.doc $actualname = $fil
Qt Http QHttpMultiPart上傳檔案到java http伺服器
Qt Http QHttpMultiPart上傳檔案到java http伺服器 1.最近專案用到了Qt上傳檔案到HT
Tomcat配置虛擬路徑,使上傳檔案與伺服器及工程檔案分離開
原文連結:https://my.oschina.net/pingdy/blog/381001 摘要: 現在一般的專案都可能會涉及檔案的上傳與下載,那如何管理這些檔案呢?做法各有千秋!今天我也分享下我是怎麼去管理這些檔案的!當然了,這樣的方式只適用於檔案量小的情況下!如果檔案數量非
原生node用fs和http完成檔案上傳到伺服器
var http=require("http"); var fs=require("fs"); http.createServer(function(req,res){ res.writeHead(200,{"Content-type":"text/html;charset=UTF-8","