欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

java代碼獲取ftp java代碼獲取今天星期幾

用java怎么獲取ftp上的文件

public class FtpClientUtil {

為企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站優(yōu)化、營(yíng)銷型網(wǎng)站、競(jìng)價(jià)托管、品牌運(yùn)營(yíng)等營(yíng)銷獲客服務(wù)。成都創(chuàng)新互聯(lián)擁有網(wǎng)絡(luò)營(yíng)銷運(yùn)營(yíng)團(tuán)隊(duì),以豐富的互聯(lián)網(wǎng)營(yíng)銷經(jīng)驗(yàn)助力企業(yè)精準(zhǔn)獲客,真正落地解決中小企業(yè)營(yíng)銷獲客難題,做到“讓獲客更簡(jiǎn)單”。自創(chuàng)立至今,成功用技術(shù)實(shí)力解決了企業(yè)“網(wǎng)站建設(shè)、網(wǎng)絡(luò)品牌塑造、網(wǎng)絡(luò)營(yíng)銷”三大難題,同時(shí)降低了營(yíng)銷成本,提高了有效客戶轉(zhuǎn)化率,獲得了眾多企業(yè)客戶的高度認(rèn)可!

FtpClient ftpClient;

private String server;

private int port;

private String userName;

private String userPassword;

public FtpClientUtil(String server,int port,String userName,String userPassword)

{

this.server=server;

this.port=port;

this.userName=userName;

this.userPassword=userPassword;

}

/**

* 鏈接到服務(wù)器

* @return

*/

public boolean open()

{

if(ftpClient!=nullftpClient.serverIsOpen())

return true;

try

{

ftpClient= new FtpClient();

ftpClient.openServer(server,port);

ftpClient.login(userName, userPassword);

ftpClient.binary();

return true;

}

catch(Exception e)

{

e.printStackTrace();

ftpClient=null;

return false;

}

}

public boolean cd(String dir){

boolean f = false;

try {

ftpClient.cd(dir);

} catch (IOException e) {

Logs.error(e.toString());

return f;

}

return true;

}

/**

* 上傳文件到FTP服務(wù)器

* @param localPathAndFileName 本地文件目錄和文件名

* @param ftpFileName 上傳后的文件名

* @param ftpDirectory FTP目錄如:/path1/pathb2/,如果目錄不存在回自動(dòng)創(chuàng)建目錄

* @throws Exception

*/

public boolean upload(String localDirectoryAndFileName,String ftpFileName,String ftpDirectory)throws Exception {

if(!open())

return false;

FileInputStream is=null;

TelnetOutputStream os=null;

try

{

char ch = ' ';

if (ftpDirectory.length() 0)

ch = ftpDirectory.charAt(ftpDirectory.length() - 1);

for (; ch == '/' || ch == '\\'; ch = ftpDirectory.charAt(ftpDirectory.length() - 1))

ftpDirectory = ftpDirectory.substring(0, ftpDirectory.length() - 1);

int slashIndex = ftpDirectory.indexOf(47);

int backslashIndex = ftpDirectory.indexOf(92);

int index = slashIndex;

String dirall = ftpDirectory;

if (backslashIndex != -1 (index == -1 || index backslashIndex))

index = backslashIndex;

String directory = "";

while (index != -1) {

if (index 0) {

String dir = dirall.substring(0, index);

directory = directory + "/" + dir;

ftpClient.sendServer("XMKD " + directory + "\r\n");

ftpClient.readServerResponse();

}

dirall = dirall.substring(index + 1);

slashIndex = dirall.indexOf(47);

backslashIndex = dirall.indexOf(92);

index = slashIndex;

if (backslashIndex != -1 (index == -1 || index backslashIndex))

index = backslashIndex;

}

ftpClient.sendServer("XMKD " + ftpDirectory + "\r\n");

ftpClient.readServerResponse();

os = ftpClient.put(ftpDirectory + "/"

+ ftpFileName);

File file_in = new File(localDirectoryAndFileName);

is = new FileInputStream(file_in);

byte bytes[] = new byte[1024];

int i;

while ((i = is.read(bytes)) != -1)

os.write(bytes, 0, i);

//清理垃圾

return true;

}

catch(Exception e)

{

e.printStackTrace();

return false;

}

finally

{

if (is != null)

is.close();

if (os != null)

os.close();

}

}

/**

* 從FTP服務(wù)器上下載文件并返回下載文件長(zhǎng)度

* @param ftpDirectoryAndFileName

* @param localDirectoryAndFileName

* @return

* @throws Exception

*/

public long download(String ftpDirectoryAndFileName,String localDirectoryAndFileName)throws Exception

{

long result = 0;

if(!open())

return result;

TelnetInputStream is = null;

FileOutputStream os = null;

try

{

is = ftpClient.get(ftpDirectoryAndFileName);

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

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 (Exception e)

{

throw e;

}

finally

{

if (is != null)

is.close();

if (os != null)

os.close();

}

return result;

}

/**

* 返回FTP目錄下的文件列表

* @param ftpDirectory

* @return

*/

public ListString getFileNameList(String ftpDirectory)

{

ListString list = new ArrayListString();

if(!open())

return list;

try

{

DataInputStream dis = new DataInputStream(ftpClient.nameList(ftpDirectory));

String filename = "";

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

{

list.add(filename);

}

} catch (Exception e)

{

e.printStackTrace();

}

return list;

}

/**

* 刪除FTP上的文件

* @param ftpDirAndFileName

*/

public boolean deleteFile(String ftpDirAndFileName)

{

if(!open())

return false;

ftpClient.sendServer("DELE "+ftpDirAndFileName+"\r\n");

return true;

}

/**

* 刪除FTP目錄

* @param ftpDirectory

*/

public boolean deleteDirectory(String ftpDirectory)

{

if(!open())

return false;

ftpClient.sendServer("XRMD "+ftpDirectory+"\r\n");

return true;

}

/**

* 關(guān)閉鏈接

*/

public void close()

{

try

{

if(ftpClient!=nullftpClient.serverIsOpen())

ftpClient.closeServer();

}catch(Exception e)

{

}

}

}望采納,謝謝。

java在瀏覽器上獲取FTP讀文件路徑

問(wèn)一下,你是想做ftp上傳下載么?

首先你需要安裝一個(gè)ftp服務(wù)端程序,啟動(dòng)起來(lái),然后下載一個(gè)ftp客戶端程序,測(cè)試能不能連接,首先這一塊兒需要測(cè)試通過(guò)。

代碼ftp上傳下載

2.1 上傳代碼:

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

FileInputStream input = new FileInputStream(file2);

(file2.getName(), input);

input.close();

}

}

}else{

File file2 = new File(file.getPath());

FileInputStream input = new FileInputStream(file2);

(file2.getName(), input);

input.close();

}

}

