<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>下载录音文件</title>
</head>
<body>
<%
String fileName = "";
String remotePath = "";
String path = request.getParameter("lywjpath");
//把单右斜杠改为双右斜杠'\'->'\\'
path = path.replaceAll("\\\\", "/");
//截取文件名和文件路径
String temp[] = path.split("/");
if (temp.length > 1) {
fileName = temp[temp.length - 1];
}
for (int n = 1; n < temp.length - 1; n++) {
remotePath += "/" + temp[n];
}
String url = PropertyManager.getProperty("vFtpServer");
int port = Integer
.parseInt(PropertyManager.getProperty("vFtpPort"));
String username = PropertyManager.getProperty("vFtpUser");
String password = PropertyManager.getProperty("vFtpPassword");
String localPath = request.getRealPath("/") + "/tempdownloadvoice";
boolean success = false;
boolean flag = FTPDownLoad.downFile(url, port, username, password, remotePath, fileName, localPath);
File file = null;
if (flag) {
try {
// path是指欲下载的文件的路径。
file = new File(localPath + "/" + fileName);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream is = new BufferedInputStream(
new FileInputStream(file));
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
// 清空response
response.reset();
// 设置response的Header
response.setCharacterEncoding("gbk");
response.setContentType("application/octet-stream;charset=gbk");
response.addHeader("Content-Disposition",
"attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(
response.getOutputStream());
os.write(buffer);
os.flush();
os.close();
success = true;
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (file != null)
file.delete();
}
}
if (success) {
%>
<script type="text/javascript">
window.close();
</script>
<%
} else {
%>
<script type="text/javascript">
alert("下载失败!");
window.close();
</script>
<%
}
%>
</body>
</html>
package com.zrar.ftp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPDownLoad {
/**
* Description: 从FTP服务器下载文件
* @param url FTP服务器
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downFile(String url, int port,String username, String password, String remotePath,String fileName,String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//必须要加,否则文件会无法打开
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
//for(FTPFile ff:fs){
FTPFile ff = null;
for(int i=0;i<fs.length;i++){
ff = fs[i];
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"/"+ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}