欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

javaftp上傳代碼 javaftp上傳文件

java中怎么實(shí)現(xiàn)ftp文件傳輸

package?com.quantongfu.;

成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、富順網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為富順等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

import?java.io.File;

import?java.io.FileInputStream;

import?java.io.IOException;

import?java.net.ServerSocket;

import?java.util.List;

import?org.apache.commons.net.;

import?org.apache.log4j.Logger;

import?org.apache.log4j.net.SocketServer;

import?com.quantongfu.conf.FtpConf;

/**

*?@項(xiàng)目名稱:?telinSyslog

*?@文件名稱:?

*?@創(chuàng)建日期:2015年9月14日?下午3:22:08

*?@功能描述:ftp實(shí)體類,用于連接,上傳

*?@修訂記錄:

*/

public?class?Ftp?{

private?static?Logger?logger?=?Logger.getLogger();

private?FTPClient?ftp;

/**

*?

*?@param?path

*????????????上傳到ftp服務(wù)器哪個(gè)路徑下

*?@param?addr

*????????????地址

*?@param?port

*????????????端口號(hào)

*?@param?username

*????????????用戶名

*?@param?password

*????????????密碼

*?@return

*?@throws?Exception

*/

public?boolean?connect()?throws?Exception?{

boolean?result?=?false;

ftp?=?new?FTPClient();

int?reply;

(FtpConf.FTP_HOST,?FtpConf.FTP_PORT);

(FtpConf.FTP_USER_NAME,?FtpConf.FTP_PASSWORD);

;

;

reply?=?;

if?(!FTPReply.isPositiveCompletion(reply))?{

;

return?result;

}

if?(FtpConf.IS_FTP_DIRECTORY)?{

;

}

result?=?true;

return?result;

}

/**

*?

*?@param?files

*????????????上傳的文件

*?@throws?Exception

*/

public?boolean?upload(File?file)?throws?IOException?{

FileInputStream?input?=?null;

try?{

input?=?new?FileInputStream(file);

boolean?b?=?(file.getName()?+?".tmp",?input);

if?(b)?{

b?=?(file.getName()?+?".tmp",?file.getName());

}

return?b;

}?catch?(Exception?e)?{

e.printStackTrace();

return?false;

}?finally?{

if?(input?!=?null)?{

input.close();

}

}

}

/**

*?

*?@param?files

*????????????上傳的文件

*?@throws?Exception

*/

public?boolean?upload(ServerSocket?server,?File?file)?throws?Exception?{

FileInputStream?input?=?null;

try?{

if?(!file.exists())?{

return?true;

}

input?=?new?FileInputStream(file);

boolean?b?=?(server,?file.getName()?+?".tmp",?input);

if?(b)?{

b?=?(file.getName()?+?".tmp",?file.getName());

if?(b)?{

file.delete();

}

}

return?b;

}?catch?(Exception?e)?{

logger.error("ftp?error"?+?e.getMessage());

return?false;

}?finally?{

if?(input?!=?null)?{

try?{

input.close();

}?catch?(IOException?e)?{

e.printStackTrace();

}

}

}

}

/*斷開連接*/

public?void?disConnect()?{

try?{

if?(ftp?!=?null)?{

;

}

}?catch?(IOException?e)?{

e.printStackTrace();

}

}

/*獲取連接*/

public?static?Ftp?getFtp()?{

Ftp?ftp?=?new?Ftp();

try?{

;

}?catch?(Exception?e)?{

logger.error("FTP連接異常"?+?e.getMessage());

e.printStackTrace();

}

return?ftp;

}

/*重連*/

public?Ftp?reconnect()?{

disConnect();

return?getFtp();

}

}

使用Apache FtpClient jar包,獲取jar :

如何用java代碼實(shí)現(xiàn)ftp文件上傳

import java.io.File;

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]);

java 實(shí)現(xiàn)ftp上傳如何創(chuàng)建文件夾?

準(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怎么上傳文件

上傳下載的代碼

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();

}

}

java jdk1.6環(huán)境下實(shí)現(xiàn) ftp文件上傳

通過JDK自帶的API實(shí)現(xiàn)

package com.cloudpower.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import sun.net.TelnetInputStream;

import sun.net.TelnetOutputStream;

import sun.net.;

/**

* Java自帶的API對FTP的操作

* @Title:

* @author: 周玲斌

*/