public static void main(String[] args) throws Exception{

test t = new test();

t.connect("", "localhost", 21, "yhh", "yhhazr");

File file = new File("e:\\uploadify");

t.upload(file);

}

}

2.2 下載代碼

這里沒(méi)有用到filter,如果用filter就可以過(guò)濾想要的文件。

public class Ftp {

/**

* @param args

*/

public static void main(String[] args) {

? ?// TODO Auto-generated method stub

? ?Ftp ftp = new Ftp();

? ?String hostname = "";

? ?Integer port = 21;

? ?String username = "username";

? ?String password = "password";

? ?String remote = "/c.txt";

? ?String local = "/home/tin/LeonChen/FTP/";

? ?try {

? ? ? ?(hostname, port, username, password);

? ? ? ?System.out.println("接收狀態(tài):"+(remote, local));

? ? ? ?;

? ?} catch (IOException e) {

? ? ? ?// TODO Auto-generated catch block

? ? ? ?e.printStackTrace();

? ?}

}

private FTPClient ftpClient = new FTPClient();

/*

* * 連接到FTP服務(wù)器 ?

* * @param hostname 主機(jī)名 ?

* * @param port 端口 ?

* * @param username 用戶名 ?

* * @param password 密碼 ?

* * @return 是否連接成功

* * @throws IOException

*/

private boolean connect(String hostname, int port, String username,

? ? ? ?String password) throws IOException {

? ?ftpClient.connect(hostname, port);

? ?ftpClient.setControlEncoding("UTF-8");

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

? ? ? ?if (ftpClient.login(username, password)) {

? ? ? ? ? ?return true;

? ? ? ?}

? ?}

? ?disconnect();

? ?return false;

}

