package com.zte.xh.fund.util;
import java.io.BufferedOutputStream;
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 java.nio.channels.FileChannel;
import java.util.Properties;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import com.jfinal.kit.PropKit;
import com.mysql.jdbc.StringUtils;
/**
* 用来对文件操作的一些方法
*
* @author Jay_Lee
*
*/
public class FileUtil {
// ftp的propertis信息
private static Properties properties = PropKit.use(
"config/manager.properties", "utf-8").getProperties();
// ftp上传的信息
private static final String IP = properties.getProperty("ip"); // 服务器IP地址
private static final String USER_NAME = properties.getProperty("userName"); // 用户名
private static final String PWD = properties.getProperty("pwd"); // 密码
private static final int PORT = Integer.valueOf(properties
.getProperty("port")); // 端口号
public static final String PATH = properties.getProperty("filepath"); // 读取文件的存放目录(也是下载目录)
public static FTPClient ftpClient = null;
/**
* copy文件给另一个文件
*
* @param sourceFile
* @param toFile
*/
public static void copyFile(File sourceFile, File toFile) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(sourceFile);
fo = new FileOutputStream(toFile);
// 得到对应的文件通道
in = fi.getChannel();
// 得到对应的文件通道
out = fo.getChannel();
// 连接两个通道,并且从in通道读取,然后写入out通道
in.transferTo(0, in.size(), out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获得新的文件名
*
* @param name
* @return
*/
public static String newFileName(String name) {
String type = name.substring(name.indexOf("."), name.length());
String newn = String.valueOf(System.currentTimeMillis());
return newn + type;
}
/**
* 输出文件的输出流
*
* @param filePath
* @param out
* @throws Exception
*/
public static void outFile(String filePath, OutputStream out)
throws Exception {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
byte[] tempB = new byte[1024];
int tempFlag;
while ((tempFlag = fis.read(tempB)) != -1) {
out.write(tempB, 0, tempFlag);
}
fis.close();
out.flush();
out.close();
}
/**
* 判断文件的格式
*
* @param fileName
* @return
*/
public static String getFileType(String fileName) {
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1,
fileName.length());
return fileType;
}
// ftp上传文件到服务器
public static String uploadToFtp(String ftpPath, String filePath,
String fileName) throws SocketException, IOException {
String returnMessage = "0";
try {
ftpConne();
int returnCode = ftpClient.getReplyCode();
FileInputStream fis = null;
if (FTPReply.isPositiveCompletion(returnCode)) {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 登陆成功设置上传目录
File f = new File(filePath);
createPath(ftpPath);
ftpClient.changeWorkingDirectory(ftpPath);
fis = new FileInputStream(f);
ftpClient.storeFile(fileName, fis);
returnMessage = "1";
} else {
returnMessage = "0";
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
// 关闭链接
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
}
return returnMessage;
}
// ftp判断是否存在目录,不存在则创建
public static void createPath(String paths) {
try {
// 解析path的地址是否包含子路劲
String[] ps = paths.split("/");
String rp = "";
boolean isexist = false;
for (String path : ps) {
if (StringUtils.isNullOrEmpty(path)) {
continue;
}
rp += "/" + path;
isexist = ftpClient.changeWorkingDirectory(rp);
if (!isexist) {
ftpClient.makeDirectory(path);
ftpClient.changeWorkingDirectory(rp);
}
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("创建文件夹出错!", e);
}
}
/**
* 从ftp下载文件
*
* @param localFileName
* 本地文件名
* @param remoteFileName
* 远程文件名
*/
public static String downFromFtp(String localPath, String remotePath,
String fileName) {
BufferedOutputStream buffOut = null;
String result = "";
try {
ftpConne();
buffOut = new BufferedOutputStream(new FileOutputStream(localPath));
boolean isExist = ftpClient.changeWorkingDirectory(remotePath);
if (!isExist) {
result = "3";
System.out.println("路径不存在");
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.retrieveFile(fileName, buffOut);
result = "1";
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (buffOut != null)
buffOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
// ftp的链接
public static void ftpConne() {
try {
ftpClient = new FTPClient();
ftpClient.setBufferSize(1024 * 1024 * 2);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
ftpClient.connect(IP, PORT);
ftpClient.login(USER_NAME, PWD);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP链接出错!", e);
}
}
public static void downFile(String realPath, String enName,
String flieName, int flag, OutputStream out) throws Exception {
String filePath = null;
if (flag == 1) {
String fileM = realPath + File.separator + "template"
+ File.separator + enName;
filePath = realPath + File.separator + "template" + File.separator
+ enName + File.separator + flieName;
File file = new File(fileM);
if (!file.exists()) {
file.mkdirs();
downFromFtp(filePath, PATH + "template/" + enName, flieName);
}
}
if (flag == 2) {
String fileM = realPath + File.separator + "details"
+ File.separator + enName;
filePath = realPath + File.separator + "details" + File.separator
+ enName + File.separator + flieName;
File file = new File(fileM);
if (!file.exists()) {
file.mkdirs();
downFromFtp(filePath, PATH + "details/" + enName, flieName);
}
}
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
byte[] tempB = new byte[1024];
int tempFlag;
while ((tempFlag = fis.read(tempB)) != -1) {
out.write(tempB, 0, tempFlag);
}
fis.close();
out.flush();
out.close();
}
// 测试demo
public static void main(String[] args) throws SocketException, IOException {
FileUtil.ftpConne();
FileUtil.uploadToFtp(PATH, "f:" + File.separator + "测试.xls",
"测试.xls");
// FileUtil.downFromFtp("f:" + File.separator + "测试.xls",
// PATH,"测试.xls");
// 在action中调用此方法,直接在页面展示图片
FileUtil.outFile(localPath, getResponse().getOutputStream());
}
}
分享到:
相关推荐
简单的说,FTP就是完成两台计算机之间的拷贝,从远程计算机拷贝文件至自己的计算机上,称之为“下载(download)”文件。若将文件从自己计算机中拷贝至远程计算机上,则称之为“上载(upload)”文件。在TCP/IP协议...
作为一种安全措施,大多数匿名FTP主机都允许用户从其下载文件,而不允许用户向其上传文件,也就是说,用户可将匿名FTP主机上的所有文件全部拷贝到自己的机器上,但不能将自己机器上的任何一个文件拷贝至匿名FTP主机...
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而...
FTP是File Transfer Protocal的缩写,意为文件传输协议,它可以将远程系统上的一个或多个文件拷贝到本地计算机, 也可以将本地计算机上的一个或多个文件拷贝到远程系统 上。 FTP是TCP/IP协议族中的应用层协议,它...
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而...
FTP教程手册 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP...
FTP 是FileTransfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序 (Application)。基于不同的操作系统有不同的FTP应用程序,而...
因为基本就我一个人看的懂,FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作...
FTP是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而...
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而...
FTP简介 FTP(File Transport Protocol,文件传输协议)用于实现客户端与服务器之间的文件传输,尽管Web 也可以提供文件下载服务,但是FTP服务的效率更高,对权限控制更为严格,因此,仍然被广泛应用于Internet/...
简单的说,FTP就是完成两台计算机之间的拷贝,从远程计算机拷贝文件至自己的计算机上,称之为“下载 (download)”文件。若将文件从自己计算机中拷贝至远程计算机上,则称之为“上载(upload)”文件。在TCP/IP协议...
Serv-U File Server 是最好用的 FTP 服务器之一了。Serv-U™ – Windows平台的FTP服务器软件Serv-U 是目前众多的 FTP 服务器软件之一。通过使用Serv-U,用户能够将任何一台PC 设置成一个FTP 服务器,这样,用户或...
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而...
Qtftp示例 上一节我们讲述了HTTP的编程,这一节...FTP的主要作用,就是让用户连接上一个远程计算机,查看远程计算机有哪些文件,然后把文件从远程计算机上拷贝到本地计算机,或者把本地计算机的文件送到远程计算机上。
FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序(Application)。基于不同的操作系统有不同的FTP应用程序,而...
FTP(File Transfer Protocol)是一个双向数据传输协议,能让用户把文件拷贝到远程主机或者从远程主机 中拷贝文件,并且允许文件有存取权限,主要适合于异型网络之间传输文件。 三、实验内容 1.安装IIS(Internet...
第1步,下载webpureftp安装文件,然后执行以下指令解压: [root@localhost Desktop]# tar zxvf webpureftp0.1.tar.gz 第2步,使用下列指令将解压出来的webpureftp移动到/var/www/html下并改名。 [root@localhost ...
FTP是File Transfer Protocal的缩写,意为文件传输协议,它可以将远程系统上的一个或多个文件拷贝到本地计算机,也可以将本地计算机上的一个或多个文件拷贝到远程系统 上。
把生成的vsftpd拷贝到根文件系统的sbin目录,vsftpd.conf文件拷贝到开发板系统根文件系统的etc目录下。 5、配置vsftpd.conf #使vsftpd处于独立启动模式 listen=YES listen_port=21 anon_other_write_enable=YES 6...