1.使用sun.net.ftp.FtpClient
package org.jasig.cas.a4.tools; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.json.JSONObject; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; /** * Title : AuditLog4AService * <p/> * Description : 4A审计日志非实时接口 * <p/> * CopyRight : CopyRight (c) 2011 * <p/> * Company : 亚信联创科技(中国)有限公司 * <p/> * JDK Version Used : JDK 5.0 + * <p/> * Modification History : * <p/> * * <pre> * </pre> * */ public class Log4AService { /** * Description: 上传文件到FTP服务器 * * @param file * 上传文件 */ private static void uploadFile(File file) { String ftpServer = "10.1.252.96"; String ftpUser = "smc"; String ftpPwd = "smc"; int ftpPort = 21; // 连接FTP服务器 FtpClient ftpClient = new FtpClient(); try { ftpClient.openServer(ftpServer, ftpPort); ftpClient.login(ftpUser, ftpPwd); ftpClient.cd("smc_bak"); System.out.println("已登录到\"" + ftpClient.pwd() + "\"目录"); ftpClient.binary(); } catch (IOException e) { throw new RuntimeException("FTP server connection failed. " + e.getMessage(), e); } // 上传文件 TelnetOutputStream tos = null; FileInputStream fis = null; try { fis = new FileInputStream(file); String fileName = file.getName(); tos = ftpClient.put(fileName); byte[] bytes = new byte[1024]; int c; while ((c = fis.read(bytes)) != -1) { tos.write(bytes, 0, c); } } catch (FileNotFoundException e) { throw new RuntimeException("File is not exist. " + e.getMessage(), e); } catch (IOException e) { throw new RuntimeException("Error uploading files. " + e.getMessage(), e); } finally { try { if (null != tos) { tos.close(); } if (null != fis) { fis.close(); } } catch (IOException ex) { throw new RuntimeException( "An error occurred while the stream is closed. " + ex.getMessage(), ex); } } } public static void main(String[] args) { File file = new File("c:/temp/1.txt"); uploadFile(file ); } }
2.ftp4j 上传
package org.jasig.cas.a4.tools; import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPFile; import java.io.File; import java.io.InputStream; import org.apache.log4j.Logger; public class FTPUtil { private Logger logger = Logger.getLogger(FTPUtil.class); private FTPClient client = null; private String ipAddress = null; private Integer port = null; private String username = null; private String password = null; public Boolean connect() throws Exception{ Boolean bl = false; if(ipAddress!=null && port!=null && username!=null && password!=null){ client = new FTPClient(); client.setCharset("utf-8"); client.setType(FTPClient.TYPE_BINARY);//二进制 try { client.connect(ipAddress, port); client.login(username, password); bl = true; } catch (Exception e) { bl = false; logger.error("FTP connect failed!", e); throw new Exception("FTP connect failed!"); } } return bl; } public Boolean upload(File file) throws Exception{ Boolean result = false; if(file!=null && file.exists()){ try { // FTPUploadListener listener = new FTPUploadListener(); client.upload(file); } catch (Exception e) { logger.error("FTP upload failed!"+file.getAbsolutePath(), e); throw new Exception("FTP upload failed! fileName:"+file.getName()); } }else{ logger.info("file not exists!"); } return result; } public static void main(String[] args) throws Exception { FTPUtil ftp = new FTPUtil(); // ftp.init("10.1.253.117", 21, "bam", "bam"); ftp.init("10.1.252.96", 21, "smc", "smc"); File file = new File("c:/temp/1.txt"); System.out.println(file.exists()); ftp.connect(); ftp.upload(file); } }
相关推荐
很好用的ftp上传工具,上传速度也很快,很好用的ftp上传工具,上传速度也很快,很好用的ftp上传工具,上传速度也很快,
教你如何用FTP上传网页到虚拟主机 fileZilla LeapFTP
可以实现ftp上传图片文件到指定ftp目录php实现通过ftp上传图片文件,用户无需手动更改目录,上传时可以选择是否上传到ftp位置,可有本地跟ftp可选
C++ libcurl ftp上传文件
c# 实现文件FTP上传至服务器 WebClient上传文件至服务器(不带进度条) 要上传的文件(全路径格式) <param name="strUrlDirPath">Web服务器文件夹路径 <returns>True/False是否上传成功</returns>
go 实现ftp上传资源 并且ftp 可以自动创建文件夹,如有不会直接私聊我 我会在第一时间为你解答。
FTP上传下载文件
FTP上传工具 论坛FTP 专用 上传工具
企业内部备份大文件,如数据库压缩文件超过30G,可能由于某些备份软件限制导致不能完全被ftp上传完,传后只有20多G,这样就失去了备份的意义,文件也无法被解压缩,针对此现状自己开发个小工具,经测试上传2G大小的...
一个PB开发的FTP上传与下载案例,绝对原创哦,自动升级程序
C#实现FTP上传、下载、删除文件,扫描(正则过滤)指定文件并上传 C#实现FTP上传、下载、删除文件,扫描(正则过滤)指定文件并上传
curl实现ftp上传下载,vs版本实现,可以使用
winform实现FTP上传、下载、删除文件。自己写的Demo,亲测可用!
ftp 文件上传 ftp上传图片
根据迁安深蓝技术公司(萤石云网站提供),定制开发的摄像头自动截图和ftp上传图片
本程序在VS2010环境下进行开发,程序用C# WinForm实现了FTP上传下载(可实现上传所选目录下的所有文件)并带上传进度条,程序会在运行根目录下自动生成日志文件以方便查看信息...本程序主要由socket进行通信...程序由...
FTP上传(断点续传)更新进度条 公司项目第一个版本
ftp 上传时,用到的工具类,项目上配置好ftp服务器后,controller可以方便的调用此工具类进行上传
ftp上传工具上传ftp服务器,支持配置多个远程ftp服务器,支持配置运行时间间隔 同步。指定上传文件路径
虚拟主机上传器,ftp上传器.FTP上传工具、FTP软件、FTP下载工具