62,614
社区成员
发帖
与我相关
我的任务
分享
import java.io.ByteArrayOutputStream;
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 javax.servlet.http.HttpServletResponse;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileFilter;
import org.apache.commons.net.ftp.FTPReply;
import com.baosight.iplat4j.util.NumberUtils;
/**
*
* Project:ieps
* <p>FTP文件上传下载工具类</p>
*
* Create On 2011-2-22 下午01:37:25
*
* @author <a href="mailto:dlm104@126.com">dinglimin</a>
* @version 1.0
*/
public class FtpUtil {
public static String url;
public static int port;
public static String user;
public static String pwd;
public static void init() throws Exception{
System.out.println("初始化FTP参数...");
try{
url =ConfigFileRead.readerConfig("FilePath", "ftp.Remote.ip");
port =NumberUtils.toint(ConfigFileRead.readerConfig("FilePath", "ftp.Remote.port"));
user =ConfigFileRead.readerConfig("FilePath", "ftp.Remote.user");
pwd = ConfigFileRead.readerConfig("FilePath", "ftp.Remote.password");
}catch(Exception e){
e.printStackTrace();
throw new Exception("connet remote ftp faild... ...");
}
}
public static void connectServer(FTPClient ftpClient){
try{
int reply;
ftpClient.connect(url, port);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftpClient.login(user, pwd);// 登录
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Description: 向FTP服务器上传文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param path
* FTP服务器保存目录
* @param filename
* 上传到FTP服务器上的文件名
* @param input
* 输入流
* @return 成功返回true,否则返回false
* @throws Exception
*/
public static boolean uploadFile(String path, String filename, InputStream input) throws Exception {
boolean success = false;
FTPClient ftpClient= new FTPClient();
try {
connectServer(ftpClient);
ftpClient.changeWorkingDirectory(path);
ftpClient.storeFile(filename, input);
input.close();
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
throw new Exception("文件上传过程中发生异常!"+e.getMessage());
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return success;
}
/**
* Description: 从FTP服务器下载文件
*
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 要下载的文件名
* @param localPath
* 下载后保存到本地的路径
* @return
* @throws Exception
*/
public static boolean downFile(String remotePath, String ftpName,
String fileName, HttpServletResponse response) throws Exception {
boolean success = false;
OutputStream out = null;
FTPClient ftpClient= new FTPClient();
try {
connectServer(ftpClient);
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftpClient.listFiles(remotePath);
for (int i = 0; i < fs.length; i++) {
if (fs[i].getName().equals(ftpName)) {
// ByteArrayOutputStream os = new ByteArrayOutputStream();
File f = new File(ftpName);
FileOutputStream fos = new FileOutputStream(f);
boolean bf = ftpClient.retrieveFile(ftpName, fos);
if (bf) {
FileInputStream fis = new FileInputStream(ftpName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch;
while((ch=fis.read())!=-1) {
bos.write(ch);
}
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;"
+ " filename="+ new String(fileName.getBytes(), "ISO-8859-1"));
byte[] bt=bos.toByteArray();
out = response.getOutputStream();
out.write(bt, 0, bt.length);
fis.close();
}
fos.close();
out.flush();
out.close();
}
}
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
throw new Exception("文件下载过程中发生异常!"+e.getMessage());
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* 删除服务器上的文件
* @param remotePath
* @param dbFileName
* @return
*/
public static boolean deleteRemoteFiles(String remotePath,String[] dbFileName,String date){
System.out.println("::: DELETE REMOTE FTP FILE BEGIN :::");
boolean success = false;
FTPClient ftpClient= new FTPClient();
try {
connectServer(ftpClient);
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
final String testDate = date;
//ftpClient.listFiles(pathname, filter)
if(!StringUtil.isArray(dbFileName)){
return true;
}
FTPFile[] fs = ftpClient.listFiles(remotePath, new FTPFileFilter() {
public boolean accept(FTPFile file) {
String fileName = file.getName();
boolean ret = fileName.startsWith(testDate);
return ret;
}
});
for (int i = 0; i < fs.length; i++) {
boolean temp=false;
//System.out.println("服务器满足条件文件:"+fs[i].getName());
for (int j = 0; j < dbFileName.length; j++) {
if (dbFileName[j].equals(fs[i].getName())) {
temp=true;
break;
}
}
if(!temp){
//System.out.println("文件["+fs[i].getName()+"]在数据库中不存在,删除...");
ftpClient.deleteFile(remotePath+"/"+fs[i].getName());
}
}
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
System.out.println("::: DELETE REMOTE FTP FILE END :::");
return success;
}
public static void main(String[] args) {
String[] files={"20110222113447353.txt"};
try {
FtpUtil.init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FtpUtil.deleteRemoteFiles("/cgftp/Attachment",files, "20110222");
}
}
try {
java.io.InputStream inputStream;
java.net.URL url = new java.net.URL(surl);
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url
.openConnection();
connection.connect();
inputStream = connection.getInputStream();
connection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}