public class Ftp {

/**

* 本地文件名

*/

private String localfilename;

/**

* 遠(yuǎn)程文件名

*/

private String remotefilename;

/**

* FTP客戶端

*/

private FtpClient ftpClient;

/**

* 服務(wù)器連接

* @param ip 服務(wù)器IP

* @param port 服務(wù)器端口

* @param user 用戶名

* @param password 密碼

* @param path 服務(wù)器路徑

* @author 周玲斌

* @date 2012-7-11

*/

public void connectServer(String ip, int port, String user,

String password, String path) {

try {

/* ******連接服務(wù)器的兩種方法*******/

//第一種方法

// ftpClient = new FtpClient();

// ftpClient.openServer(ip, port);

//第二種方法

ftpClient = new FtpClient(ip);

ftpClient.login(user, password);

// 設(shè)置成2進(jìn)制傳輸

ftpClient.binary();

System.out.println("login success!");

if (path.length() != 0){

//把遠(yuǎn)程系統(tǒng)上的目錄切換到參數(shù)path所指定的目錄

ftpClient.cd(path);

}

ftpClient.binary();

} catch (IOException ex) {

ex.printStackTrace();

throw new RuntimeException(ex);

}

}

/**

* 關(guān)閉連接

* @author 周玲斌

* @date 2012-7-11

*/

public void closeConnect() {

try {

ftpClient.closeServer();

System.out.println("disconnect success");

} catch (IOException ex) {

System.out.println("not disconnect");

ex.printStackTrace();

throw new RuntimeException(ex);

}

}

/**

* 上傳文件

* @param localFile 本地文件

* @param remoteFile 遠(yuǎn)程文件

* @author 周玲斌

* @date 2012-7-11

*/

public void upload(String localFile, String remoteFile) {

this.localfilename = localFile;

this.remotefilename = remoteFile;

TelnetOutputStream os = null;

FileInputStream is = null;

try {

//將遠(yuǎn)程文件加入輸出流中

os = ftpClient.put(this.remotefilename);

//獲取本地文件的輸入流

File file_in = new File(this.localfilename);

is = new FileInputStream(file_in);

//創(chuàng)建一個(gè)緩沖區(qū)

byte[] bytes = new byte[1024];

int c;

while ((c = is.read(bytes)) != -1) {

os.write(bytes, 0, c);

}

System.out.println("upload success");

} catch (IOException ex) {

System.out.println("not upload");

ex.printStackTrace();

throw new RuntimeException(ex);

} finally{

try {

if(is != null){

is.close();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if(os != null){

os.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* 下載文件

* @param remoteFile 遠(yuǎn)程文件路徑(服務(wù)器端)

* @param localFile 本地文件路徑(客戶端)

* @author 周玲斌

* @date 2012-7-11

*/

public void download(String remoteFile, String localFile) {

TelnetInputStream is = null;

FileOutputStream os = null;

try {

//獲取遠(yuǎn)程機(jī)器上的文件filename,借助TelnetInputStream把該文件傳送到本地。

is = ftpClient.get(remoteFile);

File file_in = new File(localFile);

os = new FileOutputStream(file_in);

byte[] bytes = new byte[1024];

int c;

while ((c = is.read(bytes)) != -1) {

os.write(bytes, 0, c);

}

System.out.println("download success");

} catch (IOException ex) {

System.out.println("not download");

ex.printStackTrace();

throw new RuntimeException(ex);

} finally{

try {

if(is != null){

is.close();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if(os != null){

os.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public static void main(String agrs[]) {

String filepath[] = { "/temp/aa.txt", "/temp/regist.log"};

String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"};

Ftp fu = new Ftp();

/*

* 使用默認(rèn)的端口號(hào)、用戶名、密碼以及根目錄連接FTP服務(wù)器

*/

fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp");

//下載

for (int i = 0; i filepath.length; i++) {

fu.download(filepath[i], localfilepath[i]);

}

String localfile = "E:\\號(hào)碼.txt";

String remotefile = "/temp/哈哈.txt";

//上傳

fu.upload(localfile, remotefile);

fu.closeConnect();

}

}

本文名稱:javaftp上傳代碼 javaftp上傳文件
URL鏈接:http://aaarwkj.com/article34/hhhese.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站策劃、電子商務(wù)、微信公眾號(hào)、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)
国产精品白嫩初高中害羞小美女 | 亚洲日本中文字幕免费观看| 99热只有这里才有精品| 日韩精品在线观看视频一区二区三区| 国产日韩精品国产二区| 亚洲精品国产自在现线| 91精品一久久香蕉国产| 欧美一区二区精品少妇| 日本在线高清精品人妻| 亚洲熟女av综合网丁香| 午夜影院免费在线观看五分钟| 国产模特一区二区三区| 亚洲欧美激情专区在线| 欧美性生活真实的视频| 国产饥渴熟女在线三区| 日日淫夜夜操熟女视频| 天天操天天夜夜操夜夜| 九九九热精品视频在线观看| 中文字幕乱码日韩在线| 国产精品福利午夜在线观看| 在线日韩观看免费av| 日韩精品在线观看一二三区| 欧美日韩精品不卡在线播放| 久久久国产精品视频一区| 四虎在线经典视频播放| 欧美午夜福利在线电影| 蜜桃福利视频一区二区| 欧美日韩亚洲中文综合网 | 九九国产精品免费视频| 一区二区三区欧美久久| 玩弄丰满熟妇留守妇女| 男人天堂一区二区av| 无遮挡动漫网站免费观看| 国产丝袜美腿视频亚洲综合| 国产一区二区爽爽爽视频| 黄色亚洲日本欧美在线观看| 清纯唯美校园春色亚洲激情| 国产精品一级性生活片| 亚洲女人下体毛茸茸视频| 亚洲国产成人91精品| 国产一级av在线播放|