惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - josson

Linux的一些命令 如何实时查看mysql当前连接数? JavaScript/Jscript核心语言对象扩充函数 (收藏)Turbo C 2.0、Borland C++库函数及用例 - josson (收藏)C库函数手册 MySQL的大小写敏感设置 ORACLE应用经验-表空间 Linux中通过locale来设置字符集 Oracle字符集问题总结 Oracle 表空间相关命令常识 定时执行的SQL脚本 ORACLE常识_关于SQLPLUS Axis开发之Stubs方式 Web Service 入门之常识 RMI入门 常用正则表达式 一些javascript脚本 文本模式启动linux vi 命令说明
JAVA写文件到FTP的两种方法(转) - josson - 博客园
josson · 2006-11-03 · via 博客园 - josson

http://www.blogjava.net/JaVaa/

1.使用URL:

URL url = new  URL( " ftp://javaa:javaa@172.168.2.222:21/test/javaa.txt " );
PrintWriter pw
= new  PrintWriter(url.openConnection().getOutputStream());
pw.write(
" this is a test " );
pw.flush();
pw.close();

上 面是代码的片断,其中URL构造函数的参数可以用不同的访问协议(比如http,ftp等),"//"后跟着的是用户名和密码,两者用":"隔开,紧跟着 是分隔符"@","@"以后的是IP地址和端口,然后是目录,最后才是我们要写入的文件名,其中目录是必须存在的,否则会抛出 FileNotFoundException,文件可以是不存在的,不存在的时候就会新建文件,否则就会用新的内容覆盖以前的内容;

2.使用FtpClient:

FtpClient ftpClient = new  FtpClient();
ftpClient.openServer(
" 172.168.2.222 " , 21 ); // IP地址和端口
ftpClient.login( " javaa " , " javaa " ); // 用户名和密码,匿名登陆的话用户名为anonymous,密码为非空字符串
ftpClient.cd( " test " ); // 切换到test目录
PrintWriter pw = new  PrintWriter(ftpClient.put( " javaa.txt " )); // 写入的文件名
pw.write( " this is a test " );
pw.flush();
pw.close();

3.用PASV模式传送数据的FtpClient

import sun.net.ftp.FtpClient;
import java.net.Socket;
import java.io.IOException;public class PasvFtpClient
    
extends FtpClient{/**
   * FTP服务器的地址
   
*/
  
private String serverAddr;
  
/**
   * 连接到FTP服务器的Socket
   
*/
  
private Socket socket;
  
/**
   * 仿造父类定义的静态变量
   
*/
  
protected final static int FTP_ERROR=3;
  
/**
   * 仿造父类定义的静态变量
   
*/
  
protected final static int FTP_SUCCESS=1;public PasvFtpClient(String s) throws IOException{
    
super(s);
    serverAddr
=s;
    socket
=null;
  }
public PasvFtpClient(String s,int i) throws IOException{
    
super(s,i);
    serverAddr
=s;
    socket
=null;
  }
public PasvFtpClient(){
    
super();
    socket
=null;
  }
/**
   * 复写的主要部分,父类采用PORT模式,这里改为PASV模式
   * 
@param s 传入的FTP命令
   * 
@return 连接到FTP服务器的Socket
   * 
@throws IOException
   
*/
  
protected Socket openDataConnection(String s) throws IOException{
    
if (socket==null){
      String s1
="PASV";
      
if (issueCommand(s1)==FTP_ERROR){
        MyFtpProtocolException ftpprotocolexception
=new MyFtpProtocolException(
            
"PASV");
        
throw ftpprotocolexception;
      }
      String responseStr
=this.getResponseString();
      
int location=responseStr.lastIndexOf(",");
      
int n=Integer.parseInt(responseStr.substring(location+1,
          responseStr.indexOf(
")")));
      responseStr
=responseStr.substring(0,location);
      location
=responseStr.lastIndexOf(",");
      
int m=Integer.parseInt(responseStr.substring(location+1,
          responseStr.length()));
      socket
=new Socket(serverAddr,m*256+n);
    }
    
if (issueCommand(s)==FTP_ERROR){
      MyFtpProtocolException ftpprotocolexception1
=new MyFtpProtocolException(s);
      
throw ftpprotocolexception1;
    }
    
return socket;
  }
/**
   * 关闭与FTP服务器的连接
   * 
@throws IOException
   
*/
  
public void closeServer() throws IOException{
    socket.close();
    socket
=null;
    
super.closeServer();
  }
/**
   * 打开与FTP服务器的连接
   * 
@param s FTP服务器地址
   * 
@param i FTP服务器端口
   * 
@throws IOException
   
*/
  
public void openServer(String s,int i) throws IOException{
    
super.openServer(s,i);
    serverAddr
=s;
  }
}
/**
 * 自定义的FTP异常类
 
*/
class MyFtpProtocolException
    
extends IOException{
  MyFtpProtocolException(String s){
    
super(s);
  }
}