您的位置:首页 > 编程语言 > Java开发

JAVA Ftp实现文件上传于下载

2013-02-19 18:45 691 查看
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件

本文简单介绍如何利用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);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: