阅读更多
/**
* 下载文件到指定目录
* @param filePath String
* @param response HttpServletResponse
* @throws ApplicationException
*/
public void downloadFileToClient(String fileName,String webDirectoryAndFileName) throws Exception{
FTPClient ftpClient = new FTPClient();
InputStream is = null;
FileOutputStream os = null;
try {
/*建立连接*/
server = SystemParameter.getParameterValue("SYS_IDC_FTP_HOST");
user = SystemParameter.getParameterValue("SYS_IDC_FTP_USER");
password = SystemParameter.getParameterValue("SYS_IDC_FTP_PASSWORD");
port = SystemParameter.getParameterValue("SYS_IDC_FTP_PORT");
String workHome = SystemParameter.getParameterValue("SYS_IDC_FTP_WORK_HOME");
ftpClient.connect(server);
ftpClient.login(user, password);
String remoteFileName = workHome+"/"+fileName;
boolean Flag= findName(remoteFileName);
if(!Flag)
{
throw new ApplicationException("没找到您所下载的文件,请检查文件名是否正确");
}
// 设置文件的传输类型,默认是ASCII,修改为二进制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
is = ftpClient.retrieveFileStream(remoteFileName);
File outfile = new File(webDirectoryAndFileName);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) > 0)
{
os.write(bytes, 0, c);
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();
ftpClient.logout();
ftpClient.disconnect();
}
}
-------------
/**
* 下载文件
* @param filePath String
* @param response HttpServletResponse
* @throws ApplicationException
*/
public void downloadFile(String fileName,OutputStream out) throws Exception{
FTPClient ftpClient = new FTPClient();
try {
/*建立连接*/
server = SystemParameter.getParameterValue("SYS_IDC_FTP_HOST");
user = SystemParameter.getParameterValue("SYS_IDC_FTP_USER");
password = SystemParameter.getParameterValue("SYS_IDC_FTP_PASSWORD");
port = SystemParameter.getParameterValue("SYS_IDC_FTP_PORT");
String workHome = SystemParameter.getParameterValue("SYS_IDC_FTP_WORK_HOME");
ftpClient.connect(server);
ftpClient.login(user, password);
String remoteFileName = workHome+"/"+fileName;
boolean Flag= findName(remoteFileName);
if(!Flag)
{
throw new ApplicationException("没找到您所下载的文件,请检查文件名是否正确");
}
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.retrieveFile(remoteFileName, out);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
}
finally
{
ftpClient.logout();
ftpClient.disconnect();
}
}
--------
/**
* 建立FTP连接
* @param server String
* @param user String
* @param password String
* @param path String
*/
private void connectServer(String server,String user,String password,String port) throws Exception{
ftpClient = new FTPClient();
try {
if(port!=null && port.length()>0)
{
int ftpPort = Integer.parseInt(port);
ftpClient.connect(server,ftpPort);
int reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
throw new Exception("ftp 服务器拒绝连接");
}
ftpClient.login(user,password);
log.info("你已成功连接到"+server+"服务器"+"\n");
log.info("端口是"+ftpPort+"\n");
}
} catch (IOException ex) {
throw new Exception(ex);
}
}
/**
* 关闭连接
* @throws ApplicationException
*/
private void closeConnect() throws ApplicationException{
try {
ftpClient.logout();
ftpClient.disconnect();
log.info("正常退出文件服务器");
} catch (IOException ex) {
throw new ApplicationException(ex);
}
}
------
/**
* 上传文件
* @param uploadFile File
* @param outputPath String
* @return boolean
* @throws ApplicationException
*/
public void fileUpload(String path,String outPutName) throws Exception{
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
/*建立连接*/
server = SystemParameter.getParameterValue("SYS_IDC_FTP_HOST");
if(server==null || "".equals(server)){
server = "**";
}
user = SystemParameter.getParameterValue("SYS_IDC_FTP_USER");
if(user==null || "".equals(user)){
user = "**";
}
password = SystemParameter.getParameterValue("SYS_IDC_FTP_PASSWORD");
if(password==null || "".equals(password)){
password = "**";
}
port = SystemParameter.getParameterValue("SYS_IDC_FTP_PORT");
if(port==null || "".equals(port)){
port = "21";
}
String workHome = SystemParameter.getParameterValue("SYS_IDC_FTP_WORK_HOME");
if(workHome==null || "".equals(workHome)){
workHome = "/idcfile2";
}
ftpClient.connect(server,Integer.valueOf(port));
ftpClient.login(user, password);
File srcFile = new File(path);
fis = new FileInputStream(srcFile);
//设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//String currentPath = creatOutputPath(outPutName);
String currentPath = workHome+"/"+outPutName;
ftpClient.changeWorkingDirectory(path);
ftpClient.storeFile(currentPath, fis);
fis.close();
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
}
}