Java实现文件远程上传和下载

来源:互联网 发布:fifaaddict数据库 编辑:程序博客网 时间:2024/05/22 05:35


/** Created on 2009-9-14* Copyright 2009 by www.xfok.net. All Rights Reserved**/package net.xfok.ftp;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Properties;import java.util.Vector;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;import com.jcraft.jsch.SftpException;/*** @author YangHua* 转载请注明出处:http://www.xfok.net/2009/10/124485.html*/public class MySFTP {/*** 连接sftp服务器* @param host 主机* @param port 端口* @param username 用户名* @param password 密码* @return*/public ChannelSftp connect(String host, int port, String username,String password) {ChannelSftp sftp = null;try {JSch jsch = new JSch();jsch.getSession(username, host, port);Session sshSession = jsch.getSession(username, host, port);System.out.println("Session created.");sshSession.setPassword(password);Properties sshConfig = new Properties();sshConfig.put("StrictHostKeyChecking", "no");sshSession.setConfig(sshConfig);sshSession.connect();System.out.println("Session connected.");System.out.println("Opening Channel.");Channel channel = sshSession.openChannel("sftp");channel.connect();sftp = (ChannelSftp) channel;System.out.println("Connected to " + host + ".");} catch (Exception e) {}return sftp;}/*** 上传文件* @param directory 上传的目录* @param uploadFile 要上传的文件* @param sftp*/public void upload(String directory, String uploadFile, ChannelSftp sftp) {try {sftp.cd(directory);File file=new File(uploadFile);sftp.put(new FileInputStream(file), file.getName());} catch (Exception e) {e.printStackTrace();}}/*** 下载文件* @param directory 下载目录* @param downloadFile 下载的文件* @param saveFile 存在本地的路径* @param sftp*/public void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {try {sftp.cd(directory);File file=new File(saveFile);sftp.get(downloadFile, new FileOutputStream(file));} catch (Exception e) {e.printStackTrace();}}/*** 删除文件* @param directory 要删除文件所在目录* @param deleteFile 要删除的文件* @param sftp*/public void delete(String directory, String deleteFile, ChannelSftp sftp) {try {sftp.cd(directory);sftp.rm(deleteFile);} catch (Exception e) {e.printStackTrace();}}/*** 列出目录下的文件* @param directory 要列出的目录* @param sftp* @return* @throws SftpException*/public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException{return sftp.ls(directory);}public static void main(String[] args) {MySFTP sf = new MySFTP(); String host = "192.168.0.1";int port = 22;String username = "root";String password = "root";String directory = "/home/httpd/test/";String uploadFile = "D:\\tmp\\upload.txt";String downloadFile = "upload.txt";String saveFile = "D:\\tmp\\download.txt";String deleteFile = "delete.txt";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{sftp.cd(directory);sftp.mkdir("ss");System.out.println("finished");}catch(Exception e){e.printStackTrace();} } }






转自:http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888384.html

参考:http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 锅的把手松了怎么办 奶锅把手松了怎么办 锅的手柄烧坏了怎么办 鞋子上的铆钉生锈了怎么办 包包上的铆钉生锈了怎么办 凉鞋的铆钉生锈了怎么办 扇子的铆钉坏了怎么办 包包的铆钉坏了怎么办 汽车半轴螺丝母拧不动怎么办? 卫衣袖子短了怎么办 u型导轨蚊帐下垂怎么办 100的水管螺纹出漏水怎么办 吊顶螺丝没有防锈处理怎么办 膨胀螺丝洞松了怎么办 膨胀螺丝眼大了怎么办 墙上螺丝孔大了怎么办 膨胀螺丝孔深了怎么办 克霉膨胀栓的线怎么办 摩托车排气管螺丝断了怎么办 汽车轮胎螺丝卸不下来怎么办 内六角螺丝卸不下来怎么办 洗衣机六角螺丝卸不动怎么办 黄油嘴打不进去怎么办 螺杆冷水机氟系统有空气怎么办 脚踏式加油枪皮碗不下去怎么办? 自攻螺丝滑丝怎么办? 大工打小工老板不管怎么办 虾缸的过滤吸虾怎么办 加热棒坏了鱼怎么办 钢材软打孔断钻头怎么办 空调余额下水管检查口按不上怎么办 风机盘管噪音大怎么办 混凝土水泥放少了怎么办 门式钢梁端板连接下料短啦怎么办? 灌桩导管堵了怎么办 公路车尾钩歪了怎么办 铃木羚羊车大灯不亮怎么办 玻璃瓶打碎了里面食物怎么办 玻璃门上轴坏了怎么办 配筋面积小了怎么办 ps大文件存不了怎么办