各位大哥,java类库中有没有用于上传、下载的类包?最好是用ftp协议的。那里可以找到? java.net.*只有更底层的socket类。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用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); } } 我一般用jspSmartUpload包(下载很多,搜一下就有)下面这个是它的教程http://www.tongyi.net/article/20031015/200310153755_1.shtml如果你想自己编写,这里有它的源代码你可以参考一下:http://www.9soho.com/Software/Catalog64/1408.html http://www.tongyi.net/article/20031015/200310153755_1.shtmlhttp://www.9soho.com/Software/Catalog64/1408.htmlsmartupload 我是用swing开发的图形界面,不是用的b/s结构,请问c/s结构下能使用smartupload吗? to : topil(认认真真学习,塌塌实实工作)你程序中用到的 ort sun.net.ftp.FtpClient; 是哪里的类包阿?哪里能找到他的说明文档?我想实现的是从服务器上下载一个文件,你的例子是上传阿。谢谢! 简单啊,用cd命令到目录,再用PUT命令得文件啊 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。登陆名和密码没问题!请问是什么原因? 连接到的服务器还要开ftp服务吗?我的服务器用的是solaris .请问在solaris上有什么需要注意的地方?主要是路径问题。 sun.net.ftp.FtpClient是jdk里面自带的,直接去jdk的文档里面就可以找到。服务器当然要开FTP了才能够连接的嘛,楼主先试试你从IE里面输入你的用户名,密码能否连接上去,你那个返回false,肯定是没有连接上服务器。关于server的OS没有太大区别的 请问连接到ftp服务器后是不是直接连接到ftp指定的文件夹?如果我想下载excel文件夹下的Template.xls文件,是否这样? ftpClient.cd("excel/"); ftpClient.binary(); TelnetInputStream is=ftpClient.get("Template.xls"); 很多请求只打开一个数据库连接行吗? 关闭applet怎么会自动关闭浏览器 new类时,后面有{//do something}? 一个很奇怪的java问题? 在jface里面,如何把action关联到button中? smartupload上传文件 如果磁盘已满 有提示么? 请教JDK在LINUX下的安装问题 取钱 线程同步问题 请问jar就是class的包吗?放在哪,怎么用? 如何设置Weblogic中的用户限制,我的Weblogic目前只允许3人登录? 怎样得到borderlayout布局中center区域组件的大小? 如何写任意有颜色的点写到一个jpg文件??
一个用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);
}
}
下面这个是它的教程
http://www.tongyi.net/article/20031015/200310153755_1.shtml
如果你想自己编写,这里有它的源代码你可以参考一下:
http://www.9soho.com/Software/Catalog64/1408.html
http://www.9soho.com/Software/Catalog64/1408.html
smartupload
我想实现的是从服务器上下载一个文件,你的例子是上传阿。
谢谢!
System.out.println("1");
ftpClient.openServer(server);
System.out.println("2");
ftpClient.login(user, password);
System.out.println("3");为什么我在登陆ftpClient.login(user, password);这个地方过不去阿?总是返回一个false。登陆名和密码没问题!请问是什么原因?
服务器当然要开FTP了才能够连接的嘛,楼主先试试你从IE里面输入你的用户名,密码能否连接上去,你那个返回false,肯定是没有连接上服务器。关于server的OS没有太大区别的
ftpClient.cd("excel/");
ftpClient.binary();
TelnetInputStream is=ftpClient.get("Template.xls");