/*

* 從FTP服務(wù)器上下載文件,支持?jǐn)帱c(diǎn)續(xù)傳,上傳百分比匯報(bào)

*

* @param remote 遠(yuǎn)程文件路徑

*

* @param local 本地文件路徑

*

* @return 上傳的狀態(tài)

*

* @throws IOException

*/

public DownloadStatus download(String remote, String local)

? ? ? ?throws IOException {

? ?// 設(shè)置被動(dòng)模式

? ?ftpClient.enterLocalPassiveMode();

? ?// 設(shè)置以二進(jìn)制方式傳輸

? ?ftpClient.setFileType();

? ?DownloadStatus result;

? ?// 檢查遠(yuǎn)程文件是否存在

? ?FTPFile[] files = ftpClient.listFiles(new String(remote

? ? ? ? ? ?.getBytes("UTF-8"), "iso-8859-1"));

? ?if (files.length != 1) {

? ? ? ?System.out.println("遠(yuǎn)程文件不存在");

? ? ? ?return DownloadStatus.Remote_File_Noexist;

? ?}

? ?long lRemoteSize = files[0].getSize();

? ?String fildName = files[0].getName();

? ?// 本地存在文件,進(jìn)行斷點(diǎn)下載

? ?File f = new File(local+fildName);

? ?if (f.exists()) {

? ? ? ?long localSize = f.length();

? ? ? ?if (localSize = lRemoteSize) {

? ? ? ? ? ?System.out.println("本地文件大于遠(yuǎn)程文件,下載中止");

? ? ? ? ? ?return DownloadStatus.Local_Bigger_Remote;

? ? ? ?}

? ? ? ?// 進(jìn)行斷點(diǎn)續(xù)傳,并記錄狀態(tài)

? ? ? ?FileOutputStream out = new FileOutputStream(f, true);

? ? ? ?ftpClient.setRestartOffset(localSize);

? ? ? ?InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("UTF-8"), "iso-8859-1"));

? ? ? ?byte[] bytes = new byte[1024];

? ? ? ?long step = lRemoteSize / 100;

? ? ? ?long process = localSize / step;

? ? ? ?int c;

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

? ? ? ? ? ?out.write(bytes, 0, c);

? ? ? ? ? ?localSize += c;

? ? ? ? ? ?long nowProcess = localSize / step;

? ? ? ? ? ?if (nowProcess process) {

? ? ? ? ? ? ? ?process = nowProcess;

? ? ? ? ? ? ? ?if (process % 10 == 0)

? ? ? ? ? ? ? ? ? ?System.out.println("下載進(jìn)度:" + process);

? ? ? ? ? ? ? ?// TODO 更新文件下載進(jìn)度,值存放在process變量中

? ? ? ? ? ?}

? ? ? ?}

? ? ? ?in.close();

? ? ? ?out.close();

? ? ? ?boolean isDo = ftpClient.completePendingCommand();

? ? ? ?if (isDo) {

? ? ? ? ? ?result = DownloadStatus.Download_From_Break_Success;

? ? ? ?} else {

? ? ? ? ? ?result = DownloadStatus.Download_From_Break_Failed;

? ? ? ?}

? ? } else {

? ? ? ?OutputStream out = new FileOutputStream(f);

? ? ? ?InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("UTF-8"), "iso-8859-1"));

? ? ? ?byte[] bytes = new byte[1024];

? ? ? ?long step = lRemoteSize / 100;

? ? ? ?long process = 0;

? ? ? ?long localSize = 0L;

? ? ? ?int c;

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

? ? ? ? ? ?out.write(bytes, 0, c);

? ? ? ? ? ?localSize += c;

? ? ? ? ? ?long nowProcess = localSize / step;

? ? ? ? ? ?if (nowProcess process) {

? ? ? ? ? ? ? ?process = nowProcess;

? ? ? ? ? ? ? ?if (process % 10 == 0)

? ? ? ? ? ? ? ? ? ?System.out.println("下載進(jìn)度:" + process);

? ? ? ? ? ? ? ?// TODO 更新文件下載進(jìn)度,值存放在process變量中

? ? ? ? ? ?}

? ? ? ?}

? ? ? ?in.close();

? ? ? ?out.close();

? ? ? ?boolean upNewStatus = ftpClient.completePendingCommand();

