当前位置:首页 » 文件管理 » ftpset

ftpset

发布时间: 2022-03-04 22:58:32

javaftp上下载文件大小大概10M左右,ftpClient.setBufferSize(1024)要设置多大的呢

几k到1m 这个是临时的缓冲

㈡ 看了一段java代码是从FTP上下载文件,ftpClient.setBufferSize()这个是什么用处,要怎么使用它

缓冲区大小,等从ftp下载的数据存储到缓冲区,等缓冲区满了,进行磁盘读写。

㈢ ftp的路径怎么设置

问一下,你是想做ftp上传下载么?

首先你需要安装一个ftp服务端程序,启动起来,然后下载一个ftp客户端程序,测试能不能连接,首先这一块儿需要测试通过。
代码ftp上传下载
2.1 上传代码:
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class test {

private FTPClient ftp;
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param file 上传的文件或文件夹
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}else{
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
public static void main(String[] args) throws Exception{
test t = new test();
t.connect("", "localhost", 21, "yhh", "yhhazr");
File file = new File("e:\\uploadify");
t.upload(file);
}
}
2.2 下载代码
这里没有用到filter,如果用filter就可以过滤想要的文件。

public class Ftp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Ftp ftp = new Ftp();
String hostname = "www.strawberry.com";
Integer port = 21;
String username = "username";
String password = "password";
String remote = "/c.txt";
String local = "/home/tin/LeonChen/FTP/";
try {
ftp.connect(hostname, port, username, password);
System.out.println("接收状态:"+ftp.download(remote, local));
ftp.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private FTPClient ftpClient = new FTPClient();

/*
* * 连接到FTP服务器
* * @param hostname 主机名
* * @param port 端口
* * @param username 用户名
* * @param password 密码
* * @return 是否连接成功
* * @throws IOException
*/
private boolean connect(String hostname, int port, String username,
String password) throws IOException {
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding("UTF-8");
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
if (ftpClient.login(username, password)) {
return true;
}
}
disconnect();
return false;
}

/*
* 从FTP服务器上下载文件,支持断点续传,上传百分比汇报
*
* @param remote 远程文件路径
*
* @param local 本地文件路径
*
* @return 上传的状态
*
* @throws IOException
*/
public DownloadStatus download(String remote, String local)
throws IOException {
// 设置被动模式
ftpClient.enterLocalPassiveMode();
// 设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result;
// 检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(remote
.getBytes("UTF-8"), "iso-8859-1"));
if (files.length != 1) {
System.out.println("远程文件不存在");
return DownloadStatus.Remote_File_Noexist;
}
long lRemoteSize = files[0].getSize();
String fildName = files[0].getName();
// 本地存在文件,进行断点下载
File f = new File(local+fildName);
if (f.exists()) {
long localSize = f.length();
if (localSize >= lRemoteSize) {
System.out.println("本地文件大于远程文件,下载中止");
return DownloadStatus.Local_Bigger_Remote;
}

// 进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f, true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("UTF-8"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
long process = localSize / step;
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0)
System.out.println("下载进度:" + process);
// TODO 更新文件下载进度,值存放在process变量中
}
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if (isDo) {
result = DownloadStatus.Download_From_Break_Success;
} else {
result = DownloadStatus.Download_From_Break_Failed;
}
} else {
OutputStream out = new FileOutputStream(f);
InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("UTF-8"), "iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize / 100;
long process = 0;
long localSize = 0L;
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
localSize += c;
long nowProcess = localSize / step;
if (nowProcess > process) {
process = nowProcess;
if (process % 10 == 0)
System.out.println("下载进度:" + process);
// TODO 更新文件下载进度,值存放在process变量中
}
}
in.close();
out.close();
boolean upNewStatus = ftpClient.completePendingCommand();
if (upNewStatus) {
result = DownloadStatus.Download_New_Success;
} else {
result = DownloadStatus.Download_New_Failed;
}
}
return result;
}

private void disconnect() throws IOException {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
}

}

㈣ 请教关于FtpSetCurrentDirectory的用法

我猜你应该使用一个JFileChooser对象的酒吧,里面有一个叫changeToParentDirectory()方法方法,该方法将被调整到当前目录,比如C盘或D盘的根目录,还有另一种方法叫做setCurrentDirectory(文件中的文件)的方法,这种方法可以直接指定当前目

㈤ 批处理FTP文件及报错,请大神帮忙看看,谢谢

ftp 的语句里没发现明显错误,你把
ftp -n那句改成
ftp -s:"%ftpfile%" >> "%logfile%"
试试,还不行的话,把最后那句del删掉运行批处理,然后打开一个cmd窗口,按照生成的putfile.ftp的命令一行一行手工执行下,看看哪一句出了问题,这是处理这类问题的一个思路。

㈥ java ftpclient filetype有哪些类型

切换工作路径啊,
ftpClient.changeWorkingDirectory("/admin/pic"); //工作路径切换到/admin/pic ftpClient.setBufferSize(1024); //设置1M缓冲
ftpClient.setControlEncoding("GBK"); //设置编码为GBK

ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //文件类型为二进制文件
ftpClient.storeFile("3.gif", fis)//在前面设置好路径,缓冲,编码,文件类型后,开始上传3.gif

㈦ 批处理复制文件到ftp服务器

将以下内容复制到文本当中,修改ftp的地址、用户、密码、端口保存,然后将格式修改成cmd或bat,双击运行即可。
@echo off
rem ftp地址
set ftpAddr=192.168.1.20
rem ftp用户
set ftpUser=upload
rem ftp密码
set ftpPwd=123456
rem ftp端口
set ftpPort=21
:input
set /p file=输入文件或将文件拖至本窗口:
if not exist %file% echo 文件不存在 && goto input
echo open %ftpAddr% %ftpPort%>openFtp.txt
echo %ftpUser%>>openFtp.txt
echo %ftpPwd%>>openFtp.txt
echo put %var%>>openFtp.txt
echo bye>>openFtp.txt
ftp -s:openFtp.txt
del openFtp.txt
pause

热点内容
大嘴脚本是什么 发布:2024-10-29 06:20:59 浏览:204
电脑批量压缩图片软件 发布:2024-10-29 06:14:39 浏览:719
php邮件内容 发布:2024-10-29 06:14:33 浏览:835
伪装者缓存 发布:2024-10-29 06:12:19 浏览:786
原神怎么调低手机配置 发布:2024-10-29 06:12:01 浏览:1000
安卓怎么安装starwalk 发布:2024-10-29 05:49:20 浏览:831
安卓怎么关闭ipodspro主动降噪 发布:2024-10-29 05:49:17 浏览:769
霹雳天命之斩魔录ftp 发布:2024-10-29 05:19:48 浏览:345
2b的2t服务器是什么 发布:2024-10-29 05:08:25 浏览:600
设有递推算法 发布:2024-10-29 04:54:21 浏览:164