在web下的 sun.net.ftp.FtpClient 的ftp上传与下载

      上传文件到指定的服务器

      url为服务器的ip

     username为服务器的登录名

     password为服务器的密码

     path为要上传的服务器绝对路径  /home/xxxx/file

     filename   文件名的数组

     filepath   文件路径的数组,

     现在ie8,firefox的安全机制都使得现在的上传文件或许不到绝对路径,当然如果直接上传到tomcat服务器的时候例外.

    如果上传的地方与tomcat没有任何关系,就是要上传到linux的一个指定文件夹下的话,这样就比较麻烦了,因为上传需要的是绝对路径,而现在的浏览器又获取不到.

    这时候就需要先把文件利用struts上传文件到tomcat下的一个临时目录,再通过ServletActionContext.getRequest().getRealPath("/tmp")获取临时文件夹的路径加上文件名.这就是filepath的由来。

    

为防止文件名冲突所带的影响,就采用的时间long型作为文件名,当然数据库存的时候要指定一下

	public static String[] uploadFile(String url, String username,
			String password, String path, String[] filename, String[] filepath) {
		Timestamp d = new Timestamp(System.currentTimeMillis());
		String[] uppath = new String[filename.length];
		TelnetOutputStream os = null;
		FtpClient	ftpClient = new FtpClient();
		try {
			ftpClient.openServer(url);
			ftpClient.login(username, password);
			ftpClient.cd(path);
			ftpClient.binary();
			for (int i = 0; i < filename.length; i++) {
				FileInputStream in = new FileInputStream(new File(
						filepath[i]));
				uppath[i] = d.getTime() + "." + getFileHouZhui(filename[i]);
				try {
					// 命名文件
					os = ftpClient.put(uppath[i]);
					byte[] bytes = new byte[1024];
					int c;
					while ((c = in.read(bytes)) != -1) {
						os.write(bytes, 0, c);
					}
				} catch (IOException e) {
					
				} finally {
					if (in != null) {
						in.close();
					}
					if (os != null) {
						os.close();
					}
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			closeFTPClient();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return uppath;
	}
	
	public static String getFileHouZhui(String filename) {

		String fn[] = filename.split("[.]");
		String houzhuiname = fn[fn.length - 1];
		return houzhuiname;
	}


ACTION下载文件

这个是利用浏览器的下载功能,需先从配置文件中获取ftp所需的基本信息 ip  登录名 密码 路径

public String downFile() throws Exception {

		String username = CommonProperties.getStringProperty("username");
		String password = CommonProperties.getStringProperty("password");
		String ip = CommonProperties.getStringProperty("ip");
		String path = CommonProperties.getStringProperty("path");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		response.setContentType("apploaction/x-download");
		String filepath = new String(getDownpath().getBytes("ISO-8859-1"),"UTF-8");
		String filename = filepath;
		response.setHeader("Content-disposition", "attachment;filename="
				+ new String(filename.getBytes("utf-8"), "ISO-8859-1"));
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			FtpClient ftp = new FtpClient(ip);
			ftp.login(username, password);
			ftp.binary();

			if (path.length() != 0) {
				ftp.cd(path);
			}
			ServletOutputStream sout = response.getOutputStream();
			TelnetInputStream is = ftp.get(filepath);
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(sout);
			byte[] bytes = new byte[1024];
			int c;
			while ((c = bis.read(bytes)) != -1) {
				bos.write(bytes, 0, c);
			}
			bos.close();
			is.close();
			bis.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		return null;
		
	}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值