我在网上找的代码编了一个Ftp类,Ftp上传下载功能都可以实现,
可是速度太慢了不能忍受。这么写不对吗?
怎么改进可以使ftp上传下载速度加快?
还有没有更好的写法?
代码如下:
import java.lang.*;
import java.net.*;
import java.io.*;
import sun.net.*;
import sun.net.ftp.*;public class Ftp
{
FtpClient ftpClient;//--------------------------------------------------------------------------
public void upload(String filepathname)//filepathname文件名用'/'格开
{
byte[] bytes=new byte[1024];
int count;
try{
String fg = new String("/");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index + 1);
RandomAccessFile sendFile = new RandomAccessFile(filepathname, "r");
sendFile.seek(0);
TelnetOutputStream os=ftpClient.put(filename);
while((count=sendFile.read(bytes))!=-1){
os.write(bytes);
}
sendFile.close();
os.close();
System.out.println("文件上传成功!!!");
}
catch (IOException e) {
System.out.println("文件上传失败!!!");
}
}
//----------------------------------------------------------------------
public void download(String filename)
{
byte[] bytes=new byte[1024];
int count;
try{
RandomAccessFile downFile = new RandomAccessFile(filename, "rw");
downFile.seek(0);
TelnetInputStream is =ftpClient.get(filename);
while ((count=is.read(bytes)) != -1)
{
downFile.write(bytes,0,count);
}
is.close();
downFile.close();
System.out.println("文件下载成功!!!");
}
catch (IOException e) {
System.out.println("文件上传失败!!!");
}
}
public Ftp()
{
String server="188.177.200.172";
String user="user";
String password="user";
String filename="f:/java/file/1.txt"; try{
ftpClient=new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user,password);
ftpClient.binary();
System.out.println("连接ok!");
upload(filename);
//download("1.txt");
ftpClient.closeServer();
}
catch(FtpLoginException e){
System.out.println("无权限与主机:连接!");
}
catch (IOException e){
System.out.println("连接主机失败!");
}
catch(SecurityException e)
{
System.out.println("无权限与主机:连接!");
}
}
public static void main(String[] args)
{
Ftp aftp=new Ftp();
}
}
可是速度太慢了不能忍受。这么写不对吗?
怎么改进可以使ftp上传下载速度加快?
还有没有更好的写法?
代码如下:
import java.lang.*;
import java.net.*;
import java.io.*;
import sun.net.*;
import sun.net.ftp.*;public class Ftp
{
FtpClient ftpClient;//--------------------------------------------------------------------------
public void upload(String filepathname)//filepathname文件名用'/'格开
{
byte[] bytes=new byte[1024];
int count;
try{
String fg = new String("/");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index + 1);
RandomAccessFile sendFile = new RandomAccessFile(filepathname, "r");
sendFile.seek(0);
TelnetOutputStream os=ftpClient.put(filename);
while((count=sendFile.read(bytes))!=-1){
os.write(bytes);
}
sendFile.close();
os.close();
System.out.println("文件上传成功!!!");
}
catch (IOException e) {
System.out.println("文件上传失败!!!");
}
}
//----------------------------------------------------------------------
public void download(String filename)
{
byte[] bytes=new byte[1024];
int count;
try{
RandomAccessFile downFile = new RandomAccessFile(filename, "rw");
downFile.seek(0);
TelnetInputStream is =ftpClient.get(filename);
while ((count=is.read(bytes)) != -1)
{
downFile.write(bytes,0,count);
}
is.close();
downFile.close();
System.out.println("文件下载成功!!!");
}
catch (IOException e) {
System.out.println("文件上传失败!!!");
}
}
public Ftp()
{
String server="188.177.200.172";
String user="user";
String password="user";
String filename="f:/java/file/1.txt"; try{
ftpClient=new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user,password);
ftpClient.binary();
System.out.println("连接ok!");
upload(filename);
//download("1.txt");
ftpClient.closeServer();
}
catch(FtpLoginException e){
System.out.println("无权限与主机:连接!");
}
catch (IOException e){
System.out.println("连接主机失败!");
}
catch(SecurityException e)
{
System.out.println("无权限与主机:连接!");
}
}
public static void main(String[] args)
{
Ftp aftp=new Ftp();
}
}
FileOutputStream
吗?
BufferedReader buffer = new BufferedReader(new InputStreamReader(ftpClient.get(filename)));
if (buffer != null) {
while ((v_temp = buffer.readLine()) != null) {
myFile.println(v_temp);
}
buffer.close();