file文件的拷贝 以及ftp的下载

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());
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值