分享

ftp上传、下载文件工具类

 嗖叭鸟 2014-01-25
package cn.microvideo.util.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import sun.net.TelnetInputStream;

import cn.microvideo.util.PropertyReader;

public class FTPTools {
private String ftp_server;//server
private String ftp_user;//user
private String ftp_password;//pwd
private String projectDir;//upload path
private PropertyReader pr;//read config
public FTPTools(){
pr = new PropertyReader("fileupload.properties");
ftp_server = pr.getProperty("ftp_server");
ftp_user =pr.getProperty("ftp_user");
ftp_password =pr.getProperty("ftp_password");
   
projectDir = pr.getProperty("projectDir");
}
/**
* 上传
* @param localPath 文件的本地路径
*/
public void upload(String localPath){
FTPTools t = new FTPTools();  
        try {
boolean flag=t.connect(projectDir, ftp_server, 21, ftp_user, ftp_password);
if (flag) {
File file=new File(localPath);
t.upload(file);
}
} catch (Exception e) {
e.printStackTrace();
}  
}
/**
* 下载
* @param remotePath 文件所在的FTP绝对路径
*/
public InputStream download(String remotePath){
FTPTools t = new FTPTools();  
InputStream is=null;
        try {
boolean flag=t.connect(projectDir, ftp_server, 21, ftp_user, ftp_password);
if (flag) {
File file=new File(remotePath);
is=t.download(file);
}
} catch (Exception e) {
e.printStackTrace();
}  
return is;
}
//------------
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();
}
}
/**
* 下载文件
* @param file
* @throws IOException
*/
private InputStream download(File file) {
InputStream is=null;
try {
is=ftp.retrieveFileStream(file.getName());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多