? ? ? ?if (upNewStatus) {

? ? ? ? ? ?result = DownloadStatus.Download_New_Success;

? ? ? ?} else {

? ? ? ? ? ?result = DownloadStatus.Download_New_Failed;

? ? ? ?}

? ?}

? ?return result;

}

private void disconnect() throws IOException {

? ?if (ftpClient.isConnected()) {

? ? ? ?ftpClient.disconnect();

? ?}

}

}

java獲取ftp文件路徑怎么寫

package com.weixin.util;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.io.RandomAccessFile;

import org.apache.commons.net.PrintCommandListener;

import org.apache.commons.net.;

import org.apache.commons.net.;

import org.apache.commons.net.;

import org.apache.commons.net.;

import com.weixin.constant.DownloadStatus;

import com.weixin.constant.UploadStatus;

/**

* 支持?jǐn)帱c(diǎn)續(xù)傳的FTP實(shí)用類

* @version 0.1 實(shí)現(xiàn)基本斷點(diǎn)上傳下載

* @version 0.2 實(shí)現(xiàn)上傳下載進(jìn)度匯報(bào)

* @version 0.3 實(shí)現(xiàn)中文目錄創(chuàng)建及中文文件創(chuàng)建,添加對(duì)于中文的支持

*/

public class ContinueFTP {

public FTPClient ftpClient = new FTPClient();

public ContinueFTP(){

//設(shè)置將過(guò)程中使用到的命令輸出到控制臺(tái)

this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

}

/**

* 連接到FTP服務(wù)器

* @param hostname 主機(jī)名

* @param port 端口

* @param username 用戶名

* @param password 密碼

* @return 是否連接成功

* @throws IOException

*/

public boolean connect(String hostname,int port,String username,String password) throws IOException{

ftpClient.connect(hostname, port);

ftpClient.setControlEncoding("GBK");

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

if(ftpClient.login(username, password)){

return true;

}

}

disconnect();

return false;

}

/**

* 從FTP服務(wù)器上下載文件,支持?jǐn)帱c(diǎn)續(xù)傳,上傳百分比匯報(bào)

* @param remote 遠(yuǎn)程文件路徑

* @param local 本地文件路徑

* @return 上傳的狀態(tài)

* @throws IOException

*/

public DownloadStatus download(String remote,String local) throws IOException{

//設(shè)置被動(dòng)模式

ftpClient.enterLocalPassiveMode();

//設(shè)置以二進(jìn)制方式傳輸

ftpClient.setFileType();

DownloadStatus result;

//檢查遠(yuǎn)程文件是否存在

FTPFile[] files = ftpClient.listFiles(new String(remote.getBytes("GBK"),"iso-8859-1"));

if(files.length != 1){

System.out.println("遠(yuǎn)程文件不存在");

return DownloadStatus.Remote_File_Noexist;

}

long lRemoteSize = files[0].getSize();

File f = new File(local);

//本地存在文件,進(jìn)行斷點(diǎn)下載

if(f.exists()){

long localSize = f.length();

//判斷本地文件大小是否大于遠(yuǎn)程文件大小

if(localSize = lRemoteSize){

System.out.println("本地文件大于遠(yuǎn)程文件,下載中止");

return DownloadStatus.Local_Bigger_Remote;

}

//進(jìn)行斷點(diǎn)續(xù)傳,并記錄狀態(tài)

FileOutputStream out = new FileOutputStream(f,true);

ftpClient.setRestartOffset(localSize);

InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));

byte[] bytes = new byte[1024];

long step = lRemoteSize /100;

long process=localSize /step;

int c;

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

out.write(bytes,0,c);

localSize+=c;

long nowProcess = localSize /step;

if(nowProcess process){

process = nowProcess;

if(process % 10 == 0)

System.out.println("下載進(jìn)度:"+process);

//TODO 更新文件下載進(jìn)度,值存放在process變量中

}

}

in.close();

out.close();

boolean isDo = ftpClient.completePendingCommand();

if(isDo){

result = DownloadStatus.Download_From_Break_Success;

}else {

result = DownloadStatus.Download_From_Break_Failed;

}

}else {

OutputStream out = new FileOutputStream(f);

InputStream in= ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));

byte[] bytes = new byte[1024];

long step = lRemoteSize /100;

long process=0;

long localSize = 0L;

int c;

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

out.write(bytes, 0, c);

localSize+=c;

long nowProcess = localSize /step;

