中文字幕第五页-中文字幕第页-中文字幕韩国-中文字幕最新-国产尤物二区三区在线观看-国产尤物福利视频一区二区

java代碼編寫ftp,java代碼編寫四則運算計算器

如何用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服務器客戶端程序。

import java.io.*;

import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){

String initDir;

initDir = "D:/Ftp";

ServerSocket server;

Socket socket;

String s;

String user;

String password;

user = "root";

password = "123456";

try{

System.out.println("MYFTP服務器啟動....");

System.out.println("正在等待連接....");

//監聽21號端口

server = new ServerSocket(21);

socket = server.accept();

System.out.println("連接成功");

System.out.println("**********************************");

System.out.println("");

InputStream in =socket.getInputStream();

OutputStream out = socket.getOutputStream();

DataInputStream din = new DataInputStream(in);

DataOutputStream dout=new DataOutputStream(out);

System.out.println("請等待驗證客戶信息....");

while(true){

s = din.readUTF();

if(s.trim().equals("LOGIN "+user)){

s = "請輸入密碼:";

dout.writeUTF(s);

s = din.readUTF();

if(s.trim().equals(password)){

s = "連接成功。";

dout.writeUTF(s);

break;

}

else{s ="密碼錯誤,請重新輸入用戶名:";br dout.writeUTF(s);br br }

}

else{

s = "您輸入的命令不正確或此用戶不存在,請重新輸入:";

dout.writeUTF(s);

}

}

System.out.println("驗證客戶信息完畢...."); while(true){

System.out.println("");

System.out.println("");

s = din.readUTF();

if(s.trim().equals("DIR")){

String output = "";

File file = new File(initDir);

String[] dirStructure = new String[10];

dirStructure= file.list();

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

output +=dirStructure[i]+"\n";

}

s=output;

dout.writeUTF(s);

}

else if(s.startsWith("GET")){

s = s.substring(3);

s = s.trim();

File file = new File(initDir);

String[] dirStructure = new String[10];

dirStructure= file.list();

String e= s;

int i=0;

s ="不存在";

while(true){

if(e.equals(dirStructure[i])){

s="存在";

dout.writeUTF(s);

RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");

byte byteBuffer[]= new byte[1024];

int amount;

while((amount = outFile.read(byteBuffer)) != -1){

dout.write(byteBuffer, 0, amount);break;

}break;

}

else if(idirStructure.length-1){

i++;

}

else{

dout.writeUTF(s);

break;

}

}

}

else if(s.startsWith("PUT")){

s = s.substring(3);

s = s.trim();

RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");

byte byteBuffer[] = new byte[1024];

int amount;

while((amount =din.read(byteBuffer) )!= -1){

inFile.write(byteBuffer, 0, amount);break;

}

}

else if(s.trim().equals("BYE"))break;

else{

s = "您輸入的命令不正確或此用戶不存在,請重新輸入:";

dout.writeUTF(s);

}

}

din.close();

dout.close();

in.close();

out.close();

socket.close();

}

catch(Exception e){

System.out.println("MYFTP關閉!"+e);

}

}}

怎么用java代碼創建ftp用戶和密碼

創建ftp用戶名和密碼,其實就在ftp服務器的用戶文件里面添加條記錄。

方法有兩種,我說下思路。

一、你可以用java程序找到相應的配置文件,打開、把用戶名密碼寫入進去。ok了。

二、你用用java程序調用創建ftp用戶的命令,來創建ftp用戶。

如何用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.net.ConnectException: Connection refused: connect FTP

你用的FTPClient引入不對吧,我們項目上都是用的

import org.apache.commons.net.;

import org.apache.commons.net.;

import org.apache.commons.net.;

下面是我們項目上用到的FTP的實現代碼(FTP需要先連接,再登錄,之后就是校驗登錄是否成功),具體代碼如下:

/**

*?獲取FTPClient對象

*

*?@param?ftpHost?FTP主機服務器

*?@param?ftpPassword?FTP?登錄密碼

*?@param?ftpUserName?FTP登錄用戶名

*?@param?ftpPort?FTP端口?默認為21

*?@return?FTPClient

*?@throws?Exception

*/

