javaFtpclient下载_hanCSDN_20130416

<%@ 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;
	}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值