java - HDFS读取excel内容出现乱码
天蓬老师
天蓬老师 2017-04-18 09:49:02
0
2
765

1.使用poi生成excel,上传hdfs
2.当有需要的时候,去下载该文件
3.下载文件是通过rest接口
4.接口中通过hdfs api读取hdfs上的excel文件内容,并转成字符串,响应到客户端


图1 通过hdfs的get命令拉去的文件,显示正常


图2 通过hdfs的java api下载的文件内容,显示乱码
最原始的代码:

public static String readFile(Configuration conf, String filePath) throws IOException, URISyntaxException { String fileContent = null; FileSystem fs = getFileSystem(conf); Path path = new Path(filePath); InputStream inputStream = null; ByteArrayOutputStream outputStream = null; try { inputStream = fs.open(path); outputStream = new ByteArrayOutputStream(inputStream.available()); IOUtils.copyBytes(inputStream, outputStream, conf); byte[] lens = outputStream.toByteArray(); //解决中文乱码 fileContent = new String(lens, "UTF-8"); } finally { IOUtils.closeStream(inputStream); IOUtils.closeStream(outputStream); fs.close(); } return fileContent; }

修改后的代码:

/** * 读取文件内容 * * @param conf * @param filePath * @return * @throws IOException */ public static String readFile(Configuration conf, String filePath) throws IOException, URISyntaxException { String fileContent = null; FileSystem fs = getFileSystem(conf); Path path = new Path(filePath); FSDataInputStream in = null; ByteArrayOutputStream outputStream = null; try { in = fs.open(path); outputStream = new ByteArrayOutputStream(); //IOUtils.copyBytes(in, outputStream, conf); //这个也是乱码 byte[] b = new byte[1024]; int numBytes = 0; while ((numBytes = in.read(b)) > 0) { outputStream.write(b, 0, numBytes); } fileContent = new String(outputStream.toByteArray(), "UTF8"); } finally { IOUtils.closeStream(in); IOUtils.closeStream(outputStream); fs.close(); } return fileContent;

网上的方法基本都用过了,ByteArrayOutputStream换成FsDataOutputStream也尝试过了,也是乱码。

public static String readFile(Configuration conf, String filePath) throws IOException, URISyntaxException { String fileContent = null; FileSystem fs = getFileSystem(conf); Path path = new Path(filePath); FSDataInputStream fin = null; InputStreamReader bin = null; int line; try { StringBuffer buffer = new StringBuffer(); fin = fs.open(path); bin = new InputStreamReader(fin, "UTF-8"); while ((line = bin.read()) > 0) { buffer.append(line); } fileContent = new String(buffer); } finally { IOUtils.closeStream(fin); IOUtils.closeStream(bin); fs.close(); } return fileContent; }

使用System.out也是乱码:

public static String readFile(Configuration conf, String filePath) throws IOException, URISyntaxException { String fileContent = null; FileSystem fs = getFileSystem(conf); Path path = new Path(filePath); FSDataInputStream in = null; int line; try { in = fs.open(path); IOUtils.copyBytes(in, System.out, 4096, false); fileContent = new String(""); } finally { IOUtils.closeStream(in); fs.close(); } return fileContent; }

我对IO流不熟悉,求大神们帮忙看看!

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (2)
刘奇

Solved!
I have compiled the solution:

Exporting big data reports based on haddop's HDFS and Excel open source library POI·pitfalls encountered

Everyone is welcome to give your own opinions and suggestions, and everyone is welcome to study together!

    左手右手慢动作

    fileContent = new String(outputStream.toByteArray(), "UTF8");
    What are you transcoding for?

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!