JAVA Ftp实现文件上传于下载
2013-02-19 18:45
691 查看
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件
本文简单介绍如何利用FTPClient(在commons-net包中)实现上传下载文件。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil {
private final static String module = FtpUtil.class.getName();
private static InitLog log;
private static FTPClient ftp = null;
public static Map<String,String> putFile (Map<String,Object> context){
log.logInfo("start put file", module);
Map<String,String> retMap = new HashMap<String,String>();
String fileName = (String)context.get("fileName");
//本地文件路径 "D:/ftp"
String path = (String)context.get("path");
FileInputStream input =(FileInputStream)context.get("input");
ftp = new FTPClient();
Properties p = null;
try {
p = AnalyzingFile.getProperties("IPMessage.properties");
} catch (IOException e) {
log.logDebug("加载配置文件错误", module);
e.printStackTrace();
}
String url = AnalyzingFile.getStringProperty(p, "url", null);
Integer port = AnalyzingFile.getIntegerProperty(p, "port", 0);
String loginName = AnalyzingFile.getStringProperty(p, "loginName", null);
String password = AnalyzingFile.getStringProperty(p, "password", null);
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
Boolean loginFlag = ftp.login(loginName, password);//登录
if(!loginFlag){
retMap.put("result", "error");
return retMap;
}
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
retMap.put("result", "success");
return retMap;
}
//log.logInfo("workingDirectory is :"+ftp.get, password)
Boolean changeFlag = ftp.changeWorkingDirectory(path);
if(!changeFlag){
retMap.put("result", "error");
return retMap;
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.storeFile(fileName, input);
input.close();
ftp.logout();
retMap.put("result", "success");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return retMap;
}
public static Map<String,String> downFile (Map<String,Object> context){
log.logInfo("start down file", module);
Map<String,String> retMap = new HashMap<String,String>();
String fileName = (String)context.get("fileName");
//remotePath
String remotePath = (String)context.get("remotePath");
String localPath = (String)context.get("localPath");
// FileOutputStream output =(FileOutputStream)context.get("output");
ftp = new FTPClient();
Properties p = null;
try {
p = AnalyzingFile.getProperties("IPMessage.properties");
} catch (IOException e) {
log.logDebug("加载配置文件错误", module);
e.printStackTrace();
}
String url = AnalyzingFile.getStringProperty(p, "url", null);
Integer port = AnalyzingFile.getIntegerProperty(p, "port", 0);
String loginName = AnalyzingFile.getStringProperty(p, "loginName", null);
String password = AnalyzingFile.getStringProperty(p, "password", null);
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
Boolean loginFlag = ftp.login(loginName, password);//登录
if(!loginFlag){
retMap.put("result", "error");
return retMap;
}
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
retMap.put("result", "success");
return retMap;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
/*
* 获取所有文件夹下所有文件
* FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"/"+ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
} */
File localFile = new File(localPath+"/"+fileName);
OutputStream output = new FileOutputStream(localFile);
ftp.retrieveFile(fileName, output);
output.close();
ftp.logout();
retMap.put("result", "success");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return retMap;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.lwd.util.FtpUtil;
public class TestFtp {
private static final String module = TestFtp.class.getName();
public static void main(String[] args) {
/*FileInputStream input = null;
try {
input = new FileInputStream(new File("D:/ftp/WebContent.war"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String,Object> context = new HashMap<String,Object>();
context.put("path", "20130219");
context.put("fileName", "WebContent.war");
context.put("input", input);
Map<String,String> retMap = new HashMap<String,String>();
retMap = FtpUtil.putFile(context);
if("success".equals(retMap.get("result"))){
System.out.println("upload success");
}else{
System.out.println("upload error!");
}*/
Map<String,Object> context = new HashMap<String,Object>();
context.put("remotePath", "20130219");
context.put("fileName", "WebContent.war");
context.put("localPath", "D:/ftp/20130219");
Map<String,String> retMap = new HashMap<String,String>();
retMap = FtpUtil.downFile(context);
if("success".equals(retMap.get("result"))){
System.out.println("upload success");
}else{
System.out.println("upload error!");
}
System.exit(0);
}
}
2、下载时,可以批量下载
3、IO流一定要关闭
4、出现
JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error .
本文简单介绍如何利用FTPClient(在commons-net包中)实现上传下载文件。
FTPUtil工具类,实现上传与下载package com.lwd.util;
package com.lwd.util;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil {
private final static String module = FtpUtil.class.getName();
private static InitLog log;
private static FTPClient ftp = null;
public static Map<String,String> putFile (Map<String,Object> context){
log.logInfo("start put file", module);
Map<String,String> retMap = new HashMap<String,String>();
String fileName = (String)context.get("fileName");
//本地文件路径 "D:/ftp"
String path = (String)context.get("path");
FileInputStream input =(FileInputStream)context.get("input");
ftp = new FTPClient();
Properties p = null;
try {
p = AnalyzingFile.getProperties("IPMessage.properties");
} catch (IOException e) {
log.logDebug("加载配置文件错误", module);
e.printStackTrace();
}
String url = AnalyzingFile.getStringProperty(p, "url", null);
Integer port = AnalyzingFile.getIntegerProperty(p, "port", 0);
String loginName = AnalyzingFile.getStringProperty(p, "loginName", null);
String password = AnalyzingFile.getStringProperty(p, "password", null);
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
Boolean loginFlag = ftp.login(loginName, password);//登录
if(!loginFlag){
retMap.put("result", "error");
return retMap;
}
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
retMap.put("result", "success");
return retMap;
}
//log.logInfo("workingDirectory is :"+ftp.get, password)
Boolean changeFlag = ftp.changeWorkingDirectory(path);
if(!changeFlag){
retMap.put("result", "error");
return retMap;
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.storeFile(fileName, input);
input.close();
ftp.logout();
retMap.put("result", "success");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return retMap;
}
public static Map<String,String> downFile (Map<String,Object> context){
log.logInfo("start down file", module);
Map<String,String> retMap = new HashMap<String,String>();
String fileName = (String)context.get("fileName");
//remotePath
String remotePath = (String)context.get("remotePath");
String localPath = (String)context.get("localPath");
// FileOutputStream output =(FileOutputStream)context.get("output");
ftp = new FTPClient();
Properties p = null;
try {
p = AnalyzingFile.getProperties("IPMessage.properties");
} catch (IOException e) {
log.logDebug("加载配置文件错误", module);
e.printStackTrace();
}
String url = AnalyzingFile.getStringProperty(p, "url", null);
Integer port = AnalyzingFile.getIntegerProperty(p, "port", 0);
String loginName = AnalyzingFile.getStringProperty(p, "loginName", null);
String password = AnalyzingFile.getStringProperty(p, "password", null);
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
Boolean loginFlag = ftp.login(loginName, password);//登录
if(!loginFlag){
retMap.put("result", "error");
return retMap;
}
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
retMap.put("result", "success");
return retMap;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
/*
* 获取所有文件夹下所有文件
* FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"/"+ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
} */
File localFile = new File(localPath+"/"+fileName);
OutputStream output = new FileOutputStream(localFile);
ftp.retrieveFile(fileName, output);
output.close();
ftp.logout();
retMap.put("result", "success");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return retMap;
}
}
2 测试类TestFtp
package com.lwd.test;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.lwd.util.FtpUtil;
public class TestFtp {
private static final String module = TestFtp.class.getName();
public static void main(String[] args) {
/*FileInputStream input = null;
try {
input = new FileInputStream(new File("D:/ftp/WebContent.war"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String,Object> context = new HashMap<String,Object>();
context.put("path", "20130219");
context.put("fileName", "WebContent.war");
context.put("input", input);
Map<String,String> retMap = new HashMap<String,String>();
retMap = FtpUtil.putFile(context);
if("success".equals(retMap.get("result"))){
System.out.println("upload success");
}else{
System.out.println("upload error!");
}*/
Map<String,Object> context = new HashMap<String,Object>();
context.put("remotePath", "20130219");
context.put("fileName", "WebContent.war");
context.put("localPath", "D:/ftp/20130219");
Map<String,String> retMap = new HashMap<String,String>();
retMap = FtpUtil.downFile(context);
if("success".equals(retMap.get("result"))){
System.out.println("upload success");
}else{
System.out.println("upload error!");
}
System.exit(0);
}
}
3 注意问题
1、上传时FTP所接收文件路径问题,最好写相对路径2、下载时,可以批量下载
3、IO流一定要关闭
4、出现
JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error .
解决System.exit(0);
相关文章推荐
- java操作ftp实现文件的上传下载(适用于图片文档服务器)
- JAVA中使用FTPClient实现文件上传下载
- Java连接FTP服务器并且实现对其文件的上传和下载
- JAVA中使用FTPClient实现文件上传下载
- Java实现FTP文件与文件夹的上传和下载
- java操作FTP,实现文件上传下载删除操作
- Java实现FTP文件上传与下载
- java实现ftp文件的上传与下载
- JAVA中使用FTPClient实现文件上传下载
- java操作FTP,实现文件上传下载删除操作
- JAVA中使用FTPClient实现文件上传下载
- JAVA中使用FTPClient实现文件上传下载实例代码
- Java代码使用FTPClient实现ftp文件上传下载和删除的功能
- java实现文件上传下载至ftp服务器
- Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题
- Java实现FTP文件上传与下载
- Java中实现FTP文件上传下载 涉及中文路径以及中文文件
- java实现ftp文件上传下载功能
- JAVA中使用FTPClient实现文件上传下载
- Java实现FTP服务器的上传、下载、删除、查看文件列表