if(nowProcess process){

process = nowProcess;

if(process % 10 == 0)

System.out.println("下載進(jìn)度:"+process);

//TODO 更新文件下載進(jìn)度,值存放在process變量中

}

}

in.close();

out.close();

boolean upNewStatus = ftpClient.completePendingCommand();

if(upNewStatus){

result = DownloadStatus.Download_New_Success;

}else {

result = DownloadStatus.Download_New_Failed;

}

}

return result;

}

/**

* 上傳文件到FTP服務(wù)器,支持?jǐn)帱c(diǎn)續(xù)傳

* @param local 本地文件名稱,絕對(duì)路徑

* @param remote 遠(yuǎn)程文件路徑,使用/home/directory1/subdirectory/file.ext 按照Linux上的路徑指定方式,支持多級(jí)目錄嵌套,支持遞歸創(chuàng)建不存在的目錄結(jié)構(gòu)

* @return 上傳結(jié)果

* @throws IOException

*/

public UploadStatus upload(String local,String remote) throws IOException{

//設(shè)置PassiveMode傳輸

ftpClient.enterLocalPassiveMode();

//設(shè)置以二進(jìn)制流的方式傳輸

ftpClient.setFileType();

ftpClient.setControlEncoding("GBK");

UploadStatus result;

//對(duì)遠(yuǎn)程目錄的處理

String remoteFileName = remote;

if(remote.contains("/")){

remoteFileName = remote.substring(remote.lastIndexOf("/")+1);

//創(chuàng)建服務(wù)器遠(yuǎn)程目錄結(jié)構(gòu),創(chuàng)建失敗直接返回

if(CreateDirecroty(remote, ftpClient)==UploadStatus.Create_Directory_Fail){

return UploadStatus.Create_Directory_Fail;

}

}

//檢查遠(yuǎn)程是否存在文件

FTPFile[] files = ftpClient.listFiles(new String(remoteFileName.getBytes("GBK"),"iso-8859-1"));

if(files.length == 1){

long remoteSize = files[0].getSize();

File f = new File(local);

long localSize = f.length();

if(remoteSize==localSize){

return UploadStatus.File_Exits;

}else if(remoteSize localSize){

return UploadStatus.Remote_Bigger_Local;

}

//嘗試移動(dòng)文件內(nèi)讀取指針,實(shí)現(xiàn)斷點(diǎn)續(xù)傳

result = uploadFile(remoteFileName, f, ftpClient, remoteSize);

//如果斷點(diǎn)續(xù)傳沒(méi)有成功,則刪除服務(wù)器上文件,重新上傳

if(result == UploadStatus.Upload_From_Break_Failed){

if(!ftpClient.deleteFile(remoteFileName)){

return UploadStatus.Delete_Remote_Faild;

}

result = uploadFile(remoteFileName, f, ftpClient, 0);

}

}else {

result = uploadFile(remoteFileName, new File(local), ftpClient, 0);

}

return result;

}

/**

* 斷開(kāi)與遠(yuǎn)程服務(wù)器的連接

* @throws IOException

*/

public void disconnect() throws IOException{

if(ftpClient.isConnected()){

ftpClient.disconnect();

}

}

/**

* 遞歸創(chuàng)建遠(yuǎn)程服務(wù)器目錄

* @param remote 遠(yuǎn)程服務(wù)器文件絕對(duì)路徑

* @param ftpClient FTPClient對(duì)象

* @return 目錄創(chuàng)建是否成功

* @throws IOException

*/

