社区
Web 开发
帖子详情
如何在网站中实现ftp上传功能-------救急各位大哥
liuzhx
2005-06-07 09:29:28
我做个教学网站,老师说要实现ftp上传功能提交作业,我现在搞不定,高手救我!~
qq 297310344.
...全文
576
12
打赏
收藏
如何在网站中实现ftp上传功能-------救急各位大哥
我做个教学网站,老师说要实现ftp上传功能提交作业,我现在搞不定,高手救我!~ qq 297310344.
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
12 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
zxmzfbdc
2005-06-14
打赏
举报
回复
使用apache的common—net.jar,代码如下:
/*
* Created on 2005-5-20
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package cn.zxm.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
/**
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FTPUpload
{
FTPClient client = null;
/**
* Logins ftp server
* @param model
* @return
*/
public boolean login(FTPModel model) {
boolean suc = false;
client = new FTPClient();
try {
System.out.println("starting......");
client.connect(model.getHostServer(), model.getPort());
suc = client.login(model.getUserName(), model.getPassword());
System.out.println("reply......" + client.getReplyString());
if (suc) {
System.out.println("成功登录");
}
//client.makeDirectory("aaaaaa");
} catch (IOException ex) {
}
return suc;
}
/**
* Logouts ftp server
* @return
*/
public boolean logout() {
boolean suc = false;
try {
client.logout();
System.out.println("注销成功");
} catch (IOException io) {
io.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException ex) {
}
}
return suc;
}
/**
* Uploads a single file to ftp server
* @param source
* @param from
* @param to
* @return
*/
public boolean upload(String source, String from, String to) {
boolean suc = false;
source = replaceAll(source, '\\', '/');
if (!source.startsWith("/")) {
source = "/" + source;
}
from = this.replaceAll(from, '\\', '/');
if (from.endsWith("/")) {
from = from.substring(0, from.length() - 1);
}
to = this.replaceAll(to, '\\', '/');
if (to.endsWith("/")) {
to = to.substring(0, to.length() - 1);
}
System.out.println(source + "\n" + from + "\n" + to);
try {
FileInputStream in = new FileInputStream(from + source);
int last = source.lastIndexOf("/");
//client.makeDirectory("aaaaaa");
System.out.println("store......" + to + source.substring(0, last));
this.makeDirectory(to + source.substring(0, last));
client.storeFile(to + source, in);
in.close();
suc = true;
} catch (FileNotFoundException ex) {
suc = false;
ex.printStackTrace();
} catch (IOException io) {
suc = false;
io.printStackTrace();
}
return false;
}
public boolean upload(FTPModel model)
{
FTPClient client = new FTPClient();
boolean error = true;
try
{
System.out.println("starting......");
client.connect(model.getHostServer(), model.getPort());
boolean suc = client.login(model.getUserName(), model.getPassword());
System.out.println("reply......" + client.getReplyString());
if (suc)
{
System.out.println("成功登录");
}
String from = model.getFromDirectory();
String to = model.getToDirectory();
upload(client, model);
System.out.println("文件上传成功\n");
boolean isLogout = client.logout();
if (isLogout)
{
System.out.println("注销成功");
}
}
catch (IOException ex)
{
error = false;
}
finally
{
try
{
client.disconnect();
}
catch (IOException io)
{
io.printStackTrace();
}
}
return error;
}
public void upload(FTPClient client, FTPModel model) throws IOException
{
String orifrom = model.getFromDirectory();
String orito = model.getToDirectory();
String from = replaceAll(orifrom, '\\', '/');
String to = replaceAll(orito, '\\', '/');
String[] source = to.split("/");
StringBuffer path = new StringBuffer();
for (int i = 0; i < source.length; i++)
{
path.append("/" + source[i]);
client.makeDirectory(path.toString());
}
File file = new File(from);
uploadFiles(client, model, file);
//file.listFiles()
}
public void uploadFiles(FTPClient client, FTPModel model, File source) throws IOException
{
File[] files = source.listFiles();
String orifrom = model.getFromDirectory();
String orito = model.getToDirectory();
String from = replaceAll(orifrom, '\\', '/');
String to = replaceAll(orito, '\\', '/');
String path = "";
for (int i = 0; i < files.length; i++)
{
if (files[i].isFile())
{
path = files[i].getAbsolutePath();
//System.out.println(replaceAll(path, '\\', '/') + "\n" + from + " " + path.indexOf(from));
path = replaceAll(path, '\\', '/').substring(from.length());
if (path.startsWith("/"))
{
path = path.substring(1);
}
//System.err.println(to + path + " " + files[i].getAbsolutePath());
client.storeFile("/" + to + "/" + path, new FileInputStream(files[i]));
//boolean suc = client.deleteFile("/"+to + "/" + path);
//System.out.println(suc);
}
else if (files[i].isDirectory())
{
path = files[i].getAbsolutePath();
path = path.substring(from.length());
if (path.startsWith("/"))
{
path = path.substring(1);
}
client.makeDirectory("/" + to + "/" + path);
//client.deleteFile("/" + to + "/" + path);
uploadFiles(client, model, files[i]);
}
}
}
public void makeDirectory(String dirs) throws IOException{
StringBuffer dir = new StringBuffer();
String[] path = dirs.split("/");
for(int i = 0; i < path.length; i++){
System.out.println(path[i]);
dir.append("/" + path[i]);
client.makeDirectory(dir.toString().substring(1, dir.toString().length()));
}
System.out.println("dir ===============> " + dir.toString().substring(1, dir.toString().length()));
}
public String replaceAll(String ori, char regex, char replacement)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ori.length(); i++)
{
char c = ori.charAt(i);
if (c == regex)
{
c = replacement;
}
sb.append(c);
}
return sb.toString();
}
}
public void test()
{
try
{
FTPClient client = new FTPClient();
System.out.println("starting...");
//Socket socket = new Socket("www.sohu.com", 80);
client.connect("192.168.1.217", 21);
System.out.println("connected...");
System.out.println("reply: " + client.getReplyString());
boolean suc = client.login("forum", "forum");//client.login("zhaoxueminftpuser", "zhaoxueminftpuser");
System.out.println(suc);
if(suc){
System.out.println("登录FTP成功");
}
FileInputStream in = new FileInputStream("C:/img/cat/yellow.jpg");
client.makeDirectory("zxm");
client.makeDirectory("zxm/cat");
boolean res = client.storeFile("zxm/cat/cat.jpg", in);
if(res){
System.out.println("上传文件成功");
}
// client.logout();
// if(client.isConnected()){
// client.disconnect();
// }
}
catch (SocketException se)
{
se.printStackTrace();
}
catch (IOException io)
{
io.printStackTrace();
}
}
liuzhx
2005-06-12
打赏
举报
回复
up
liuzhx
2005-06-11
打赏
举报
回复
不是吧!~
没有人帮我。我实现了上传,上传在当前目录下liuzhx文件夹里。怎么样才能下载liuzhx文件夹里的文件呢?还有删除里面的选定的文件呢?
不用远程控制,解决就给加分。嫌少可以再加。
liuzhx
2005-06-09
打赏
举报
回复
jspsmartupload组件应该可以吧!
谁有一个可以下载的地址!~
我在线等。
凯晰叶子
2005-06-09
打赏
举报
回复
UP
liuzhx
2005-06-09
打赏
举报
回复
实现上传就可以了!~
http也可以
liuzhx
2005-06-09
打赏
举报
回复
诸位大虾帮忙!~
liuzhx
2005-06-08
打赏
举报
回复
能发给我吗?
我的油箱是liuzhx401@163.com
我7月1号到深圳上班,各位大哥帮帮忙,分不够可以在加
starwill
2005-06-07
打赏
举报
回复
1月前写过,实例在公司,没带出来~~~
很简单的,查一下写上就能写出来 ~~
liuzhx
2005-06-07
打赏
举报
回复
有实例吧?
我jsp不熟!~
帮人做,苦啊
starwill
2005-06-07
打赏
举报
回复
文件上传的方式有几种:
最简单的方式:直接用Struts标签:<html:file>,Action里会得到一个文件输入流,然后写入一个新文件
如果一定要用FTP(我以前做的一个工程就是),建议另写一个FtpOper的类,封装FTP上传,下载,连接服务器,断开连接等。实现方式用 sun.net.ftp.FtpClient 类中的方法,查一下JDK文档很快就能搞出来~~
需要说明一下的是:如果是在Action里得到文件输入流,然后调用FTP方法上传,实际的上传方式并不是由客户端直接传到FTP服务器,而是由客户端先传到应用服务器(装WEBLOGIC或TOMCAT之类),然后由应用服务器搞用FTP传到FTP服务器~~~
以上
zidian
2005-06-07
打赏
举报
回复
smartupload组件
使用QT
实现
文件
上传
和下载----
ftp
服务器
使用QT
实现
文件
上传
和下载,服务器为
FTP
服务器,这是一个简单的测试源码。希望能够给初学者一定的启发。
Java语言
实现
简单
FTP
软件------>源码放送(十三)
Java语言
实现
简单
FTP
软件------>
FTP
协议分析(一) Java语言
实现
简单
FTP
软件------>
FTP
软件效果图预览之下载
功能
(二) Java语言
实现
简单
FTP
软件------>
FTP
软件效果图预览之
上传
功能
(三) Java语言
实现
简单
FTP
软件------>
FTP
软件主界面的
实现
(四) Java语言
实现
简单
FTP
软件------>FT
linux
ftp
-v -n,请问
ftp
中
-v,-i,-n参数都表示什么意思啊???
请问
ftp
中
-v,-i,-n参数都表示什么意思啊???|在
ftp
命令
中
使用了
ftp
-i-n选项,这意味着不要自动登录,而且关闭交互模式在用shell编程时-i-n这2个参数用的比较多-v是把交互式的信息提示显示出来|man
ftp
楼主够懒,纯为接分-vVerboseoptionforces
ftp
toshowallresponsesfromtheremot...
ftp
-i -n -v <中 -n禁止自动登录到初始连接
<<!说明是输入。如是结束了需要再输入 !例如:
ftp
-i -n -v<<! 这里的叹号代表是
ftp
命令的开始get文件exit! 代表
ftp
的命令结束了。 <<后面可以跟任何字符,但是
ftp
命令结束后,要成对出现,即一个开始一个结束。 其
中
-n禁止自动登录到初始连接,自动连接时会要求输出密码导致无法登录 转载于:ht...
Java语言
实现
简单
FTP
软件------>
FTP
软件效果图预览之
上传
功能
(三)
Java语言
实现
简单
FTP
软件------>
FTP
软件效果图预览之
上传
功能
(三) 下面展示一下
上传
功能
的过程
Web 开发
81,094
社区成员
341,711
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章