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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
雷峰网
雷峰网
T
Tailwind CSS Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
The Blog of Author Tim Ferriss
S
Schneier on Security
C
CERT Recently Published Vulnerability Notes
Help Net Security
Help Net Security
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
I
InfoQ
H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
G
GRAHAM CLULEY
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
I
Intezer
大猫的无限游戏
大猫的无限游戏
AWS News Blog
AWS News Blog
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Spread Privacy
Spread Privacy
博客园_首页
宝玉的分享
宝玉的分享
量子位
T
Threatpost
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
C
Cybersecurity and Infrastructure Security Agency CISA
SecWiki News
SecWiki News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
T
Tenable Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
The Register - Security
The Register - Security
V2EX - 技术
V2EX - 技术
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Last Week in AI
Last Week in AI
L
LangChain Blog
T
Tor Project blog
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客

博客园 - 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);
  }
}