public UploadStatus CreateDirecroty(String remote,FTPClient ftpClient) throws IOException{

UploadStatus status = UploadStatus.Create_Directory_Success;

String directory = remote.substring(0,remote.lastIndexOf("/")+1);

if(!directory.equalsIgnoreCase("/")!ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"),"iso-8859-1"))){

//如果遠(yuǎn)程目錄不存在,則遞歸創(chuàng)建遠(yuǎn)程服務(wù)器目錄

int start=0;

int end = 0;

if(directory.startsWith("/")){

start = 1;

}else{

start = 0;

}

end = directory.indexOf("/",start);

while(true){

String subDirectory = new String(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");

if(!ftpClient.changeWorkingDirectory(subDirectory)){

if(ftpClient.makeDirectory(subDirectory)){

ftpClient.changeWorkingDirectory(subDirectory);

}else {

System.out.println("創(chuàng)建目錄失敗");

return UploadStatus.Create_Directory_Fail;

}

}

start = end + 1;

end = directory.indexOf("/",start);

//檢查所有目錄是否創(chuàng)建完畢

if(end = start){

break;

}

}

}

return status;

}

/**

* 上傳文件到服務(wù)器,新上傳和斷點(diǎn)續(xù)傳

* @param remoteFile 遠(yuǎn)程文件名,在上傳之前已經(jīng)將服務(wù)器工作目錄做了改變

* @param localFile 本地文件File句柄,絕對(duì)路徑

* @param processStep 需要顯示的處理進(jìn)度步進(jìn)值

* @param ftpClient FTPClient引用

* @return

* @throws IOException

*/

public UploadStatus uploadFile(String remoteFile,File localFile,FTPClient ftpClient,long remoteSize) throws IOException{

UploadStatus status;

//顯示進(jìn)度的上傳

long step = localFile.length() / 100;

long process = 0;

long localreadbytes = 0L;

RandomAccessFile raf = new RandomAccessFile(localFile,"r");

OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"),"iso-8859-1"));

//斷點(diǎn)續(xù)傳

if(remoteSize0){

ftpClient.setRestartOffset(remoteSize);

process = remoteSize /step;

raf.seek(remoteSize);

localreadbytes = remoteSize;

}

byte[] bytes = new byte[1024];

int c;

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

out.write(bytes,0,c);

localreadbytes+=c;

if(localreadbytes / step != process){

process = localreadbytes / step;

System.out.println("上傳進(jìn)度:" + process);

//TODO 匯報(bào)上傳狀態(tài)

}

}

out.flush();

raf.close();

out.close();

boolean result =ftpClient.completePendingCommand();

if(remoteSize 0){

status = result?UploadStatus.Upload_From_Break_Success:UploadStatus.Upload_From_Break_Failed;

}else {

status = result?UploadStatus.Upload_New_File_Success:UploadStatus.Upload_New_File_Failed;

}

return status;

}

public static void main(String[] args) {

ContinueFTP myFtp = new ContinueFTP();

try {

System.err.println(my("10.10.6.236", 21, "5", "jieyan"));

// my(new String("歌曲".getBytes("GBK"),"iso-8859-1"));

// my(new String("歌曲".getBytes("GBK"),"iso-8859-1"));

// my(new String("愛(ài)你等于愛(ài)自己".getBytes("GBK"),"iso-8859-1"));

// System.out.println(my("E:\\yw.flv", "/yw.flv",5));

// System.out.println(my("E:\\愛(ài)你等于愛(ài)自己.mp4","/愛(ài)你等于愛(ài)自己.mp4"));

//System.out.println(my("/愛(ài)你等于愛(ài)自己.mp4", "E:\\愛(ài)你等于愛(ài)自己.mp4"));

my;

} catch (IOException e) {

System.out.println("連接FTP出錯(cuò):"+e.getMessage());

}

}

}

如何用JAVA實(shí)現(xiàn)FTP訪問(wèn)

在主函數(shù)中,完成服務(wù)器端口的偵聽(tīng)和服務(wù)線程的創(chuàng)建。我們利用一個(gè)靜態(tài)字符串變量initDir 來(lái)保存服務(wù)器線程運(yùn)行時(shí)所在的工作目錄。服務(wù)器的初始工作目錄是由程序運(yùn)行時(shí)用戶輸入的,缺省為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!");

//監(jiān)聽(tīng)21號(hào)端口

ServerSocket s = new ServerSocket(21);

for(;;){

//接受客戶端請(qǐng)求

Socket incoming = s.accept();

//創(chuàng)建服務(wù)線程

new ftpServer(incoming,i).start();

i++;

}

}catch(Exception e){}

}

java 獲取ftp文件的最后修改時(shí)間比實(shí)際時(shí)間少8小時(shí),該怎么處理

org.apache.commons.net.;

java 獲取ftp文件的最后修改時(shí)間比實(shí)際時(shí)間少了8小時(shí)代碼如下:FTPFile[] files = ftpClient.listFiles();

