FTP上传下载文件,利用FTP传输文件到指定目录
2013-07-31 10:34
691 查看
package com.xx.util; /** * <p> * Title: FtpUtil * </p> * <p> * Description FTP上传文件类,利用FTP协议传输文件到指定目录 * </p> * <p> */ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import sun.net.TelnetInputStream; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; public class FtpUtil { private FtpClient ftpClient; // 连接ftp服务器 public void connectServer(String server, String user, String password, String path) throws IOException { // server:FTP服务器的IP地址;user:登录FTP服务器的用户名 // password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径 ftpClient = new FtpClient(); ftpClient.openServer(server); ftpClient.login(user, password); // path是ftp服务下主目录的子目录 if (path.length() != 0) ftpClient.cd(path); // 用2进制上传 ftpClient.binary(); } // 上传文件;并返回上传文件的信息 public String upload(String oldFileName,String newFileName) throws Exception { TelnetOutputStream os = null; FileInputStream is = null; try { // "upftpfile"用ftp上传后的新文件名 os = ftpClient.put(newFileName); java.io.File file_in = new java.io.File(oldFileName); if (file_in.length() == 0) { return "上传文件为空!"; } is = new FileInputStream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } } finally { if (is != null) { is.close(); } if (os != null) { os.close(); } } return "上传文件成功!"; } public String upload(byte [] by, String newFileName) throws Exception { TelnetOutputStream os =null; try{ if(by.length==0){ return "上传文件为空!"; } os = ftpClient.put(newFileName); os.write(by); }catch(Exception e){ e.printStackTrace(); }finally{ if (os != null) { os.close(); } } return "上传文件成功!"; } public boolean uploadftp(byte [] by, String newFileName) throws Exception { boolean res=false; TelnetOutputStream os =null; try{ if(by.length==0){ return res; } os = ftpClient.put(newFileName); os.write(by); res=true; }catch(Exception e){ e.printStackTrace(); }finally{ if (os != null) { os.close(); } } return res; } /** * 从FTP将文件下载到本地 * @param filename * @param newfilename * @return * @throws Exception */ public long download(String filename, String newfilename) throws Exception { long result = 0; TelnetInputStream is = null; FileOutputStream os = null; try { is = ftpClient.get(filename); java.io.File outfile = new java.io.File(newfilename); os = new FileOutputStream(outfile); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); result = result + c; } } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { is.close(); } if (os != null) { os.close(); } } return result; } public static void main(String args[]) { FtpUtil ftputil = new FtpUtil(); try { // 连接ftp服务器 ftputil.connectServer("xxx.xxx.xxx.xxx", "sxx", "xxx", "/xxx/sxdoc/user_projects/domains/Data/"); // 上传文件;并返回上传文件的信息 // ftputil.upload("c:/test.txt","test.txt"); String str=""; byte [] by=str.getBytes(); ftputil.upload(by,"test.txt"); } catch (Exception e) { System.out.println(e.toString()); return; } finally { try { if (ftputil.ftpClient != null) { ftputil.ftpClient.closeServer(); } } catch (Exception e) { e.printStackTrace(); } } } public FtpClient getFtpClient() { return ftpClient; } public void setFtpClient(FtpClient ftpClient) { this.ftpClient = ftpClient; } }
相关文章推荐
- 利用FtpClient实现上传下载及获得文件目录
- Delphi7 中利用Indy的TidFtp客户端控件实现在服务器指定目录下下载需要的文件
- 探究ftp的奥妙之系统用户如何上传下载文件、vsftpd如何识别证书并利用客户端来实现远程连接传输
- 利用WinInet库和STL获取计算机硬盘中的文件并上传到指定FTP服务器
- 利用FTPClient类实现文件的上传下载功能
- 利用Socket 实现 FTP 文件上传和文件下载
- python 实现统计ftp服务器指定目录下文件夹数目、文件数目及所有文件大小 本次主要为满足应用方核对上传到ftp服务器的文件是否缺漏。 主要要求:指定目录下,文件夹数目/文件数目/所有文件大小
- java实现ftp连接、登陆、上传、下载、删除文件、获取目录、文件列表
- Linux 利用shell中使用ftp自动下载上传文件
- Python--利用ftplib自动上传/下载文件
- 利用 Spring MVC 上传多文件到指定目录 spring upload files
- FTPClient--FTP操作帮助类,上传下载,文件,目录操作
- C# 利用FTP自动下载xml文件后利用 FileSystemWatcher 监控目录下文件变化并自动更新数据库
- net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- 使用 wget 从 ftp上下载文件到指定目录
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名 .
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表...
- java通过ftp上传、下载文件,遍历文件目录
- C#实现Ftp各种操作(上传,下载,删除文件,创建目录,删除目录,获得文件列表)
- Java 利用Apache Commons Net 实现 FTP文件上传下载