import java.io.File;
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、吉安網(wǎng)站維護(hù)、網(wǎng)站推廣。
import java.io.FileInputStream;
import org.apache.commons.net.;
import org.apache.commons.net.;
public class test {
private FTPClient ftp;
/**
*
* @param path 上傳到ftp服務(wù)器哪個(gè)路徑下
* @param addr 地址
* @param port 端口號(hào)
* @param username 用戶名
* @param password 密碼
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
;
;
;
reply = ;
if (!FTPReply.isPositiveCompletion(reply)) {
;
return result;
}
;
result = true;
return result;
}
/**
*
* @param file 上傳的文件或文件夾
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
(file.getName());
(file.getName());
String[] files = file.list();
for (int i = 0; i files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
;
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
上傳下載的代碼
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import sun.net.TelnetOutputStream;
import sun.net.TelnetInputStream;
import sun.net.;
public class download {
String localfilename;
String remotefilename;
FtpClient ftpClient;
// server:服務(wù)器名字
// user:用戶名
// password:密碼
// path:服務(wù)器上的路徑
public void connectServer(String ip, int port,String user
, String password,String path) {
try {
ftpClient = new FtpClient();
ftpClient.openServer(ip,port);
ftpClient.login(user, password);
System.out.println("login success!");
if (path.length() != 0) ftpClient.cd(path);
ftpClient.binary();
} catch (IOException ex) {
System.out.println("not login");
System.out.println(ex);
}
}
public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
System.out.println(ex);
}
}
public void upload() {
this.localfilename = "D://test2//test.txt";
this.remotefilename = "test.txt";
try {
TelnetOutputStream os = ftpClient.put(this.remotefilename);
java.io.File file_in = new java.io.File(this.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);
}
System.out.println("upload success");
is.close();
os.close();
} catch (IOException ex) {
System.out.println("not upload");
System.out.println(ex);
}
}
public void download() {
try {
TelnetInputStream is = ftpClient.get(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remotePath,String remoteFile,String localFile) {
try {
if (remotePath.length() != 0) ftpClient.cd(remotePath);
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remoteFile,String localFile) {
try {
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
} catch (IOException ex) {
System.out.println("not download");
System.out.println(ex);
}
}
public static void main(String agrs[]) {
String filepath[] = { "/callcenter/index.jsp", "/callcenter/ip.txt",
"/callcenter/mainframe/image/processing_bar_2.gif",
"/callcenter/mainframe/image/logo_01.jpg" };
String localfilepath[] = { "C:\\FTP_Test\\index.jsp",
"C:\\FTP_Test\\ip.txt", "C:\\FTP_Test\\processing_bar_2.gif",
"C:\\FTP_Test\\logo_01.jpg" };
download fu = new download();
fu.connectServer("172.16.1.66",22, "web_test", "123456","/callcenter");
for(int i=0;ifilepath.length;i++){
fu.download(filepath[i],localfilepath[i]);
}
//fu.upload();
//fu.download();
fu.closeConnect();
}
}
準(zhǔn)備條件:java實(shí)現(xiàn)ftp上傳用到了commons-net-3.3.jar包
首先建立ftphost連接
public?boolean?connect(String?path,?String?addr,?int?port,?String?username,?String?password)?{
try?{
//FTPClient?ftp?=?new?FTPHTTPClient(addr,?port,?username,?password);
ftp?=?new?FTPClient();
int?reply;
;
System.out.println("連接到:"?+?addr?+?":"?+?port);
System.out.print();
reply?=?;
if?(!FTPReply.isPositiveCompletion(reply))?{
;
System.err.println("FTP目標(biāo)服務(wù)器積極拒絕.");
System.exit(1);
return?false;
}else{
(username,?password);
;
;
;
System.out.println("已連接:"?+?addr?+?":"?+?port);
return?true;
}
}?catch?(Exception?ex)?{
ex.printStackTrace();
System.out.println(ex.getMessage());
return?false;
}
}
然后再利用ftpclient的makeDirectory方法創(chuàng)建文件夾
public?void?createDir(String?dirname){
try{
;
System.out.println("在目標(biāo)服務(wù)器上成功建立了文件夾:?"?+?dirname);
}catch(Exception?ex){
System.out.println(ex.getMessage());
}
}
斷開host連接
public?void?disconnect(){
try?{
;
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
最后是程序的調(diào)用方法
public?static?void?main(String[]?args)?{
FtpUploadTest?ftpupload?=?new?FtpUploadTest();
if(ftpupload.connect("",?"172.39.8.x",?20,?"administrator",?"abc@123")){
ftpupload.createDir("/UPLOAD");
ftpupload.disconnect();
}
}
本文名稱:java的ftp上傳代碼 java操作ftp上的文件
網(wǎng)頁(yè)URL:http://muchs.cn/article44/hjcjhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、營(yíng)銷型網(wǎng)站建設(shè)、App設(shè)計(jì)、商城網(wǎng)站、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容