社区
Java SE
帖子详情
FTPClient.rename 报502错误,怎么回事
非典型宅男c
2016-07-03 03:20:24
...全文
448
4
打赏
收藏
FTPClient.rename 报502错误,怎么回事
[图片]
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
非典型宅男c
2016-07-08
打赏
举报
回复
引用 1 楼 anselmoe 的回复:
听听大神解答~~~~~~~~~~~~~~~~
大神手把手的教我吧!
Ansel-枫儿-Moe
2016-07-08
打赏
举报
回复
可能是windows 系统下,命令不认识
Ansel-枫儿-Moe
2016-07-08
打赏
举报
回复
听听大神解答~~~~~~~~~~~~~~~~
commons-net-1.4.1.jar
ftp
包,java 对
FTP
服务器文件的 增加,删除等操作。
JAVA
FTP
Client
文件操作
JAVA
FTP
Client
文件操作
ftp
上传文本、移动文件、删除文件
public class
FTP
Util { private
FTP
Client
ftp
Client
=null; private boolean result = false; private FileInputStream fis; String
ftp
Host = "10.16.111.111"; String port = 21 String
ftp
UserName = "
ftp
user11; String
ftp
Password = "1234561"; /** * 登录服务器 * @param
ftp
Info * @return * @throws IOException */ public
FTP
Client
login() throws IOException {
ftp
Client
= new
FTP
Client
();
ftp
Client
.connect(
ftp
Host); boolean login =
ftp
Client
.login(
ftp
UserName,
ftp
Password); int reply =
ftp
Client
.getReplyCode(); if (!
FTP
Reply.isPositiveCompletion(reply)) {
ftp
Client
.disconnect(); } if(login){ System.out.println("
ftp
连接成功!"); }else{ System.out.println("
ftp
连接失败!"); } //
ftp
Client
.setControlEncoding("GBK"); return
ftp
Client
; } /** * 字符串作为文件上传指定目录 下 * @param content 源字符串 * @param uploadDir 上传目录 * @param
ftp
FileName 上传文件名称 * @throws Exception */ public void
ftp
UploadByText(String content ,String uploadDir,String
ftp
FileName) throws Exception{ try {
ftp
Client
= this.login(); //创建目录 createDir(
ftp
Client
,uploadDir); // 设置上传目录 这个也应该用配置文件读取
ftp
Client
.changeWorkingDirectory(uploadDir);
ftp
Client
.setBufferSize(1024);
ftp
Client
.setControlEncoding("GBK"); // 设置文件类型(二进制)
ftp
Client
.setFileType(
FTP
Client
.BINARY_FILE_TYPE); String fileName = new String(
ftp
FileName.getBytes("GBK"),"iso-8859-1"); OutputStream os =
ftp
Client
.storeFileStream(fileName); byte[] bytes = content.getBytes(); os.write(bytes); os.flush(); os.close(); } catch (Exception e) {
ftp
Client
.disconnect();
ftp
Client
= null; e.printStackTrace(); throw e; }finally{
ftp
Client
.disconnect();
ftp
Client
= null; } } /** * 移动文件 * @param
ftp
Info * @return * @throws Exception */ public boolean moveFile(
FTP
Info
ftp
Info)throws Exception { boolean flag = false; try {
ftp
Client
= this.login(); flag = this.moveFile(
ftp
Client
,
ftp
Info.getChangeWorkingDirectoryPath(),
ftp
Info.getFilePath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try {
ftp
Client
.disconnect();
ftp
Client
= null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭
FTP
连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 删除文件 * @param
ftp
Info * @return * @throws Exception */ public boolean deleteFile(
FTP
Info
ftp
Info)throws Exception { boolean flag = false; try {
ftp
Client
= this.login(); flag = this.deleteByFolder(
ftp
Client
,
ftp
Info.getChangeWorkingDirectoryPath()); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try {
ftp
Client
.disconnect();
ftp
Client
= null; } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭
FTP
连接发生异常!", e); }catch (Exception e) { e.printStackTrace(); throw e; } } return flag; } /** * 实现文件的移动,这里做的是一个文件夹下的所有内容移动到新的文件, * 如果要做指定文件移动,加个判断判断文件名 * 如果不需要移动,只是需要文件重命名,可以使用
ftp
.
rename
(oleName,newName) * @param
ftp
* @param oldPath * @param newPath * @return */ public boolean moveFile(
FTP
Client
ftp
,String oldPath,String newPath){ boolean flag = false; try {
ftp
.changeWorkingDirectory(oldPath);
ftp
.enterLocalPassiveMode(); //获取文件数组
FTP
File[] files =
ftp
.listFiles(); //新文件夹不存在则创建 if(!
ftp
.changeWorkingDirectory(newPath)){
ftp
.makeDirectory(newPath); } //回到原有工作目录
ftp
.changeWorkingDirectory(oldPath); for (
FTP
File file : files) { if(file.isDirectory()) { moveFile(
ftp
,oldPath+file.getName()+"/" ,newPath+file.getName()+"/"); }else{ //转存目录 flag =
ftp
.
rename
(oldPath+new String(file.getName().getBytes("GBK"),"ISO-8859-1"), newPath+"/"+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } if(flag){ System.out.println(file.getName()+"移动成功"); }else{ System.out.println(file.getName()+"移动失败"); } }
ftp
.removeDirectory(new String(oldPath.getBytes("GBK"),"ISO-8859-1")); } catch (Exception e) { e.printStackTrace(); System.out.println("移动文件失败"); } return flag; } /** * 删除
FTP
上指定文件夹下文件及其子文件方法,添加了对中文目录的支持 * @param
ftp
FTP
Client
对象 * @param
Ftp
Folder 需要删除的文件夹 * @return */ public boolean deleteByFolder(
FTP
Client
ftp
,String
Ftp
Folder){ boolean flag = false; try {
ftp
.changeWorkingDirectory(new String(
Ftp
Folder.getBytes("GBK"),"ISO-8859-1"));
ftp
.enterLocalPassiveMode();
FTP
File[] files =
ftp
.listFiles(); for (
FTP
File file : files) { //判断为文件则删除 if(file.isFile()){
ftp
.deleteFile(
Ftp
Folder+new String(file.getName().getBytes("GBK"),"ISO-8859-1")); } //判断是文件夹 if(file.isDirectory()){ String childPath =
Ftp
Folder +file.getName()+ "/"; //递归删除子文件夹 deleteByFolder(
ftp
,childPath); } } //循环完成后删除文件夹 flag =
ftp
.removeDirectory(new String(
Ftp
Folder.getBytes("GBK"),"ISO-8859-1")); if(flag){ System.out.println(
Ftp
Folder+"文件夹删除成功"); }else{ System.out.println(
Ftp
Folder+"文件夹删除成功"); } } catch (Exception e) { e.printStackTrace(); System.out.println("删除失败"); } return flag; } /** * 创建目录 * @param createpath * @param s
ftp
*/ public void createDir(
FTP
Client
ftp
Client
,String createpath) throws Exception { try { if(
ftp
Client
.changeWorkingDirectory(createpath)) { return; } String pathArry[] = createpath.split("/"); StringBuffer filePath = new StringBuffer("/"); for (String path : pathArry) { if (path.equals("")) { continue; } filePath.append(path + "/"); if(!
ftp
Client
.changeWorkingDirectory(filePath.toString())) {
ftp
Client
.makeDirectory(filePath.toString());
ftp
Client
.changeWorkingDirectory(filePath.toString()); } }
ftp
Client
.changeWorkingDirectory(createpath); }catch (Exception e) { e.printStackTrace(); throw new Exception("创建路径
错误
:" + createpath); } } }
Java实现
ftp
上传程序源代码
这是一个Java语言写成的上传到文件服务器的源代码 编译能通过可直接运行 另加三个实例代码
commons-
ftp
中
ftp
Client
类的API
commons-
ftp
中
ftp
Client
类的API
Java SE
62,635
社区成员
307,269
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章