在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;
		
	}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章