1. 背景
这两天在写一个jsp应用,涉及客户端上传一批pdf文件到服务端,然后取出来处理,再将相关结果返回。
开始想用 org.apache.commons.fileupload , 用表单的方式上传, 但是这样只能上传单个文件,批上传则要在一个
form里做多个上传的input,不太方便,所以后来想用ftp的方式,让用户选择本地一个文件夹,点击后整个上传到服务端的一个文件夹(网页的局限性导致无法直接由jsp获取文件夹目录,网上说好像要用 applet 做,现在还没搞清楚)。
2. 网络资源
网上找到的ftp上传主要有两种:一种是 sun.net.ftp ,据说sun包都不是标准包,所以没有javadoc,可以到www.sun.com 去下载源码,
我找到了一个中文介绍:http://xhy0422.iteye.com/blog/63017 ,
还有一个包含 ftp 的远程命令列表的详细使用 http://blog.sina.com.cn/s/blog_4b898bdb010009xf.html
第二种是 com.enterprisedt.net.ftp ,它是一个第三方包,功能比 sun.net.ftp 强大,找到一个比较详细的说明
http://blog.163.com/cjp1320@126/blog/static/3547436320096212286312/
有了以上几个链接,基本上就能实现 ftp 的相关功能了,这里再插下题外话,就是sun.net.ftp究竟是怎么实现的呢?我没有看过它的源码,但是看了那些方法之后,估计是对一般的ftp命令的封装,内部可能是用 socket 做的,使用时你会看到,一些方法里面很多命令行控制的影子,比如创建文件夹的核心语句 ftpClient.sendServer("MKD " + pathName + "\r\n");
3.异常总结
在实际使用过程中,出现了一些错误,网上看了一下,几乎没遇到好的说明的,所以总结一下,
我在测试 sun.net.ftp 的时候,发现一个问题,就是老是出现
java.io.FileNotFoundException: CWD F:\test\test4: 550 CWD failed. "/F:/test/test4": directory not found.
的异常,其中,F:\\test\\test4 是我在服务端想要创建的文件夹,层层定位进去,发现创建文件夹那句ftpClient.sendServer("MKD " + pathName + "\r\n");并没有抛出异常,而是创建完后 cd(pathname)出现找不到文件夹的错误。远程登录到服务器去看,文件夹并没有创建。
折腾了一下午后,才发现,代码是没错的,错在服务器端的保存路径“F:\\test\\test4 ”设错了,应该改为“\\test\\test4 ”就是将盘符去掉,结果一运行就可以了,远程登录看到在设为ftp服务域的文件夹下有了test/test4文件夹,里边打开有了上传的pdf文件。
思考后发现,应该是服务器安装ftp服务器的时候,会选一个文件夹作为服务域(就是在本地用ftp工具登录过去看到的那个文件夹),所以,如果你在代码中设的目录像我前面那样“F:\\test\\test4 ”包含盘符就是违法的,因为F盘根目录没有在服务区域,改为“\\test\\test4 ”后,运行后是将这个目录连在服务域根目录之下。
补充:查看网络帖子的过程中,发现一个比较多的错误,就是创建文件夹不成功的错误,原因是在创建语句ftpClient.sendServer("MKD " + pathName + "\r\n");后边缺了得到服务器端响应的语句ftpClient.readServerResponse();加上去就可以了,这类错误的异常是
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT command not understood
【奇怪的是,我自己测的时候去掉后面那句,还是可以执行成功,估计是不是权限方面的问题,希望研究过sun.net.ftp源码的朋友赐教一下】
4.我的sun.net.ftp 的源码
package common;
//转载请标明出处:http://blog.csdn.net/yancong008/archive/2008/11/18/3327088.aspx
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
import sun.net.ftp.FtpClient;
/**
* ftp上传
*
* @author colin
* @version 1.0
*
*/
public class MyFtp {
private String ip = "";
private String username = "";
private String password = "";
private int port = 21;
private String localFileDir = "";// 需要上传的目录
private String remoteFileDir = "";
FtpClient ftpClient = null;
OutputStream os = null;
FileInputStream is = null;
public MyFtp(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
}
public MyFtp(String serverIP, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
}
/**
* 创建文件夹
*
* @param dir
* @param ftpClient
* @throws Exception
*/
private void createDir(String dir, FtpClient ftpClient) throws Exception {
ftpClient.ascii();
StringTokenizer s = new StringTokenizer(dir, "/"); // sign
int count = s.countTokens();
String pathName = "";
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpClient.sendServer("MKD " + pathName + "\r\n");
System.out.println("success make"+pathName);
} catch (Exception e) {
System.out.println(e);
e = null;
}
ftpClient.readServerResponse();
}
ftpClient.binary();
}
/**
* 检查文件夹是否存在
*
* @param dir
* @param ftpClient
* @return
*/
private Boolean isDirExist(String dir, FtpClient ftpClient) {
try {
ftpClient.cd(dir);
} catch (Exception e) {
return false;
}
return true;
}
/**
*
* @param ftpClient
*/
public void testCreatDir(FtpClient ftpClient){
String path = "F:/IM_FTP/test";
try{
ftpClient.sendServer("MKD " + path + "\r\n");
ftpClient.readServerResponse();
}catch(Exception e){
System.out.println(e);
}
}
/**
* ftp上传
*
* @param localFileFullName
* 上传的源文件夹
* @return
*/
public Boolean upload(String localFileDir, String remoteFileDir) {
this.localFileDir = localFileDir;
this.remoteFileDir = remoteFileDir;
try {
// 中文路径转换编码,本机操作系统默认编码是'GBK'
String localfilepath = new String(localFileDir.getBytes("GBK"),
"utf-8");
// 新建一个FTP客户端连接
ftpClient = new FtpClient();
ftpClient.openServer(this.ip, this.port);
ftpClient.login(this.username, this.password);
ftpClient.cd("/");
// System.out.println(ftpClient.welcomeMsg);//报告连接信息
// 打开本地待上传的文件
// testCreatDir(ftpClient);
File file_in = new File(localfilepath);
processFile(file_in, ftpClient);
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (ftpClient != null) {
ftpClient.closeServer();
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception e in Ftp upload(): " + e.toString());
return false;
}
}
/**
* 上传文件
*
* @param source
* @param ftpClient
* @throws Exception
*/
private void processFile(File source, FtpClient ftpClient) throws Exception {
if (source.exists()) {
if (source.isDirectory()) {
System.out.println("source:" + source.getPath());
if (!isDirExist(this.remoteFileDir, ftpClient)) {
createDir(this.remoteFileDir, ftpClient);
}
File sourceFile[] = source.listFiles();
for (int i = 0; i < sourceFile.length; i++) {
if (sourceFile[i].exists()) {
if (sourceFile[i].isDirectory()) {
this.processFile(sourceFile[i], ftpClient);
} else {
System.out.println("success1");
ftpClient.cd(this.remoteFileDir);
System.out.println("success2");
ftpClient.binary();
String fname = sourceFile[i].getName();
os = ftpClient.put(fname);
byte[] bytes = new byte[1024];
is = new FileInputStream(sourceFile[i]);
// 开始复制
int c;
// 暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
}
}
}
} else {
ftpClient.cd(this.remoteFileDir);
ftpClient.binary();
os = ftpClient.put(source.getName());
byte[] bytes = new byte[1024];
is = new FileInputStream(source);
// 开始复制
int c;
// 暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
}
} else {
throw new Exception("此文件或文件夹[" + source.getName() + "]有误或不存在!");
}
}
public static void main(String args[]) throws Exception {
MyFtp ftpup = new MyFtp("202.116.73.50", 2345, "**", "**");
ftpup.upload("F:\\chen_pdf_processor\\pdf_test_data2", "\\test\\test4");
}
}
分享到:
相关推荐
本书是第II卷,以开发人员在项目开发中经常遇到的问题和必须掌握的技术为中心,介绍了应用Java进行桌面程序开发各个方面的知识和技巧,主要包括Java语法与面向对象技术、Java高级应用、窗体与控件应用、文件操作...
在JAVA开发领域,了解面经知识点是非常重要的。本文将从JDK代理和CGlib代理开始,详细介绍抽象类和接口、数据库范式、TCP三次握手和四次挥手、数据库各种锁、HTTP长连接和短连接、TCP和UDP的区别、POST和GET的区别、...
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
Java maven netty Redis Spring Spring4新特性 Spring使用注意事项 Spring验证Validation SpringBoot 开发知识 相关技术名词 开发技术框架工具整理 架构知识 开发过程注意事项整理 常用开发技巧 数据库 数据库基础...
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....
知识点整理及分享 课程项目环境 centos下载地址 阿里云的下载已经失效,请前往官网下载地址下载。 如自己找软件比较麻烦,可以访问 如自己配置比较麻烦,可以参考线上配置 接口文档: SQL导入,可以通过下载的mmall....