ftp 文件获取和下载

ftp 文件下载和获取

package com.hp.vtms.service.impl;


import com.hp.vtms.model.RDPFileInformation;
import com.hp.vtms.service.FTPService;
import com.hp.vtms.util.FileDownload;


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletResponse;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA. User: meij Date: 13-12-9 Time: 下午2:04 To change
 * this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class FTPServiceImpl implements FTPService {

private static Logger _LOG = LoggerFactory.getLogger(FTPServiceImpl.class);

@Value("#{envConfig.ftp_url}")
private String url;
@Value("#{envConfig.ftp_username}")
private String FTPusername;
@Value("#{envConfig.ftp_password}")
private String FTPpassword;
@Value("#{envConfig.ftp_port}")
private Integer port;


@Override
public List FTPFolderList(String remotePath) {


remotePath = "/" + remotePath;
boolean success = false;
FTPClient ftp = new FTPClient();
InputStream inStream = null;
List ftpnameList = new ArrayList();
try {
int reply;
ftp.connect(url, port);
ftp.login(FTPusername, FTPpassword);// login
reply = ftp.getReplyCode(); // 230 is successful
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
_LOG.info("connection failed");
}


if (ftp.changeWorkingDirectory(remotePath)) {
String[] ftpnames = ftp.listNames();
for (int i = 0; i < ftpnames.length; i++) {
RDPFileInformation rdpFileInformation = new RDPFileInformation();
rdpFileInformation.setRdpfileRealName(ftpnames[i].split("\\.")[0]);
rdpFileInformation.setRdpfilename(ftpnames[i]);
rdpFileInformation.setRdpfilePath(remotePath);
ftpnameList.add(rdpFileInformation);
}
}


ftp.logout();


} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return ftpnameList;
}


@Override
public void downLoadOnline(String filePath, String filename, HttpServletResponse response, boolean isOnLine)
throws IOException {
BufferedInputStream br = null;
OutputStream out = null;
try {
if (filename == "") {
response.sendError(404, "File not found!");
return;
}


String type = "";
if (filename.toLowerCase().indexOf(".rdp") > 0) {
type = "application/x-rdp";
}


br = new BufferedInputStream(FileDownload.downFile(url, port, FTPusername, FTPpassword, "/" + filePath,
filename, ""));
byte[] buf = new byte[1024];
int len = 0;
response.reset();
if (isOnLine) {
response.setContentType(type);
response.setHeader("Content-Disposition", "inline; filename=" + filename);


} else {
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
}
out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
} catch (IOException e) {
e.printStackTrace(); // To change body of catch statement use File |
// Settings | File Templates.
} finally {
out.flush();
br.close();
out.close();
}
}


}





FileDownload.java 到ftp服务器获取文件

package com.hp.vtms.util;



import java.io.IOException;
import java.io.InputStream;


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.hp.vtms.service.impl.FTPServiceImpl;


/**
 * Created with IntelliJ IDEA. User: meij Date: 13-12-3 Time: 下午2:47 To change
 * this template use File | Settings | File Templates.
 */
public class FileDownload {

private static Logger _LOG = LoggerFactory.getLogger(FTPServiceImpl.class);


public static InputStream downFile(String url, int port, String username, String password, String remotePath,
String fileName, String localPath) throws IOException {
boolean success = false;
FTPClient ftp = new FTPClient();
InputStream inStream = null;
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);// login
reply = ftp.getReplyCode(); // 230 is successful
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
_LOG.info("connection failed");
}
ftp.changeWorkingDirectory(remotePath);// turn to the ftp server
// path


String[] ftpnames = ftp.listNames();
for (int i = 0; i < ftpnames.length; i++) {
_LOG.info(ftpnames[i]);
}


FTPFile[] fs = ftp.listFiles(remotePath);
_LOG.info("get ftp file size is---" + fs.length);
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
inStream = ftp.retrieveFileStream(ff.getName());
}
}


ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return inStream;
}




}

你可能感兴趣的:(FTP服务器)