public?static?FTPClient?getFTPClient(String?ftpHost,?String?ftpUserName,

String?ftpPassword,?int?ftpPort)?throws?Exception?{

try?{

FTPClient?ftpClient?=?new?FTPClient();

ftpClient.connect(ftpHost,?ftpPort);//?連接FTP服務器

ftpClient.login(ftpUserName,?ftpPassword);//?登陸FTP服務器

if?(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))?{

logger.error("未連接到FTP,用戶名或密碼錯誤!");

ftpClient.disconnect();

return?null;

}?else?{

logger.info("FTP連接成功!");

return?ftpClient;

}

}?catch?(SocketException?socketException)?{

logger.error("FTP的IP地址可能錯誤,請正確配置!");

throw?socketException;

}?catch?(IOException?ioException)?{

logger.error("FTP的端口錯誤,請正確配置!");

throw?ioException;

}

}

怎么用Java實現FTP上傳

sun.net..,該類庫主要提供了用于建立FTP連接的類。利用這些類的方法,編程人員可以遠程登錄到FTP服務器,列舉該服務器上的目錄,設置傳輸協議,以及傳送文件。FtpClient類涵蓋了幾乎所有FTP的功能,FtpClient的實例變量保存了有關建立"代理"的各種信息。下面給出了這些實例變量:

public static boolean useFtpProxy

這個變量用于表明FTP傳輸過程中是否使用了一個代理,因此,它實際上是一個標記,此標記若為TRUE,表明使用了一個代理主機。

public static String ftpProxyHost

此變量只有在變量useFtpProxy為TRUE時才有效,用于保存代理主機名。

public static int ftpProxyPort此變量只有在變量useFtpProxy為TRUE時才有效,用于保存代理主機的端口地址。

FtpClient有三種不同形式的構造函數,如下所示:

1、public FtpClient(String hostname,int port)

 此構造函數利用給出的主機名和端口號建立一條FTP連接。

2、public FtpClient(String hostname)

此構造函數利用給出的主機名建立一條FTP連接,使用默認端口號。

3、FtpClient()

此構造函數將創建一FtpClient類,但不建立FTP連接。這時,FTP連接可以用openServer方法建立。

一旦建立了類FtpClient,就可以用這個類的方法來打開與FTP服務器的連接。類ftpClient提供了如下兩個可用于打開與FTP服務器之間的連接的方法。

public void openServer(String hostname)

這個方法用于建立一條與指定主機上的FTP服務器的連接,使用默認端口號。

public void openServer(String host,int port)

這個方法用于建立一條與指定主機、指定端口上的FTP服務器的連接。

打開連接之后,接下來的工作是注冊到FTP服務器。這時需要利用下面的方法。

public void login(String username,String password)

此方法利用參數username和password登錄到FTP服務器。使用過Intemet的用戶應該知道,匿名FTP服務器的登錄用戶名為anonymous,密碼一般用自己的電子郵件地址。

下面是FtpClient類所提供的一些控制命令。

public void cd(String remoteDirectory):該命令用于把遠程系統上的目錄切換到參數remoteDirectory所指定的目錄。

public void cdUp():該命令用于把遠程系統上的目錄切換到上一級目錄。

public String pwd():該命令可顯示遠程系統上的目錄狀態。

public void binary():該命令可把傳輸格式設置為二進制格式。

public void ascii():該命令可把傳輸協議設置為ASCII碼格式。

public void rename(String string,String string1):該命令可對遠程系統上的目錄或者文件進行重命名操作。

除了上述方法外,類FtpClient還提供了可用于傳遞并檢索目錄清單和文件的若干方法。這些方法返回的是可供讀或寫的輸入、輸出流。下面是其中一些主要的方法。

public TelnetInputStream list()

返回與遠程機器上當前目錄相對應的輸入流。

public TelnetInputStream get(String filename)

獲取遠程機器上的文件filename,借助TelnetInputStream把該文件傳送到本地。

public TelnetOutputStream put(String filename)

以寫方式打開一輸出流,通過這一輸出流把文件filename傳送到遠程計算機

package myUtil;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;

import sun.net.TelnetInputStream;

import sun.net.TelnetOutputStream;

import sun.net.;

/**

* ftp上傳,下載

*

* @author why 2009-07-30

*

*/

public class FtpUtil {

private String ip = "";

private String username = "";

private String password = "";

private int port = -1;

private String path = "";

FtpClient ftpClient = null;

OutputStream os = null;

FileInputStream is = null;

public FtpUtil(String serverIP, String username, String password) {

this.ip = serverIP;

this.username = username;

this.password = password;

}

public FtpUtil(String serverIP, int port, String username, String password) {

this.ip = serverIP;

this.username = username;

this.password = password;

this.port = port;

}

/**

* 連接ftp服務器

*

* @throws IOException

*/

public boolean connectServer() {

ftpClient = new FtpClient();

try {

if (this.port != -1) {

ftpClient.openServer(this.ip, this.port);

} else {

ftpClient.openServer(this.ip);

}

ftpClient.login(this.username, this.password);

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

ftpClient.cd(this.path);// path是ftp服務下主目錄的子目錄

}

ftpClient.binary();// 用2進制上傳、下載

System.out.println("已登錄到\"" + ftpClient.pwd() + "\"目錄");

return true;

} catch (IOException e) {

e.printStackTrace();

return false;

}

}

