如何在网站中实现ftp上传功能-------救急各位大哥

liuzhx 2005-06-07 09:29:28
我做个教学网站,老师说要实现ftp上传功能提交作业,我现在搞不定,高手救我!~
qq 297310344.
...全文
576 12 打赏 收藏 转发到动态 举报
写回复
用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组件

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