请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

关于Win下copyToLocalFile方法报错的解决

我也遇到了这个问题,看评论区有小伙伴说前后各加上一个参数就好了,试了一下还真是

先来看一下该方法的说明

/**
* The src file is under FS, and the dst is on the local disk. Copy it from FS
* control to the local dst name. delSrc indicates if the src will be removed
* or not. useRawLocalFileSystem indicates whether to use RawLocalFileSystem
* as local file system or not. RawLocalFileSystem is non crc file system.So,
* It will not create any crc files at local.
*
public void copyToLocalFile(boolean delSrc, Path src, Path dst,

boolean useRawLocalFileSystem) 

第一个参数 delSrc:是否删除掉源目录

最后一个参数RawLocalFileSystem:是否使用本地文件系统

可能在win下就是要开启使用本地文件系统吧。


除了直接使用拷贝方法,还可以通过流的方式来进行下载,下面是我的代码

    //从hdfs拷贝文件到本地:下载
    @Test
    public void copyToLocalFile() throws IOException
    {
//        fileSystem.copyToLocalFile(false,new Path("/hdfsapi/test/mp4"),
//                new Path("C:\\Users\\naimehao\\Pictures\\testVV.mp4"),true);

        FSDataInputStream in = fileSystem.open(new Path("/hdfsapi/test/mp4"), 4096);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\naimehao\\Pictures\\testVV.mp4")));
        IOUtils.copyBytes(in, out, 4096);
    }

试验了,下载后还能打开播放呢~


希望能帮到大家

好人一生平安:)

正在回答

4回答

两种不同方式都能OK

0 回复 有任何疑惑可以回复我~
那些穿越时光的歌声 2021-03-21 15:49:41

我用你流下载代码下载下来的文本是空的。。。

0 回复 有任何疑惑可以回复我~
heibanbaima 2020-12-25 20:05:45

超级感谢!

0 回复 有任何疑惑可以回复我~
  • 提问者 乃好 #1
    慕课网打钱!
    回复 有任何疑惑可以回复我~ 2020-12-25 20:06:33
weixin_慕瓜0383873 2020-04-09 22:05:49

感谢乃好同学的回答,测试了MP4文件一下,确实可以。

但是对于txt文件和pdf文件上传到HDFS,再下载到windows上,就会出现乱码问题,不知道你有没有办法解决

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信