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鏈嶅姟鍣ㄩ粯璁P鍦板潃
int port=21;//ftp鏈嶅姟榛樿绔彛
String user="";//鐧誨綍ftp鎵闇甯愭埛
String pwd="";//瀵嗙爜
String path="";//鎵鍦ㄧ殑璺緞
String err="";
/*
* 嫻嬭瘯鏂規(guī)硶錛岀敤URL綾葷洿鎺ヨ鍙杅tp涓婄殑鏂囦歡
* */
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瀹㈡埛绔繛鎺?br style="line-height: 22px; " />
* @author chenshan
* @date 2008-11-11
* @param ip錛?nbsp; ftp鏈嶅姟鍣ㄦ墍鍦ㄧ殑IP鍦板潃錛?nbsp;
* port錛?nbsp; ftp鏈嶅姟鍗犵敤鐨勭鍙o紱
* user錛?nbsp; 鐧誨綍ftp鎵闇甯愭埛錛?br style="line-height: 22px; " />
* pwd錛?nbsp; 鐧誨綍ftp鎵闇瀵嗙爜錛?br style="line-height: 22px; " />
* path錛?nbsp; 瑕佺櫥褰曞埌ftp鐨勭浉瀵硅礬寰勶紝濡傝榪涘叆ftp://10.140.112.120/HBSQ/System_copy錛?br style="line-height: 22px; " />
* 閭d箞path搴旇涓?#8221;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 璇繪湰鍦版枃浠跺皢鍏跺啓鍏tp鏈嶅姟鍣紙璇ユ柟娉曟祴璇曠敤錛?br style="line-height: 22px; " />
* @author chenshan
* @date 2008-11-11
* @param textContext錛氭墍闇鍐欏叆鐨勫唴瀹?nbsp;
*
*/
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);//鍦ㄦ湇鍔″櫒涓婂垱寤烘枃浠?br style="line-height: 22px; " />
/*浠ヤ笅涓鴻鍑烘湰鍦版枃浠?/
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);//鍐欏叆鍒癴tp涓婂垱寤虹殑鏂囦歡涓?br style="line-height: 22px; " />
}
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 鍦╢tp鏈嶅姟鍣ㄥ垱寤烘枃浠訛紝騫跺皢鎸囧畾鐨勫唴瀹瑰啓鍏ユ枃浠?br style="line-height: 22px; " />
* @author chenshan
* @date 2008-11-11
* @param textContext: 闇瑕佸啓鍏tp鏂囦歡鐨勫唴瀹瑰瓧絎︿覆,
* remotefilename: 鍦╢tp涓婂垱寤虹殑鏂囦歡鍚嶇О
* bankCode 閾惰浠g爜錛堝媧ョ殑閾惰浠g爜涓?3鍜?7錛?3鏄偖鏀垮眬錛?7鏄腑鍥介摱琛岋級(jí)
* path 浠f墸鏂囦歡闇瑕佸彂閫佽嚦鏈嶅姟鍣ㄧ殑鐩稿璺緞
*/
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("鍙戠幇涓嶅悎娉曠殑閾惰浠g爜: "+bankCode+"=========>浠f墸鏂囦歡鏈兘鍙戦佽嚦FTP鏈嶅姟鍣?);
return "鍙戠幇涓嶅悎娉曠殑閾惰浠g爜: "+bankCode+" -->浠f墸鏂囦歡鏈兘鍙戦佽嚦FTP鏈嶅姟鍣?璇蜂粩緇嗘鏌ュ茍鎵嬪伐鎷瘋礉錛?;
}
FtpClient ftpClient = getFtpConnection(this.ip,this.port,this.user,this.pwd,this.path);
byte[] _textContext = textContext.getBytes();
TelnetOutputStream os = ftpClient.put(remotefilename);//鍦ㄦ湇鍔″櫒涓婂垱寤烘枃浠?br style="line-height: 22px; " />
//灝嗗瓧鑺傚唴瀹瑰啓鍏ュ埌os鎵浠h〃鐨勬枃浠朵腑
os.write(_textContext,0,_textContext.length);
//鍏抽棴涓巉tp鏈嶅姟鍣ㄧ殑榪炴帴
System.out.println("upload success");
os.close();
ftpClient.closeServer();
return "0";
}catch(Exception ex){
this.err+=ex.getMessage();
return err;
}
}
}