/**

* 斷開與ftp服務器連接

*

* @throws IOException

*/

public boolean closeServer() {

try {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

if (ftpClient != null) {

ftpClient.closeServer();

}

System.out.println("已從服務器斷開");

return true;

} catch (IOException e) {

e.printStackTrace();

return false;

}

}

/**

* 檢查文件夾在當前目錄下是否存在

*

* @param dir

*@return

*/

private boolean isDirExist(String dir) {

String pwd = "";

try {

pwd = ftpClient.pwd();

ftpClient.cd(dir);

ftpClient.cd(pwd);

} catch (Exception e) {

return false;

}

return true;

}

/**

* 在當前目錄下創建文件夾

*

* @param dir

* @return

* @throws Exception

*/

private boolean createDir(String dir) {

try {

ftpClient.ascii();

StringTokenizer s = new StringTokenizer(dir, "/"); // sign

s.countTokens();

String pathName = ftpClient.pwd();

while (s.hasMoreElements()) {

pathName = pathName + "/" + (String) s.nextElement();

try {

ftpClient.sendServer("MKD " + pathName + "\r\n");

} catch (Exception e) {

e = null;

return false;

}

ftpClient.readServerResponse();

}

ftpClient.binary();

return true;

} catch (IOException e1) {

e1.printStackTrace();

return false;

}

}

/**

* ftp上傳 如果服務器段已存在名為filename的文件夾,該文件夾中與要上傳的文件夾中同名的文件將被替換

*

* @param filename

* 要上傳的文件(或文件夾)名

* @return

* @throws Exception

*/

public boolean upload(String filename) {

String newname = "";

if (filename.indexOf("/") -1) {

newname = filename.substring(filename.lastIndexOf("/") + 1);

} else {

newname = filename;

}

return upload(filename, newname);

}

/**

* ftp上傳 如果服務器段已存在名為newName的文件夾,該文件夾中與要上傳的文件夾中同名的文件將被替換

*

* @param fileName

* 要上傳的文件(或文件夾)名

* @param newName

* 服務器段要生成的文件(或文件夾)名

* @return

*/

