社区
Web 开发
帖子详情
如何在网站中实现ftp上传功能-------救急各位大哥
liuzhx
2005-06-07 09:29:28
我做个教学网站,老师说要实现ftp上传功能提交作业,我现在搞不定,高手救我!~
qq 297310344.
...全文
636
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组件
Ubuntu22.04下快速搭建
FTP
服务器:vs
ftp
d配置全攻略
本文提供在Ubuntu 22.04系统上快速搭建
FTP
服务器的完整指南。详细介绍了使用vs
ftp
d软件进行安装、核心配置、用户隔离与目录权限设置的全过程,并涵盖SSL/TLS加密、防火墙配置等关键安全加固措施,帮助用户快速构建安全、稳定的内部文件共享服务。
【企业风险管理】基于COSO框架的整合式风险管理:目标设定、风险识别与应对机制在组织
中
的协同应用
内容概要:本文介绍了COSO企业风险管理框架,阐述了企业风险管理的定义、目标、构成要素及其与内部控制的关系。文章指出,企业风险管理是一个由董事会、管理层及其他人员实施的,应用于战略制定并贯穿于企业之
中
的过程,旨在识别可能影响主体的潜在事项,管理风险以使其在该主体的风险容量之内,并为主体目标的
实现
提供合理保证。该框架强调了企业风险管理的八大构成要素:内部环境、目标设定、事项识别、风险评估、风险应对、控制活动、信息与沟通、监控。此外,文章还讨论了企业风险管理的局限性,如人为判断失误、成本与效益的权衡、管理层凌驾等。 适合人群:具备一定管理学基础知识,对风险管理有兴趣的学者、企业管理人员、内部审计师及外部审计师。 使用场景及目标:①帮助组织理解并应用企业风险管理框架,以提高组织的风险管理水平;②指导企业如何通过有效的风险管理来
实现
其战略、经营、报告和合规目标;③为企业提供一套标准化的风险管理语言,促进内外部沟通与合作。 阅读建议:读者应结合实际案例,深入理解每个构成要素的具体应用,同时关注企业风险管理的局限性,以全面把握企业风险管理的本质。此外,建议定期回顾和更新风险管理策略,以适应不断变化的内外部环境。
凸轮.prt_UG四五轴CNC编程练习图档.rar
凸轮.prt_UG四五轴CNC编程练习图档.rar
792.zip
当 CAD 缺失对应字体时,图纸文字会显示异常,出现乱码、问号。将下载好的字体文件复制到 AutoCAD 的 Fonts 文件夹
中
,即可恢复正常显示。
科研院所如何
实现
科技成果的智能化管理与推广?.docx
科易网基于40亿+科创知识图谱数据库,深度探索AI技术在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,研究科技创新领域的AI+数智化解决方案,推动科技创新与产业创新智能化发展。
Web 开发
81,108
社区成员
341,721
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章