ITeye博客

java连接ftp上传

java_script · 2011-04-19 08:42 · java编程 · 编程语言 · Java · F# · Markdown

/** * 利用ftp把handlepath路径里面的文件都上传上去,上传完后删除掉。 * @param handlePath / private void ftpUpload(String handlePath) { FTPClient ftp = new FTPClient(); try { /Properties properties = new Properties(); properties.load(CpsUploadFileAction.class.getClassLoader().getResourceAsStream("/config/ftpconfig.properties"));/ Properties properties = FtpConfigUtil.getInstance().getFtpConfigProp(); ftp.connect(properties.getProperty(FtpConfigUtil.FTPSERVERIP), Integer.parseInt(properties.getProperty(FtpConfigUtil.FTPSERVERPORT)));//连接ftp ftp.login(properties.getProperty(FtpConfigUtil.USERNAME), properties.getProperty(FtpConfigUtil.PASSWORD));//登录ftp ftp.makeDirectory(properties.getProperty(FtpConfigUtil.PATHNAME));//创建目录; ftp.changeWorkingDirectory(properties.getProperty(FtpConfigUtil.PATHNAME)); File f = new File(handlePath); File loadFiles = f.listFiles(); //把文件传送到服务器上; for(int i=0;i<loadFiles.length;i++) { File loadFile = loadFiles; if(loadFile.isDirectory()) { continue; } if(loadFile.isFile()) { InputStream in = new FileInputStream(loadFile); ftp.storeFile(loadFile.getName(), in); in.close(); }

		}
		ftp.logout();//退出连接
	} catch(IOException ioe) {
		ioe.printStackTrace();
		throw new RuntimeException(ioe.getMessage(),ioe);
	} finally {
		if(ftp.isConnected()) {
			try {
				ftp.disconnect();//关闭连接
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage(),e);
			}
		}
	}
	//删除handlePath底下的文件;
	File f = new File(handlePath);
	File loadFiles = f.listFiles();
	for(int i=0;i<loadFiles.length;i++) {
		File tempF = loadFiles*;
		if(tempF.isDirectory()) {
			continue;
		}
		if(tempF.isFile()) {
			tempF.delete();
		}
		
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。