分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

IOUtils方式上传下载文件

发布时间:2023-09-06 02:18责任编辑:顾先生关键词:暂无标签
package com.css.hdfs04;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import org.junit.Before;import org.junit.Test;/** * IOUtils方式上传下载文件 */public class HdfsIo { ???Configuration conf = null; ???FileSystem fs =null; ???@Before ???public void init() throws IOException, InterruptedException, URISyntaxException { ???????// 1.加载配置 ???????conf = new Configuration(); ???????// 2.构造客户端 ???????fs = FileSystem.get(new URI("hdfs://192.168.146.132:9000/"), conf, "root"); ???} ???????/** ????* 文件上传HDFS ????*/ ???@Test ???public void putFileToHDFS() throws IllegalArgumentException, IOException{ ???????// 1.获取输入流 ???????FileInputStream fis = new FileInputStream(new File("c:/hello.txt")); ???????// 2.获取输出流 ???????FSDataOutputStream fos = fs.create(new Path("/hello.txt")); ???????// 3.流的拷贝 ???????IOUtils.copyBytes(fis, fos, conf); ???????// 4.关闭资源 ???????IOUtils.closeStream(fis); ???????IOUtils.closeStream(fos); ???} ???????/** ????* 文件下载HDFS ????*/ ???@Test ???public void getFileFromHDFS() throws IllegalArgumentException, IOException{ ???????// 1.获取输入流 ???????FSDataInputStream fis = fs.open(new Path("/hello")); ???????// 2.获取输出流 ???????FileOutputStream fos = new FileOutputStream(new File("c:/hello")); ???????// 3.流的对拷 ???????IOUtils.copyBytes(fis, fos, conf); ???????// 4.关闭资源 ???????IOUtils.closeStream(fis); ???????IOUtils.closeStream(fos); ???}}

IOUtils方式上传下载文件

原文地址:https://www.cnblogs.com/areyouready/p/9795442.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved