各位大哥,java类库中有没有用于上传、下载的类包?最好是用ftp协议的。那里可以找到?
如题! 问题点数:20、回复次数:15Top
1 楼darkattack(居士)回复于 2005-04-01 10:54:45 得分 0
java.net.*
只有更底层的socket类。Top
2 楼topil(认认真真学习,塌塌实实工作)回复于 2005-04-01 11:10:58 得分 20
用java.net.* 里面都有,
一个用ftp上传文件的例子
/*
* Created on 2005-3-30
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author nizheng
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import sun.net.ftp.FtpClient;
public class ftp {
public ftp() {
}
private static FtpClient m_client;
protected static void disconnect()
{
if (m_client != null)
{
try
{
m_client.closeServer();
}
catch (IOException ex)
{
}
m_client = null;
}
}
protected static boolean connect(String sHost, String user,
String password ,String sDir)
{
try
{
m_client = new FtpClient(sHost);
m_client.login(user, password);
m_client.cd(sDir);
m_client.binary();
}
catch (Exception ex)
{
return false;
}
return true;
}
protected static boolean putFiletoServer(String m_sLocalFile,String m_sHostFile)
{
if (m_sLocalFile.length()==0)
{
return false;
}
byte[] buffer = new byte[10240];
try
{
File f = new File(m_sLocalFile);
int size = (int)f.length();
FileInputStream in = new FileInputStream(m_sLocalFile);
OutputStream out = m_client.put(m_sHostFile);
int counter = 0;
while(true)
{
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
}
out.close();
in.close();
}
catch (Exception ex)
{
return false;
}
return true;
}
public static boolean putFile(String pathname,String ftpServer, String ftpUser,
String ftpPasswd, String ftpPath)
{
if (!connect(ftpServer,ftpUser,ftpPasswd,ftpPath))
{
return false;
}
int pos = pathname.lastIndexOf("/");
int len = pathname.length();
String filename = pathname.substring(pos+1,len);
if (!putFiletoServer(pathname,filename))
{
return false;
}
disconnect();
return true;
}
/* sample */
public static void main(String[] args)
{
boolean b = putFile("file.txt","172.16.3.28","Anonymous","","watool/ldmacs/share/rootdir/system/invoice/upload/");
System.out.println(b);
}
}
Top
3 楼javarat()回复于 2005-04-01 11:48:05 得分 0
我一般用jspSmartUpload包(下载很多,搜一下就有)
下面这个是它的教程
http://www.tongyi.net/article/20031015/200310153755_1.shtml
如果你想自己编写,这里有它的源代码你可以参考一下:
http://www.9soho.com/Software/Catalog64/1408.htmlTop
4 楼OnlyFor_love(『勾勾手指头 一辈子不分手』)回复于 2005-04-01 12:03:01 得分 0
用smartupload就可以了!Top
5 楼jianghuxing(回头看看原来我一无所有)回复于 2005-04-01 12:07:56 得分 0
http://www.tongyi.net/article/20031015/200310153755_1.shtml
http://www.9soho.com/Software/Catalog64/1408.html
smartuploadTop
6 楼chtfallcn(hawk)回复于 2005-04-01 13:23:56 得分 0
我是用swing开发的图形界面,不是用的b/s结构,请问c/s结构下能使用smartupload吗?Top
7 楼topil(认认真真学习,塌塌实实工作)回复于 2005-04-01 13:30:02 得分 0
楼主没试我那个程序嘛,完全可以实现你的功能啊Top
8 楼chtfallcn(hawk)回复于 2005-04-02 15:57:44 得分 0
to : topil(认认真真学习,塌塌实实工作)
你程序中用到的 ort sun.net.ftp.FtpClient; 是哪里的类包阿?哪里能找到他的说明文档?
我想实现的是从服务器上下载一个文件,你的例子是上传阿。
谢谢!Top
9 楼caoying1977(吃尽天下)回复于 2005-04-02 21:08:20 得分 0
简单啊,用cd命令到目录,再用PUT命令得文件啊Top
10 楼chtfallcn(hawk)回复于 2005-04-04 09:28:52 得分 0
FtpClient ftpClient=new FtpClient();
System.out.println("1");
ftpClient.openServer(server);
System.out.println("2");
ftpClient.login(user, password);
System.out.println("3");
为什么我在登陆ftpClient.login(user, password);这个地方过不去阿?总是返回一个false。登陆名和密码没问题!请问是什么原因?Top
11 楼chtfallcn(hawk)回复于 2005-04-04 09:43:46 得分 0
连接到的服务器还要开ftp服务吗?我的服务器用的是solaris .请问在solaris上有什么需要注意的地方?主要是路径问题。Top
12 楼topil(认认真真学习,塌塌实实工作)回复于 2005-04-04 10:07:46 得分 0
sun.net.ftp.FtpClient是jdk里面自带的,直接去jdk的文档里面就可以找到。
服务器当然要开FTP了才能够连接的嘛,楼主先试试你从IE里面输入你的用户名,密码能否连接上去,你那个返回false,肯定是没有连接上服务器。关于server的OS没有太大区别的Top
13 楼chtfallcn(hawk)回复于 2005-04-04 11:15:39 得分 0
请问连接到ftp服务器后是不是直接连接到ftp指定的文件夹?如果我想下载excel文件夹下的Template.xls文件,是否这样?
ftpClient.cd("excel/");
ftpClient.binary();
TelnetInputStream is=ftpClient.get("Template.xls");Top
14 楼lEFTmOON(我的小站(www.ismyway.com))回复于 2005-04-04 12:02:44 得分 0
关注一下!Top
15 楼newste(旭林)回复于 2005-04-11 00:29:06 得分 0
upTop