for (FTPFile file : files){

System.out.println(file.getName());

Date date = file.getTimestamp().getTime();

System.out.println(文件修改 + dateFormat.format(date));

Date date1 = new Date();

System.out.println(now + dateFormat.format(date1));

long f = date1.getTime() -date.getTime();

System.out.println(時(shí)間差 + f/60000+分);}------解決方案--------------------------------------------------------

FTPFile.getTimestamp().getTime()

java.io.File.lastModified()

java中怎么實(shí)現(xiàn)ftp服務(wù)器

學(xué)習(xí)了計(jì)算機(jī)網(wǎng)絡(luò)之后,利用java寫了一個(gè)ftp服務(wù)器。

一、實(shí)現(xiàn)的ftp命令

實(shí)現(xiàn)了基本的user,pass,list,port,quit,retr,cwd,stor等命令

二、以上命令所對(duì)應(yīng)的功能

對(duì)應(yīng)的功能是:下載,上傳,獲取服務(wù)器目錄,切換目錄等

三、用于測(cè)試的ftp客戶端:windows自帶的ftp客戶端

四、實(shí)現(xiàn)的思想

1、使用ServerSocket進(jìn)行監(jiān)聽(tīng),每個(gè)控制連接的請(qǐng)求到來(lái)之后,開(kāi)啟一個(gè)線程進(jìn)行處理(這里使用的java bio,效率較差,對(duì)于控制連接最好使用NIO處理,之后會(huì)再寫個(gè)

nio的實(shí)現(xiàn))

2、 對(duì)于命令使用工廠方法模式進(jìn)行設(shè)計(jì),當(dāng)需要添加新的命令的時(shí)候,只需要添加一個(gè)新的命令類,實(shí)現(xiàn)相應(yīng)接口,修改工廠產(chǎn)生邏輯,而不用修改其他的程序代碼。可

擴(kuò)展性較好,同時(shí)符合開(kāi)閉原則。

五、實(shí)現(xiàn)過(guò)程中碰到的問(wèn)題

1、對(duì)于tcp與socket的關(guān)系理解錯(cuò)誤,以為所有的數(shù)據(jù)的輸入都是要經(jīng)過(guò)serverSocket().accept()方法。其實(shí),ServerSocket.accept()所對(duì)應(yīng)的是tcp里面的三次握手建

立連接的階段,之后的tcp的連接由客戶端和服務(wù)器端的一對(duì)socket來(lái)維護(hù),是屬于establish階段,在這個(gè)階段,通信是全雙工的,任何一方都能夠發(fā)送數(shù)據(jù)。

socket.close()對(duì)應(yīng)的階段是斷開(kāi)連接(四次揮手)的階段。

2、剛開(kāi)始對(duì)于ftp協(xié)議不是很理解,不知道他的工作方式是怎樣的,后來(lái)在看了tcp協(xié)議卷里面的ftp的內(nèi)容之后,才知道ftp命令和應(yīng)答碼是關(guān)鍵。eg:剛開(kāi)始測(cè)試時(shí),在

輸入用戶名之后,不會(huì)提示輸入密碼的。原因:沒(méi)有返回對(duì)應(yīng)的應(yīng)答碼:331. 另外要注意的是:返回的數(shù)據(jù)要以換行回車作為結(jié)束--\r\n.

六、代碼列表

簡(jiǎn)單說(shuō)明:

ftpServer:是服務(wù)器的主程序,入口,同時(shí)負(fù)責(zé)監(jiān)聽(tīng)本地的21號(hào)端口。

ControllerThread.java:用于處理控制連接的線程(每一個(gè)控制連接請(qǐng)求對(duì)應(yīng)一個(gè)線程)ps:實(shí)在很浪費(fèi)(流量小,連接多)。

Share:一些全局性數(shù)據(jù)的維護(hù)。

Command:是命令接口,定義了一個(gè)所有命令都要實(shí)現(xiàn)的方法。

CommandFactory:命令工廠,通過(guò)傳人的參數(shù),決定生成的命令對(duì)象。

UserCommand,PortCommand等:是具體ftp命令的實(shí)現(xiàn)

分享名稱:java代碼獲取ftp java代碼獲取今天星期幾
鏈接分享:http://chinadenli.net/article42/hpjghc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)網(wǎng)站內(nèi)鏈小程序開(kāi)發(fā)品牌網(wǎng)站制作網(wǎng)站建設(shè)品牌網(wǎng)站建設(shè)

廣告

聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名