**spring
**apache
import org.apache.log4j.Logger; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import org.springframework.stereotype.Component; /** * Created by shierdie on 2017/9/26. */ @Component public class SFTPChannel { Session session = null; Channel channel = null; private static final Logger LOG = Logger.getLogger(SFTPChannel.class.getName()); /** * * @param ftpHost 服務器地址 * @param hostport 服務器端口 * @param ftpUserName 鏈接服務器用戶名 * @param ftpPassword 鏈接服務器密碼 * @param timeout 鏈接超時時間 * @return * @throws JSchException */ public ChannelSftp getChannel(String ftpHost,String hostport, String ftpUserName,String ftpPassword, int timeout) throws JSchException { int ftpPort = 22; if (hostport != null && !hostport.equals("")) { ftpPort = Integer.valueOf(hostport); } // 建立JSch對象 JSch jsch = new JSch(); // 根據用戶名,主機ip,端口獲取一個Session對象 session = jsch.getSession(ftpUserName, ftpHost, ftpPort); LOG.debug("Session created."); if (ftpPassword != null) { // 設置密碼 session.setPassword(ftpPassword); } Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); // 爲Session對象設置properties session.setConfig(config); // 設置timeout時間 session.setTimeout(timeout); // 經過Session創建連接 session.connect(); LOG.debug("Session connected."); LOG.debug("Opening Channel."); // 打開SFTP通道 channel = session.openChannel("sftp"); // 創建SFTP通道的鏈接 channel.connect(); LOG.debug("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName + ", returning: " + channel); return (ChannelSftp) channel; } public void closeChannel() throws Exception { if (channel != null) { channel.disconnect(); } if (session != null) { session.disconnect(); } } }
調用測試緩存
//上傳到服務器的路徑 String idcardFrontUrl = "/usr/local/"; InputStream src = file.getInputStream(); SFTPChannel channel = new SFTPChannel(); //獲取鏈接 ChannelSftp chSftp = channel.getChannel(127.0.0.1, 22, root, root, 60000); //上傳文件,這一步纔會上傳 //src:輸入流 //idcardFrontUrl:服務器的路徑 chSftp.put(src, idcardFrontUrl, ChannelSftp.OVERWRITE);
總體就這樣,拿過去就能用,須要注意路徑等問題 PS: 文件上傳時我遇到了405不支持post的問題,這個之後再寫服務器
=========================== 這部分是看別人說提升ftp文件上傳速度的辦法網絡
FTPClient ftp = new FTPClient(); ftp.connect("172.16.2.5", 21); ftp.login("aaa", "aaaa"); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ftp.changeWorkingDirectory("C:\\FtpPublic"); InputStream input = new ByteArrayInputStream(bytes); ftp.storeFile(filename,input);
這是優化以前的代碼 ,上傳幾百kb的文件都須要20秒,感受特別慢,最後在網上查了相關的資料,嘗試着對代碼進行了優化,session
主要是增長了 ftp.setBufferSize(1024*1024); 增大緩存區工具
其次是由於是網絡流 將輸入流 加上BufferedInputStream BufferedInputStream input = new BufferedInputStream( new ByteArrayInputStream(bytes));post
FTPClient ftp = new FTPClient(); ftp.connect("172.16.2.5", 21); ftp.login("aaa", "aaaa"); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); ftp.setBufferSize(1024*1024); ftp.changeWorkingDirectory("C:\\FtpPublic"); BufferedInputStream input = new BufferedInputStream(new ByteArrayInputStream(bytes)); ftp.storeFile(filename,input);