FTP inputstream下载测试成功
This commit is contained in:
@@ -5,6 +5,7 @@ import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
@@ -12,8 +13,8 @@ import java.io.*;
|
||||
* @date 2018-05-06 11:35
|
||||
*/
|
||||
public class FtpUtils {
|
||||
private static final String FTP_URL = "176.122.138.235";
|
||||
// private static final String FTP_URL = "192.168.79.138";
|
||||
// private static final String FTP_URL = "176.122.138.235";
|
||||
private static final String FTP_URL = "192.168.79.138";
|
||||
private static final int PORT = 21;
|
||||
private static final String USER_NAME = "cms";
|
||||
private static final String PASSWORD = "imis2";
|
||||
@@ -37,16 +38,13 @@ public class FtpUtils {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean upload(String directory, String srcFileName, String destName) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
destName = new String(destName.getBytes("UTF-8"),"iso-8859-1");
|
||||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||||
destName = new String(destName.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
FileInputStream fis = null;
|
||||
boolean result = false;
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
File srcFile = new File(srcFileName);
|
||||
fis = new FileInputStream(srcFile);
|
||||
|
||||
@@ -85,12 +83,9 @@ public class FtpUtils {
|
||||
* @param downloadName 下载后存储的文件名全路径
|
||||
*/
|
||||
public static boolean download(String directory, String destFileName, String downloadName) throws IOException {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
boolean result = false;
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setBufferSize(1024);
|
||||
|
||||
// 设置文件类型(二进制)
|
||||
@@ -119,11 +114,11 @@ public class FtpUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||
* @param remotePath
|
||||
* @param fileName
|
||||
* @param localPath
|
||||
* @return
|
||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||
*/
|
||||
public static boolean downFtpFile(String remotePath, String fileName,
|
||||
String localPath) {
|
||||
@@ -148,7 +143,7 @@ public class FtpUtils {
|
||||
for (FTPFile ff : fs) {
|
||||
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
||||
if (fname.equals(fileName)) {
|
||||
File localFile = new File(localPath+fname);
|
||||
File localFile = new File(localPath + fname);
|
||||
OutputStream is = new FileOutputStream(localFile);
|
||||
ftp.retrieveFile(ff.getName(), is);
|
||||
is.close();
|
||||
@@ -171,15 +166,16 @@ public class FtpUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||
* @param remotePath
|
||||
* @param fileName
|
||||
* @param localPath
|
||||
* @return
|
||||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||||
*/
|
||||
public static boolean downFtpFileByStream(String remotePath, String fileName,
|
||||
String localPath) {
|
||||
public static OutputStream downFtpFileByStream(String fileName) {
|
||||
|
||||
String remotePath = "/www/ftp/cms/";
|
||||
boolean success = false;
|
||||
OutputStream is = null;
|
||||
|
||||
FTPClient ftp = new FTPClient();
|
||||
try {
|
||||
int reply;
|
||||
@@ -190,7 +186,7 @@ public class FtpUtils {
|
||||
reply = ftp.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
ftp.disconnect();
|
||||
return success;
|
||||
return null;
|
||||
}
|
||||
// 转移到FTP服务器目录
|
||||
ftp.changeWorkingDirectory(remotePath);
|
||||
@@ -200,11 +196,10 @@ public class FtpUtils {
|
||||
for (FTPFile ff : fs) {
|
||||
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
||||
if (fname.equals(fileName)) {
|
||||
File localFile = new File(localPath+fname);
|
||||
OutputStream is = new FileOutputStream(localFile);
|
||||
File localFile = new File(fname);
|
||||
is = new FileOutputStream(localFile);
|
||||
ftp.retrieveFile(ff.getName(), is);
|
||||
is.close();
|
||||
break;
|
||||
return is;
|
||||
}
|
||||
}
|
||||
ftp.logout();
|
||||
@@ -212,6 +207,12 @@ public class FtpUtils {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
assert is != null;
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (ftp.isConnected()) {
|
||||
try {
|
||||
ftp.disconnect();
|
||||
@@ -219,7 +220,7 @@ public class FtpUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
return success;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -230,19 +231,16 @@ public class FtpUtils {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean rename(String directory, String oldFileName, String newFileName) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
oldFileName = new String(oldFileName.getBytes("UTF-8"),"iso-8859-1");
|
||||
newFileName = new String(newFileName.getBytes("UTF-8"),"iso-8859-1");
|
||||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||||
oldFileName = new String(oldFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
newFileName = new String(newFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否重命名成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
//重命名远程文件
|
||||
result = ftpClient.rename(oldFileName, newFileName);
|
||||
@@ -267,18 +265,15 @@ public class FtpUtils {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean remove(String directory, String fileName) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
|
||||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
//删除远程文件
|
||||
result = ftpClient.deleteFile(fileName);
|
||||
@@ -303,18 +298,15 @@ public class FtpUtils {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean makeDirecotory(String directory, String newDirectory) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
newDirectory = new String(newDirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||||
newDirectory = new String(newDirectory.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
result = ftpClient.makeDirectory(newDirectory);//创建新目录
|
||||
return result;
|
||||
@@ -339,17 +331,14 @@ public class FtpUtils {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean renameDirecotory(String directory, String oldDirectory, String newDirectory) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
oldDirectory = new String(oldDirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
newDirectory = new String(newDirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||||
oldDirectory = new String(oldDirectory.getBytes("UTF-8"), "iso-8859-1");
|
||||
newDirectory = new String(newDirectory.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
// 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
// 重命名目录
|
||||
result = ftpClient.rename(oldDirectory, newDirectory);
|
||||
@@ -374,18 +363,15 @@ public class FtpUtils {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static boolean removeDirecotory(String directory, String deldirectory) throws IOException {
|
||||
directory = new String(directory.getBytes("UTF-8"),"iso-8859-1");
|
||||
deldirectory = new String(deldirectory.getBytes("UTF-8"),"iso-8859-1");
|
||||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||||
deldirectory = new String(deldirectory.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
/**
|
||||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||||
*/
|
||||
boolean result = false;
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
result = ftpClient.removeDirectory(deldirectory);//删除目录
|
||||
return result;
|
||||
@@ -408,17 +394,12 @@ public class FtpUtils {
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String[] list(String directory) throws IOException {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
FTPClient ftpClient = makeConnectionFactory();
|
||||
try {
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||||
ftpClient.changeWorkingDirectory(directory);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
//删除目录
|
||||
String[] list = ftpClient.listNames();
|
||||
return list;
|
||||
return ftpClient.listNames();
|
||||
} catch (NumberFormatException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
@@ -431,4 +412,19 @@ public class FtpUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建FTP连接的工厂方法
|
||||
*
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private static FTPClient makeConnectionFactory() throws IOException {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
ftpClient.connect(FTP_URL, PORT);
|
||||
ftpClient.login(USER_NAME, PASSWORD);
|
||||
ftpClient.enterLocalPassiveMode();
|
||||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||||
return ftpClient;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user