**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);