package com.dicpsi.common.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.*;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class ftpWriteAndRead {
String ip="10.140.112.120";//ftp服务器默认IP地址
int port=21;//ftp服务默认端口
String user="";//登录ftp所需帐户
String pwd="";//密码
String path="";//所在的路径
String err="";
/*
* 测试方法,用URL类直接读取ftp上的文件
* */
public void tt() throws MalformedURLException{
try{
URL u=new URL("ftp://xjdl93:yzj@10.140.112.120/HBSQ/System_copy/123.txt");
String _file=u.getFile();
System.out.println(_file+">>>"+u.getHost());
InputStream is= u.openStream();
if(is == null){
System.out.println("没有能够获取指定的资源");
return;
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @comment 获取FTP客户端连接
* @author chenshan
* @date 2008-11-11
* @param ip: ftp服务器所在的IP地址;
* port: ftp服务占用的端口;
* user: 登录ftp所需帐户;
* pwd: 登录ftp所需密码;
* path: 要登录到ftp的相对路径,如要进入ftp://10.140.112.120/HBSQ/System_copy,
* 那么path应该为”HBSQ/System_copy“
*/
public FtpClient getFtpConnection(String ip,int port,String user,String pwd,String path){
try{
FtpClient cftp=new FtpClient();
cftp.openServer(ip,port);
cftp.login(user, pwd);
System.out.println("!");
if (path.length() != 0) cftp.cd(path);
//cftp.binary();
return cftp;
}catch(Exception ex){
ex.printStackTrace();
err+=ex.getMessage();
return null;
}
}
/**
* @comment 读本地文件将其写入ftp服务器(该方法测试用)
* @author chenshan
* @date 2008-11-11
* @param textContext:所需写入的内容
*
*/
public void upload(String textContext) {
this.user="xjdl93";
this.pwd="yzj";
this.path="HBSQ/System_copy";
FtpClient ftpClient = getFtpConnection(this.ip,this.port,this.user,this.pwd,this.path);
String localfilename = "E://testftp//qqq.txt";
String remotefilename = "qqq.txt";
/*以下为获取需要上传的文件列表*/
// try{
// URL url=new URL("E:\testftp\qqq.txt");
// String [] fileNames=null;
// //fileNames=url.getFile().;
// System.out.print(url.getFile());
// return;
// }catch(Exception a){
// a.printStackTrace();
// System.out.println("获取所需上传文件列表失败");
// }
try {
TelnetOutputStream os = ftpClient.put(remotefilename);//在服务器上创建文件
/*以下为读出本地文件*/
java.io.File file_in = new java.io.File(localfilename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);//写入到ftp上创建的文件中
}
System.out.println("upload success");
is.close();
os.close();
ftpClient.closeServer();
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("not upload");
System.out.println(ex);
}
}
/**
* @comment 在ftp服务器创建文件,并将指定的内容写入文件
* @author chenshan
* @date 2008-11-11
* @param textContext: 需要写入ftp文件的内容字符串,
* remotefilename: 在ftp上创建的文件名称
* bankCode 银行代码(夏津的银行代码为93和97,93是邮政局,97是中国银行)
* path 代扣文件需要发送至服务器的相对路径
*/
public String writeFileToFtp(String textContext,String remotefilename,String bankCode,String path){
try{
this.path=path;
if(bankCode.equals("97")){
this.user="xjdl97";
this.pwd="123456";
}
else if(bankCode.equals("93")){
this.user="xjdl93";
this.pwd="yzj";
}
else{
System.out.println("发现不合法的银行代码: "+bankCode+"=========>代扣文件未能发送至FTP服务器");
return "发现不合法的银行代码: "+bankCode+" -->代扣文件未能发送至FTP服务器,请仔细检查并手工拷贝!";
}
FtpClient ftpClient = getFtpConnection(this.ip,this.port,this.user,this.pwd,this.path);
byte[] _textContext = textContext.getBytes();
TelnetOutputStream os = ftpClient.put(remotefilename);//在服务器上创建文件
//将字节内容写入到os所代表的文件中
os.write(_textContext,0,_textContext.length);
//关闭与ftp服务器的连接
System.out.println("upload success");
os.close();
ftpClient.closeServer();
return "0";
}catch(Exception ex){
this.err+=ex.getMessage();
return err;
}
}
}