A. java实现ftp断点续传问题
//尝试移动文件内读取指针,实现断点续传
result
=
uploadFile(remoteFileName,
f,
ftpClient,
remoteSize);
//如果断点续传没有成功,则删除服务器上文件,重新上传
if(result
==
UploadStatus.Upload_From_Break_Failed){
if(!ftpClient.deleteFile(remoteFileName)){
return
UploadStatus.Delete_Remote_Faild;
}
result
=
uploadFile(remoteFileName,
f,
ftpClient,
0);
}
B. java实现ftp的断点续传问题
//尝试移动文件内读取指针,实现断点续传
result = uploadFile(remoteFileName, f, ftpClient, remoteSize);
//如果断点续传没有成功,则删除服务器上文件,重新上传
if(result == UploadStatus.Upload_From_Break_Failed){
if(!ftpClient.deleteFile(remoteFileName)){
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, f, ftpClient, 0);
}
C. java ftp客户端主动模式(port)下的源代码
维护一个ftp的服务器列表,包含ip端口用户名密码
定时的递归扫描每个ftp服务器的目录和文件,保存到本地数据库
搜索的时候,从本地数据库查询
至于java访问ftp服务器的方法,已经有很多现成的实现
比如www.apache.org的commons-net里就有
有的版本的sun jdk自身就带
D. java如何测试连接ftp是否通
java测试连接ftp是否连通可以使用判断是否有异常来决定,实例如下:
/**
*connectServer
*连接ftp服务器
*@throwsjava.io.IOException
*@parampath文件夹,空代表根目录
*@parampassword密码
*@paramuser登陆用户
*@paramserver服务器地址
*/
publicvoidconnectServer(Stringserver,Stringuser,Stringpassword,Stringpath)
throwsIOException
{
//server:FTP服务器的IP地址;user:登录FTP服务器的用户名
//password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
ftpClient=newFtpClient();
ftpClient.openServer(server);
ftpClient.login(user,password);
//path是ftp服务下主目录的子目录
if(path.length()!=0)ftpClient.cd(path);
//用2进制上传、下载
ftpClient.binary();
}
/**
*upload
*上传文件
*@throwsjava.lang.Exception
*@return-1文件不存在
*-2文件内容为空
*>0成功上传,返回文件的大小
*@paramnewname上传后的新文件名
*@paramfilename上传的文件
*/
publiclongupload(Stringfilename,Stringnewname)throwsException
{
longresult=0;
TelnetOutputStreamos=null;
FileInputStreamis=null;
try{
java.io.Filefile_in=newjava.io.File(filename);
if(!file_in.exists())return-1;
if(file_in.length()==0)return-2;
os=ftpClient.put(newname);
result=file_in.length();
is=newFileInputStream(file_in);
byte[]bytes=newbyte[1024];
intc;
while((c=is.read(bytes))!=-1){
os.write(bytes,0,c);
}
}finally{
if(is!=null){
is.close();
}
if(os!=null){
os.close();
}
}
returnresult;
}
/**
*upload
*@throwsjava.lang.Exception
*@return
*@paramfilename
*/
publiclongupload(Stringfilename)
throwsException
{
Stringnewname="";
if(filename.indexOf("/")>-1)
{
newname=filename.substring(filename.lastIndexOf("/")+1);
}else
{
newname=filename;
}
returnupload(filename,newname);
}
/**
*download
*从ftp下载文件到本地
*@throwsjava.lang.Exception
*@return
*@paramnewfilename本地生成的文件名
*@paramfilename服务器上的文件名
*/
publiclongdownload(Stringfilename,Stringnewfilename)
throwsException
{
longresult=0;
TelnetInputStreamis=null;
FileOutputStreamos=null;
try
{
is=ftpClient.get(filename);
java.io.Fileoutfile=newjava.io.File(newfilename);
os=newFileOutputStream(outfile);
byte[]bytes=newbyte[1024];
intc;
while((c=is.read(bytes))!=-1){
os.write(bytes,0,c);
result=result+c;
}
}catch(IOExceptione)
{
e.printStackTrace();
}
finally{
if(is!=null){
is.close();
}
if(os!=null){
os.close();
}
}
returnresult;
}
/**
*取得某个目录下的所有文件列表
*
*/
publicListgetFileList(Stringpath)
{
Listlist=newArrayList();
try
{
DataInputStreamdis=newDataInputStream(ftpClient.nameList(path));
Stringfilename="";
while((filename=dis.readLine())!=null)
{
list.add(filename);
}
}catch(Exceptione)
{
e.printStackTrace();
}
returnlist;
}
/**
*closeServer
*断开与ftp服务器的链接
*@throwsjava.io.IOException
*/
publicvoidcloseServer()
throwsIOException
{
try
{
if(ftpClient!=null)
{
ftpClient.closeServer();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args)throwsException
{
FtpUtilftp=newFtpUtil();
try{
//连接ftp服务器
ftp.connectServer("10.163.7.15","cxl","1","info2");
/**上传文件到info2文件夹下*/
System.out.println("filesize:"+ftp.upload("f:/download/Install.exe")+"字节");
/**取得info2文件夹下的所有文件列表,并下载到E盘下*/
Listlist=ftp.getFileList(".");
for(inti=0;i<list.size();i++)
{
Stringfilename=(String)list.get(i);
System.out.println(filename);
ftp.download(filename,"E:/"+filename);
}
}catch(Exceptione){
///
}finally
{
ftp.closeServer();
}
}
}
E. 求每日定时在服务器的FTP上取数据文件的源码(JAVA)
这个是可以向服务器端发送文字的程序,就是在客户端发送一句hello在服务器也可以接受到hello,这个程序可以修改一下就可以了。具体修改方法是增加一个定时器,然后把字符流改成字节流,现在有点忙,你先研究啊,近两天帮你写写看。
服务器端:
import java.net.*;
import java.io.*;
public class DateServer {
public static void main(String[] args) {
ServerSocket server=null;
try{
server=new ServerSocket(6666);
System.out.println(
"Server start on port 6666...");
while(true){
Socket socket=server.accept();
new SocketHandler(socket).start();
/*
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
);
out.println(new java.util.Date().toLocaleString());
out.close();
*/
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(server!=null) {
try{
server.close();
}catch(Exception ex){}
}
}
}
}
class SocketHandler extends Thread {
private Socket socket;
public SocketHandler(Socket socket) {
this.socket=socket;
}
public void run() {
try{
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
);
out.println(
new java.util.Date().
toLocaleString());
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
客户端:
package com.briup;
import java.io.*;
import java.net.*;
public class FtpClient {
public static void main(String[] args) {
if(args.length==0) {
System.out.println("Usage:java FtpClient file_path");
System.exit(0);
}
File file=new File(args[0]);
if(!file.exists()||!file.canRead()) {
System.out.println(args[0]+" doesn't exist or can not read.");
System.exit(0);
}
Socket socket=null;
try{
socket=new Socket(args[1],Integer.parseInt(args[2]));
BufferedInputStream in=new BufferedInputStream(
new FileInputStream(file)
);
BufferedOutputStream out=new BufferedOutputStream(
socket.getOutputStream()
);
byte[] buffer=new byte[1024*8];
int i=-1;
while((i=in.read(buffer))!=-1) {
out.write(buffer,0,i);
}
System.out.println(socket.getInetAddress().getHostAddress()+" send file over.");
in.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}finally{
if(socket!=null) {
try{
socket.close();
}catch(Exception ex){}
}
}
}
}
F. JAVA编写FTP连接报错java.net.ConnectException: Connection refused: connect FTP
你用的FTPClient引入不对吧,我们项目上都是用的
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
下面是我们项目上用到的FTP的实现代码(FTP需要先连接,再登录,之后就是校验登录是否成功),具体代码如下:
/**
*获取FTPClient对象
*
*@paramftpHostFTP主机服务器
*@paramftpPasswordFTP登录密码
*@paramftpUserNameFTP登录用户名
*@paramftpPortFTP端口默认为21
*@returnFTPClient
*@throwsException
*/
(StringftpHost,StringftpUserName,
StringftpPassword,intftpPort)throwsException{
try{
FTPClientftpClient=newFTPClient();
ftpClient.connect(ftpHost,ftpPort);//连接FTP服务器
ftpClient.login(ftpUserName,ftpPassword);//登陆FTP服务器
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
logger.error("未连接到FTP,用户名或密码错误!");
ftpClient.disconnect();
returnnull;
}else{
logger.info("FTP连接成功!");
returnftpClient;
}
}catch(){
logger.error("FTP的IP地址可能错误,请正确配置!");
throwsocketException;
}catch(IOExceptionioException){
logger.error("FTP的端口错误,请正确配置!");
throwioException;
}
}
G. java中怎么实现ftp服务器
我知道apache有个commons net包,其中的FTPClient类可以实现客户端和服务之间的文件传输,但是我如果使用这种方式的话,就得将一台服务器上的文件传到我本地,再将这个文件传到另一台服务器上,感觉这中间多了一步操作;
H. 诸位大神谁有java 实现FTP客户端的源码
您好,/ **
*创建日期:2008年12月23日
*类名:Ftp.java
*类路径:组织结构
*更改日志:
* / 包组织结构;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口的java。 io.IOException;
进口sun.net.TelnetInputStream;
进口sun.net.TelnetOutputStream;
进口sun.net.ftp.FtpClient;
> / **
* @作者南山地狱
* @说明FTP操作
* /
公共类的Ftp {
/ **
* BR />获取FTP目录* / 公共无效getftpList(){
字符串服务器=“IP地址 /输入FTP服务器/>弦乐用户=”“;/ / FTP服务器的登录用户名
字符串密码=“”;/ /登录FTP服务器的用户名
字符串路径密码=“”;/ / FTP路径上的服务器
尝试{
> FtpClient的FTP客户端=新FtpClient的();/ /创建FtpClient的对象
ftpClient.openServer(服务器);/ /连接到FTP服务器
ftpClient.login(用户名,密码);/ / FTP服务器 BR />如果(path.length()= 0){
ftpClient.cd(路径);
}
TelnetInputStream是= ftpClient.list();
诠释三;
而{
System.out.print((char)的C)((C = is.read())= -1!);
}
掉} is.close ();
ftpClient.closeServer();/ /退出FTP服务器
}赶上(IOException异常前){
System.out.println(ex.getMessage());
}
}
/ **
*
* /
公共无效getFtpFile(){
字符串服务器=“”;/ / IP地址中输入FTP服务器
弦乐用户=“”;/ / FTP服务器的登录用户名
字符串密码=“”;/ /登录密码为FTP服务器的用户名
字符串路径=“路径
字符串文件名“;/ /上=的FTP服务器”“;/ /下载文件名称
尝试{
FtpClient的FTP客户端=新FtpClient的();
ftpClient.openServer(服务器);
ftpClient.login(用户名,密码);
如果(路径。长度()= 0)
ftpClient.cd(路径);!
ftpClient.binary();
TelnetInputStream是= ftpClient.get(文件名);
文件file_out =新的文件(文件名);
文件输出流OS =新的文件输出流(file_out);
字节[]字节=新字节[1024];
诠释三;
而((C = is.read(字节))= -1){
os.write (字节,0,C);
}!
掉} is.close();
os.close();
ftpClient.closeServer();
}赶上(IOException异常前){
System.out.println (ex.getMessage());
}
FTP}
/ **
*文件上传到FTP
* /
公共无效putFtpFile() {
字符串服务器=“”;/ /输入IP地址对服务器
字符串用户的地址=“”;/ / FTP服务器的登录用户名
字符串密码=“”;/ / FTP服务器登录用户名密码
字符串路径=“”就 / FTP服务器/>字符串文件名=“”;/ /上传的文件名
FtpClient的FTP客户端=新的try { FtpClient的();
ftpClient.openServer(服务器);
ftpClient.login(用户名,密码);
如果(!path.length()= 0)
ftpClient.cd (路径);
ftpClient.binary();
TelnetOutputStream OS = ftpClient.put(文件名);
文件file_in =新的文件(文件名);
文件输入流是=新的文件输入流(file_in);
字节[]字节=新字节[1024];
诠释三;
同时(! (C = is.read(字节))= -1){
操作系统。写(字节,0,C);
}
掉} is.close();
os.close();
ftpClient.closeServer();
}赶上(IOException异常前){
System.out.println(ex.getMessage());
}
}
}
I. 给出一个C++或Java编写的ftp服务器程序
给你一个MFC写的:
// FtpClient.h: interface for the CFtpServer class.
//
#if !defined(_FTPCLIENT_H)
#define _FTPCLIENT_H
#include <afxinet.h>
class CFtpClient
{
//构造/析构函数
public:
CFtpClient(const char *pszFtpIp, const char *pszFtpPort, const char *pszFtpUser, const char *pszFtpPassWord);
//功能:构造函数
//参数:pszFtpIp---------Ftp服务器IP地址
// pszFtpPort-------Ftp服务器端口
// pszFtpUser-------Ftp用户名
// pszFtpPassWord---Ftp用户密码
//返回值:无
virtual ~CFtpClient();
//功能:析构函数
//参数:无
//返回值:无
//公有成员函数
public:
BOOL ConnectFtpServer();
//功能:连接FTP服务器
//参数:无
//返回值:TRUE--连接成功,FALSE--连接失败
void DisConnectFtpServer();
//功能:断开与FTP服务器的连接
//参数:无
//返回值:无
BOOL PutFileToFtpServer(const char *pszFilePath);
//功能:上传指定的文件到FTP服务器
//参数:要上传的文件名
//返回值:TRUE--连接成功,FALSE--连接失败
BOOL GetFileFromFtpServer(const char *pszFilePath);
//功能:从FTP服务器下载指定文件
//参数:要下载的文件名
//返回值:TRUE--连接成功,FALSE--连接失败
CString GetUpLoadFilePath();
//功能:得到上传文件名
//参数:无
//返回值:得到的当前路径
//私有成员变量
private:
char m_szFtpIp[20]; //Ftp服务器IP地址
UINT m_uFtpPort; //Ftp服务器端口
char m_szFtpUser[20]; //Ftp用户名
char m_szFtpPassWord[20]; //Ftp用户密码
CInternetSession *m_pInetSession; //Internet会话对象指针
CFtpConnection *m_pFtpConnection; //FTP服务连接对象指针
};
#endif
// FtpClient.cpp: implementation of the CFtpServer class.
//
#include "FtpClient.h"
CFtpClient::CFtpClient(const char *pszFtpIp, const char *pszFtpPort, const char *pszFtpUser,const char *pszFtpPassWord)
{
strcpy(m_szFtpIp, pszFtpIp);
m_uFtpPort = atoi(pszFtpPort);
strcpy(m_szFtpUser, pszFtpUser);
strcpy(m_szFtpPassWord, pszFtpPassWord);
m_pInetSession = NULL;
m_pFtpConnection = NULL;
}
CFtpClient::~CFtpClient()
{
}
BOOL CFtpClient::ConnectFtpServer()
{
//创建Internet会话
m_pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS);
try
{
//连接Ftp服务器
m_pFtpConnection = m_pInetSession->GetFtpConnection(m_szFtpIp, m_szFtpUser, m_szFtpPassWord, m_uFtpPort);
}
catch(CInternetException *pEx)
{
char szError[1024];
pEx->GetErrorMessage(szError, 1024);
pEx->Delete();
CLog Log;
Log.RecordLog("连接Ftp服务器", CString("9999"), CString(szError));
return FALSE;
}
return TRUE;
}
void CFtpClient::DisConnectFtpServer()
{
if(m_pFtpConnection != NULL)
{
m_pFtpConnection->Close();
delete m_pFtpConnection;
m_pFtpConnection = NULL;
}
if(m_pInetSession !=NULL)
{
m_pInetSession->Close();
delete m_pInetSession;
m_pInetSession = NULL;
}
}
BOOL CFtpClient::PutFileToFtpServer(const char *pszFilePath)
{
//1. 判断服务器是否连接
if(m_pFtpConnection == NULL)
{
return FALSE;
}
char szLocalFile[200] = {0};
char szRemoteFile[200] = {0};
//2. 本地文件名
sprintf(szLocalFile, "%s", pszFilePath);
//3. 远程文件名
char szDrive[10] = {0};
char szDir[210] = {0};
char szFileName[70] = {0};
char szExt[10] = {0};
_splitpath(pszFilePath, szDrive, szDir, szFileName, szExt);
sprintf(szRemoteFile, "%s\\%s.%s", g_strEnterpriseID, szFileName, szExt);
//4. 文件上传
if(!m_pFtpConnection->PutFile(szLocalFile, szRemoteFile))
{
return FALSE;
}
return TRUE;
}
BOOL CFtpClient::GetFileFromFtpServer(const char *pszFilePath)
{
//1. 判断服务器是否连接
if(m_pFtpConnection == NULL)
{
return FALSE;
}
char szLocalFile[200] = {0};
char szRemoteFile[200] = {0};
//2. 本地文件名
sprintf(szLocalFile,"%s",pszFilePath);
//3. 远程文件名
char szDrive[10] = {0};
char szDir[210] = {0};
char szFileName[70] = {0};
char szExt[10] = {0};
_splitpath(pszFilePath, szDrive, szDir, szFileName, szExt);
sprintf(szRemoteFile, "%s\\%s.%s", g_strEnterpriseID, szFileName, szExt);
//4. 文件下载
if(!m_pFtpConnection->GetFile(szRemoteFile, szLocalFile))
{
return FALSE;
}
return TRUE;
}
CString CFtpClient::GetUpLoadFilePath()
{
CString strPath = "", strDir = "";
//得到当前日期
CTime time = CTime::GetCurrentTime();
CString strDate = time.Format("%Y%m%d");
//得到上传文件路径
char filepath[MAX_PATH];
GetMoleFileName(NULL, filepath, MAX_PATH);
strDir.Format("%s", filepath);
strPath = strDir.Left(strDir.ReverseFind('\\')) + strDate + "\\*.txt";
return strPath;
}
//保存成两个文件,然后添加到你的工程中就可以调用了。
J. java ftp怎么实现java 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服务器的衔接,运用默许端口号。