public boolean upload(String fileName, String newName) {

try {

String savefilename = new String(fileName.getBytes("GBK"),

"GBK");

File file_in = new File(savefilename);// 打開本地待長傳的文件

if (!file_in.exists()) {

throw new Exception("此文件或文件夾[" + file_in.getName() + "]有誤或不存在!");

}

if (file_in.isDirectory()) {

upload(file_in.getPath(), newName, ftpClient.pwd());

} else {

uploadFile(file_in.getPath(), newName);

}

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

return true;

} catch (Exception e) {

e.printStackTrace();

System.err.println("Exception e in Ftp upload(): " + e.toString());

return false;

} finally {

try {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 真正用于上傳的方法

*

* @param fileName

* @param newName

* @param path

* @throws Exception

*/

private void upload(String fileName, String newName, String path)

throws Exception {

String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");

File file_in = new File(savefilename);// 打開本地待長傳的文件

if (!file_in.exists()) {

throw new Exception("此文件或文件夾[" + file_in.getName() + "]有誤或不存在!");

}

if (file_in.isDirectory()) {

if (!isDirExist(newName)) {

createDir(newName);

}

ftpClient.cd(newName);

File sourceFile[] = file_in.listFiles();

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

if (!sourceFile[i].exists()) {

continue;

}

if (sourceFile[i].isDirectory()) {

this.upload(sourceFile[i].getPath(), sourceFile[i]

.getName(), path + "/" + newName);

} else {

this.uploadFile(sourceFile[i].getPath(), sourceFile[i]

.getName());

}

}

} else {

uploadFile(file_in.getPath(), newName);

}

ftpClient.cd(path);

}

/**

* upload 上傳文件

*

* @param filename

* 要上傳的文件名

* @param newname

* 上傳后的新文件名

* @return -1 文件不存在 =0 成功上傳,返回文件的大小

* @throws Exception

*/

public long uploadFile(String filename, String newname) throws Exception {

long result = 0;

TelnetOutputStream os = null;

FileInputStream is = null;

try {

java.io.File file_in = new java.io.File(filename);

if (!file_in.exists())

return -1;

os = ftpClient.put(newname);

result = file_in.length();

is = new FileInputStream(file_in);

byte[] bytes = new byte[1024];

int c;

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

os.write(bytes, 0, c);

}

} finally {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

}

return result;

}

/**

* 從ftp下載文件到本地

*

* @param filename

* 服務器上的文件名

* @param newfilename

* 本地生成的文件名

* @return

* @throws Exception

*/

public long downloadFile(String filename, String newfilename) {

long result = 0;

TelnetInputStream is = null;

FileOutputStream os = null;

try {

is = ftpClient.get(filename);

java.io.File outfile = new java.io.File(newfilename);

os = new FileOutputStream(outfile);

byte[] bytes = new byte[1024];

int c;

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

os.write(bytes, 0, c);

result = result + c;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (is != null) {

is.close();

}

if (os != null) {

os.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return result;

}

/**

* 取得相對于當前連接目錄的某個目錄下所有文件列表

*

* @param path

* @return

*/

public List getFileList(String path) {

List list = new ArrayList();

DataInputStream dis;

try {

dis = new DataInputStream(ftpClient.nameList(this.path + path));

String filename = "";

while ((filename = dis.readLine()) != null) {

list.add(filename);

}

} catch (IOException e) {

e.printStackTrace();

}

return list;

}

public static void main(String[] args) {

FtpUtil ftp = new FtpUtil("192.168.11.11", "111", "1111");

;

boolean result = ("C:/Documents and Settings/ipanel/桌面/java/Hibernate_HQL.docx", "amuse/audioTest/music/Hibernate_HQL.docx");

System.out.println(result ? "上傳成功!" : "上傳失敗!");

;

/**

* FTP遠程命令列表 USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT PASS PASV STOR

* REST CWD STAT RMD XCUP OPTS ACCT TYPE APPE RNFR XCWD HELP XRMD STOU

* AUTH REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ QUIT MODE SYST ABOR

* NLST MKD XPWD MDTM PROT

* 在服務器上執行命令,如果用sendServer來執行遠程命令(不能執行本地FTP命令)的話,所有FTP命令都要加上\r\n

* ftpclient.sendServer("XMKD /test/bb\r\n"); //執行服務器上的FTP命令

* ftpclient.readServerResponse一定要在sendServer后調用

* nameList("/test")獲取指目錄下的文件列表 XMKD建立目錄,當目錄存在的情況下再次創建目錄時報錯 XRMD刪除目錄

* DELE刪除文件

*/

}

}

當前文章:java代碼編寫ftp,java代碼編寫四則運算計算器
文章源于:http://www.2m8n56k.cn/article24/hcecje.html

成都網站建設公司_創新互聯,為您提供動態網站軟件開發網站維護自適應網站云服務器微信公眾號

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都做網站
主站蜘蛛池模板: 中文在线亚洲 | 久久91精品国产99久久yfo | 手机看片福利永久 | 亚洲一级理论片 | 欧美另类高清xxxxx | 免费观看a毛片一区二区不卡 | 亚洲国产精品aaa一区 | 亚洲成人免费视频在线 | 亚洲精品天堂一区在线观看 | 日本二级毛片免费 | 欧美三级做爰视频 | 亚洲欧美高清在线 | 国产成人精品福利网站人 | 成年女人免费视频播放成年m | 国产一区二区三区在线观看精品 | 91精品国产91热久久p | 亚洲欧美激情在线 | 亚洲欧美日韩另类精品一区二区三区 | 亚洲午夜网 | 男女猛烈无遮掩免费视频 | 成人国产在线观看 | 男人女人做黄刺激性视频免费 | 九九国产精品视频 | 日韩欧美在线精品 | 高清一级毛片免免费看 | 欧美日本亚洲国产一区二区 | 毛片手机在线 | 久久久久久久99精品免费 | 97欧美精品一区二区三区 | 精品毛片免费看 | 人碰人碰人成人免费视频 | 免费观看大片毛片 | 久久成人国产精品 | 久久免费国产视频 | 欧美日韩在线观看视频 | 欧美精品99久久久久久人 | 高清波多野结衣一区二区三区 | 美女视频永久黄网站免费观看国产 | 国产亚洲精品久久久久久午夜 | 一级特黄牲大片免费视频 | 色欧美与xxxxx |