java代码操作ftp java代码操作数据库如何运行
如何用Java实现FTP服务器
FTP(File Transfer Protocol 文件传输协议)是Internet 上用来传送文件的协议。在Internet上通过FTP 服务器可以进行文件的上传(Upload)或下载(Download)。FTP是实时联机服务,在使用它之前必须是具有该服务的一个用户(用户名和口令),工作时客户端必须先登录到作为服务器一方的计算机上,用户登录后可以进行文件搜索和文件传送等有关操作,如改变当前工作目录、列文件目录、设置传输参数及传送文件等。使用FTP可以传送所有类型的文件,如文本文件、二进制可执行文件、图象文件、声音文件和数据压缩文件等。
成都创新互联公司专注于企业全网整合营销推广、网站重做改版、平度网站定制设计、自适应品牌网站建设、H5高端网站建设、商城网站定制开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为平度等各大城市提供网站开发制作服务。
FTP 命令
FTP 的主要操作都是基于各种命令基础之上的。常用的命令有:
设置传输模式,它包括ASCⅡ(文本) 和BINARY 二进制模式;
目录操作,改变或显示远程计算机的当前目录(cd、dir/ls 命令);
连接操作,open命令用于建立同远程计算机的连接;close命令用于关闭连接;
发送操作,put命令用于传送文件到远程计算机;mput 命令用于传送多个文件到远程计算机;
获取操作,get命令用于接收一个文件;mget命令用于接收多个文件。
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
import java.net.Socket;import org.apache.log4j.Logger;/** * 角色——服务器A * @author Leon * */public class ServerA{ public static void main(String[] args){ final String F_DIR = "c:/test";//根路径 final int PORT = 22;//监听端口号 Logger.getRootLogger(); Logger logger = Logger.getLogger("com"); try{ ServerSocket s = new ServerSocket(PORT); logger.info("Connecting to server A..."); logger.info("Connected Successful! Local Port:"+s.getLocalPort()+". Default Directory:'"+F_DIR+"'."); while( true ){ //接受客户端请求 Socket client = s.accept(); //创建服务线程 new ClientThread(client, F_DIR).start(); } } catch(Exception e) { logger.error(e.getMessage()); for(StackTraceElement ste : e.getStackTrace()){ logger.error(ste.toString()); } } }}import java.io.BufferedReader; import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.io.RandomAccessFile;import java.net.ConnectException;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;import java.nio.charset.Charset;import java.util.Random;import org.apache.log4j.Logger;/** * 客户端子线程类 * @author Leon * */public class ClientThread extends Thread { private Socket socketClient;//客户端socket private Logger logger;//日志对象 private String dir;//绝对路径 private String pdir = "/";//相对路径 private final static Random generator = new Random();//随机数 public ClientThread(Socket client, String F_DIR){ this.socketClient = client; this.dir = F_DIR; } @Override public void run() { Logger.getRootLogger(); logger = Logger.getLogger("com"); InputStream is = null; OutputStream os = null; try { is = socketClient.getInputStream(); os = socketClient.getOutputStream(); } catch (IOException e) { logger.error(e.getMessage()); for(StackTraceElement ste : e.getStackTrace()){ logger.error(ste.toString()); } } BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); PrintWriter pw = new PrintWriter(os); String clientIp = socketClient.getInetAddress().toString().substring(1);//记录客户端IP String username = "not logged in";//用户名 String password = "";//口令 String command = "";//命令 boolean loginStuts = false;//登录状态 final String LOGIN_WARNING = "530 Please log in with USER and PASS first."; String str = "";//命令内容字符串 int port_high = 0; int port_low = 0; String retr_ip = "";//接收文件的IP地址 Socket tempsocket = null; //打印欢迎信息 pw.println("220-FTP Server A version 1.0 written by Leon Guo"); pw.flush(); logger.info("("+username+") ("+clientIp+") Connected, sending welcome message..."); logger.info("("+username+") ("+clientIp+") 220-FTP Server A version 1.0 written by Leon Guo"); boolean b = true; while ( b ){ try { //获取用户输入的命令 command = br.readLine(); if(null == command) break; } catch (IOException e) { pw.println("331 Failed to get command"); pw.flush(); logger.info("("+username+") ("+clientIp+") 331 Failed to get command"); logger.error(e.getMessage()); for(StackTraceElement ste : e.getStackTrace()){ logger.error(ste.toString()); } b = false; } /* * 访问控制命令 */ // USER命令 if(command.toUpperCase().startsWith("USER")){ logger.info("(not logged in) ("+clientIp+") "+command); username = command.substring(4).trim(); if("".equals(username)){ pw.println("501 Syntax error"); pw.flush(); logger.info("(not logged in) ("+clientIp+") 501 Syntax error"); username = "not logged in"; } else{ pw.println("331 Password required for " + username); pw.flush(); logger.info("(not logged in) ("+clientIp+") 331 Password required for " + username); } loginStuts = false; } //end USER // PASS命令 else if(command.toUpperCase().startsWith("PASS")){ logger.info("(not logged in) ("+clientIp+") "+command); password = command.substring(4).trim(); if(username.equals("root") password.equals("root")){ pw.println("230 Logged on"); pw.flush(); logger.info("("+username+") ("+clientIp+") 230 Logged on");// logger.info("客户端 "+clientIp+" 通过 "+username+"用户登录"); loginStuts = true; } else{ pw.println("530 Login or password incorrect!"); pw.flush(); logger.info("(not logged in) ("+clientIp+") 530 Login or password incorrect!"); username = "not logged in"; } } //end PASS // PWD命令 else if(command.toUpperCase().startsWith("PWD")){ logger.info("("+username+") ("+clientIp+") "+command); if(loginStuts){// logger.info("用户"+clientIp+":"+username+"执行PWD命令"); pw.println("257 /""+pdir+"/" is current directory"); pw.flush(); logger.info("("+username+") ("+clientIp+") 257 /""+pdir+"/" is current directory"); } else{ pw.println(LOGIN_WARNING); pw.flush(); logger.info("("+username+") ("+clientIp+") "+LOGIN_WARNING); } } //end PWD // CWD命令 else if(command.toUpperCase().startsWith("CWD")){ logger.info("("+username+") ("+clientIp+") "+command); if(loginStuts){ str = command.substring(3).trim(); if("".equals(str)){ pw.println("250 Broken client detected, missing argument to CWD. /""+pdir+"/" is current directory."); pw.flush(); logger.info("("+username+") ("+clientIp+") 250 Broken client detected, missing argument to CWD. /""+pdir+"/" is current directory."); } else{ //判断目录是否存在 String tmpDir = dir + "/" + str; File file = new File(tmpDir); if(file.exists()){//目录存在 dir = dir + "/" + str; if("/".equals(pdir)){ pdir = pdir + str; } else{ pdir = pdir + "/" + str; }// logger.info("用户"+clientIp+":"+username+"执行CWD命令"); pw.println("250 CWD successful. /""+pdir+"/" is current directory"); pw.flush(); logger.info("("+username+") ("+clientIp+") 250 CWD successful. /""+pdir+"/" is current directory"); } else{//目录不存在 pw.println("550 CWD failed. /""+pdir+"/": directory not found."); pw.flush(); logger.info("("+username+") ("+clientIp+") 550 CWD failed. /""+pdir+"/": directory not found.");
如何用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访问
在主函数中,完成服务器端口的侦听和服务线程的创建。我们利用一个静态字符串变量initDir 来保存服务器线程运行时所在的工作目录。服务器的初始工作目录是由程序运行时用户输入的,缺省为C盘的根目录。
具体的代码如下:
public class ftpServer extends Thread{
private Socket socketClient;
private int counter;
private static String initDir;
public static void main(String[] args){
if(args.length != 0) {
initDir = args[0];
}else{ initDir = "c:";}
int i = 1;
try{
System.out.println("ftp server started!");
//监听21号端口
ServerSocket s = new ServerSocket(21);
for(;;){
//接受客户端请求
Socket incoming = s.accept();
//创建服务线程
new ftpServer(incoming,i).start();
i++;
}
}catch(Exception e){}
}
java中怎么实现ftp文件传输
package com.quantongfu.;
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 java代码操作数据库如何运行
URL网址:http://hbruida.cn/article/doeddsd.html