FTP 上精确取文件,提供byte[]传出,再转本地文件
来源:互联网 发布:网络测网速 编辑:程序博客网 时间:2024/05/16 11:44
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @khj
* @title FtpUtils
* @Description : FTP 上传下载工具类
* @time
*/
public class FTPUtils
{
/**
* 日志
*/
private static Logger logger = LoggerFactory.getLogger(FTPUtils.class);
private static FTPClient ftp;
/**
* 连接ftp <一句话功能简述> <功能详细描述>
*
* @param addr
* @param port
* @param username
* @param password
* @return
* @see [类、类#方法、类#成员]
*/
public static boolean connect(String addr, int port, String username, String password)
{
ftp = new FTPClient();
try
{
ftp.connect(addr, 21);
ftp.login(username, password);
}
catch (SocketException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return ftp.isConnected();
}
@SuppressWarnings("unused")
private void upload(File file, String path)
{
logger.info(" file.isDirectory() : {}" + file.isDirectory());
try
{
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, path);
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());
System.out.println(" file.getPath() : " + file.getPath() + " | file2.getName() : " + file2.getName());
InputStream input = new FileInputStream(file2);
ftp.changeWorkingDirectory(path);
ftp.storeFile(file2.getName(), input);
input.close(); // 关闭输入流
ftp.logout(); // 退出连接
}
}
catch (Exception e)
{
logger.error(e.getMessage());
}
}
@SuppressWarnings("static-access")
public static byte[] downloadFile(String fileName)
{
byte[] bytes = null;
try
{
ftp.changeWorkingDirectory("");
// 列出该目录下所有文件
FTPFile[] fs = ftp.listFiles(fileName);
// 遍历所有文件,找到指定的文件
for (FTPFile file : fs)
{
if (file.getName().equals(fileName))
{
// 根据绝对路径初始化文件
// File localFile = new File(localPath);
// if (!localFile.exists())
// {
// localFile.createNewFile();
// }
// // 输出流
// OutputStream os = new FileOutputStream(localFile);
InputStream in = null;
// 下载文件
ftp.setBufferSize(1024);
ftp.setControlEncoding("UTF-8");
ftp.setFileType(ftp.BINARY_FILE_TYPE);
// 下载文件到 localFile
// ftp.retrieveFile(ff.getName(), os);
String remoteAbsoluteFile = file.getName();
remoteAbsoluteFile = new String(remoteAbsoluteFile.getBytes("UTF-8"), "ISO-8859-1");
in = ftp.retrieveFileStream(remoteAbsoluteFile);
bytes = input2byte(in);
System.out.println("下载成功!" + bytes.length);
in.close();
}
}
ftp.logout(); // 退出连接
}
catch (Exception e)
{
e.printStackTrace();
}
return bytes;
}
/**
* byte[] 转 file
*
* @param bytes
* @param path
* @return
* @see [类、类#方法、类#成员]
*/
public static void byteToFile(byte[] bytes, String path)
{
try
{
// 根据绝对路径初始化文件
File localFile = new File(path);
if (!localFile.exists())
{
localFile.createNewFile();
}
// 输出流
OutputStream os = new FileOutputStream(localFile);
os.write(bytes);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 文件转成 byte[] <一句话功能简述> <功能详细描述>
*
* @param inStream
* @return
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static byte[] input2byte(InputStream inStream)
throws IOException
{
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0)
{
swapStream.write(buff, 0, rc);
}
byte[] in2b = swapStream.toByteArray();
swapStream.close();
return in2b;
}
public static void getPhoto(String filePath,String localPath)
{
FTPUtils.connect(ReadPropertiesUtils.getParamValue("ftp.host"),
21,
ReadPropertiesUtils.getParamValue("ftp.username"),
ReadPropertiesUtils.getParamValue("ftp.password"));
logger.info("");
byte[] bytes = FTPUtils.downloadFile(filePath);
FTPUtils.byteToFile(bytes, localPath);
}
public static byte[] getPhoto(String filePath)
{
FTPUtils.connect(ReadPropertiesUtils.getParamValue("ftp.host"),
21,
ReadPropertiesUtils.getParamValue("ftp.username"),
ReadPropertiesUtils.getParamValue("ftp.password"));
logger.info("");
byte[] bytes = FTPUtils.downloadFile(filePath);
//FTPUtils.byteToFile(bytes, localPath);
return bytes;
}
public static void main(String[] args)
throws Exception
{
String filePath = "T_ZDRY_IDX_JXXX_INC/010020090902731604.jpg";
String localPath = "d:\\ccc.jpg";
FTPUtils.getPhoto(filePath, localPath);
}
}
0 0
- FTP 上精确取文件,提供byte[]传出,再转本地文件
- 【转】C# 上传本地文件至ftp上的方法
- 将本地文件上传到Ftp上的一些操作【转】
- Asp.net(c#)取ftp上 文件信息
- 取本地文件
- 将本地文件上传到Ftp上的一些操作
- C# 上传本地文件至ftp上的方法
- 利用FtpClient取FTP文件
- ftp取文件 代码 测试
- 用svn上的文件,覆盖本地文件
- Beyond Compare文件上传 FTP需要匹配本地文件吗
- 将本地文件上传到远程FTP服务器和从远程FTP服务器下载文件到本地
- 使用Kettle从FTP拉取文件
- 文件转byte[]
- 在Eclipse中,使用Aptana同步更新本地文件与FTP文件.
- 提供精确的加减乘除运算
- jsp如何下载FTP上的文件---ftp协议方式
- jsp如何下载FTP上的文件---ftp协议方式
- Excel No Responding 的解决方案(Slow Addin-减少访问次数)
- Struts2:拦截器的使用【官方文档翻译】
- POCO中文路径解决方法
- RSA算法
- 根据生年月计算年龄 小方法
- FTP 上精确取文件,提供byte[]传出,再转本地文件
- Android 自定义View实现多行RadioGroup单选(多选)
- LeetCode: Integer to Roman
- PhotoView开源项目剖析
- Sublime Text 3常用插件安装(含Emmet插件指令介绍)
- Python的__name__与__main__函数
- backbone(-2) 理解面向对象
- linux下tomcat init脚本
- ZOJ Problem Set - 1188 DNA Sorting