通过FTP协议进行文件的上传下载
我们在做WEB项目时,往往会遇到图片的上传与下载,在这里我说一下java是如何通过ftp协议进行文件上传和下载的。不足之处,还请提出您宝贵的意见,话不多说,代码如下:
package fw;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
public class FTPUtil {
private static Logger logger = Logger.getLogger(FTPUtil.class);
public FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) {
FTPClient ftp = null;
try {
ftp = new FTPClient();
ftp.connect(ftpHost, ftpPort);
ftp.login(ftpUserName, ftpPassword);
ftp.setConnectTimeout(50000);
ftp.setControlEncoding("UTF-8");
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
logger.info("未连接到FTP,用户名或密码错误");
ftp.disconnect();
} else {
logger.info("FTP连接成功");
}
} catch (SocketException e) {
e.printStackTrace();
logger.info("FTP的IP地址可能错误,请正确配置");
} catch (IOException e) {
e.printStackTrace();
logger.info("FTP的端口错误,请正确配置");
}
return ftp;
}
public boolean closeFTP(FTPClient ftp) {
try {
ftp.logout();
} catch (Exception e) {
logger.error("FTP关闭失败");
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
logger.error("FTP关闭失败");
}
}
}
return false;
}
public boolean downLoadFTP(FTPClient ftp, String filePath, String fileName, String downPath) {
boolean flag = false;
try {
ftp.changeWorkingDirectory(new String(filePath.getBytes("GBK"), "ISO-8859-1"));
ftp.enterLocalPassiveMode();
FTPFile[] files = ftp.listFiles();
for (FTPFile file : files) {
if (file.getName().equals(fileName)) {
File downFile = new File(downPath + "/" + file.getName());
OutputStream out = new FileOutputStream(downFile);
flag = ftp.retrieveFile(new String(file.getName().getBytes("GBK"), "ISO-8859-1"), out);
out.flush();
out.close();
if (flag) {
logger.info("下载成功");
} else {
logger.error("下载失败");
}
}
}
} catch (Exception e) {
logger.error("下载失败");
}
return flag;
}
public boolean uploadFile(FTPClient ftp, String filePath, String ftpPath) {
boolean flag = false;
InputStream in = null;
try {
ftp.enterLocalPassiveMode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
if (!ftp.changeWorkingDirectory(ftpPath)) {
ftp.makeDirectory(ftpPath);
}
ftp.changeWorkingDirectory(ftpPath);
File file = new File(filePath);
in = new FileInputStream(file);
String tempName = ftpPath + "/" + file.getName();
System.out.println("路径:" + tempName);
flag = ftp.storeFile(new String(tempName.getBytes("GBK"), "ISO-8859-1"), in);
System.out.println("true or false?:" + flag);
if (flag) {
logger.info("上传成功");
} else {
logger.error("上传失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("上传失败");
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
public boolean readFileByFolder(FTPClient ftp, String folderPath) {
boolean flage = false;
try {
ftp.changeWorkingDirectory(new String(folderPath.getBytes("UTF-8"), "ISO-8859-1"));
ftp.enterLocalPassiveMode();
FTPFile files[] = ftp.listFiles();
InputStream in = null;
BufferedReader reader = null;
for (FTPFile file : files) {
if (file.isFile()) {
String fileName = file.getName();
if (fileName.endsWith(".txt")) {
in = ftp.retrieveFileStream(new String(file.getName().getBytes("UTF-8"), "ISO-8859-1"));
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String temp;
StringBuffer buffer = new StringBuffer();
while ((temp = reader.readLine()) != null) {
buffer.append(temp);
}
if (reader != null) {
reader.close();
}
if (in != null) {
in.close();
}
ftp.completePendingCommand();
System.out.println(buffer.toString());
}
}
if (file.isDirectory()) {
String path = folderPath + File.separator + file.getName();
readFileByFolder(ftp, path);
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error("文件解析失败");
}
return flage;
}
public static void main(String[] args) {
FTPUtil test = new FTPUtil();
FTPClient ftp = test.getFTPClient("ftpHost", 21, "ftpUserName", "ftpPassword");
test.readFileByFolder(ftp, "/");
test.closeFTP(ftp);
System.exit(0);
}
}