网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java代码ftp传文件,java通过ftp上传文件

java中怎么实现ftp文件传输

package com.quantongfu.;

成都创新互联长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为惠城企业提供专业的成都网站制作、网站设计,惠城网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。

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;

/**

* @项目名称: telinSyslog

* @文件名称: 

* @创建日期:2015年9月14日 下午3:22:08

* @功能描述:ftp实体类,用于连接,上传

* @修订记录:

*/

public class Ftp {

private static Logger logger = Logger.getLogger();

private FTPClient ftp;

/**

* @param path

*            上传到ftp服务器哪个路径下

* @param addr

*            地址

* @param port

*            端口号

* @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 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:服务器名字

// user:用户名

// password:密码

// path:服务器上的路径

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代码实现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服务器哪个路径下

* @param addr 地址

* @param port 端口号

* @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代码ftp传文件,java通过ftp上传文件
文章来源:http://bjjierui.cn/article/hoopsi.html

其他资讯