使用 NIO 读取 api 响应时出现字符编码问题
我正在从 Content-Type = text/CSV 的 API 响应中读取数据,并使用 Java 的 NIO 包在两个通道之间传输字节。 当代码执行正在进行时,我收到以下错误我检查了它包含的源 API 响应 M�
ERROR: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
代码:
private void downloadFile_NIO(String encoded_token) throws Exception
{
Path path = Paths.get(FILE_NAME);
long lines = 0;
FileOutputStream fileOutputStream=new FileOutputStream(new File(FILE_NAME),true);
URL url = new URL(FILE_URL);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + encoded_token );
connection.setDoOutput(true);
connection.setRequestMethod("GET");
ReadableByteChannel readableByteChannel = Channels.newChannel(connection.getInputStream());
FileChannel fileChannel = fileOutputStream.getChannel();
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
lines = Files.lines(path).count();
System.out.println("lines->" + lines);
System.out.println();
fileOutputStream.close();
}
它看起来像字符编码问题,也许 Charset 可以解决这个问题,但我不确定如何以及在哪里我现有的代码我可以使用吗?
Java 版本 - 8
I am reading data from API response which is of Content-Type = text/CSV and using Java's NIO package to transfer bytes between two Channels.
While the code execution is in progress, I get the below error I checked the Source API response it contains M�
ERROR: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
Code:
private void downloadFile_NIO(String encoded_token) throws Exception
{
Path path = Paths.get(FILE_NAME);
long lines = 0;
FileOutputStream fileOutputStream=new FileOutputStream(new File(FILE_NAME),true);
URL url = new URL(FILE_URL);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + encoded_token );
connection.setDoOutput(true);
connection.setRequestMethod("GET");
ReadableByteChannel readableByteChannel = Channels.newChannel(connection.getInputStream());
FileChannel fileChannel = fileOutputStream.getChannel();
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
lines = Files.lines(path).count();
System.out.println("lines->" + lines);
System.out.println();
fileOutputStream.close();
}
It looks like a character encoding issue, Maybe Charset can solve this issue, but I'm not sure how and where in my existing code I can use it?
Java version - 8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该方法
Files.lines(Path)
读取数据时使用 UTF-8。显然,您的数据不是以 UTF-8 编码,而是以单字节字符编码(例如 ISO-8859-1、Cp1252 或其他完全不同的编码)。找出正确的字符集是什么,然后使用
Files .lines(Path, Charset)
具有正确的字符集 此外,我建议您在阅读该文件 (fileOutputStream
) 之前关闭该文件。使用try-with-resources 以确保正确关闭资源。The method
Files.lines(Path)
uses UTF-8 when reading data. Apparently, your data is not encoded in UTF-8, but in a single-byte character encoding (e.g. ISO-8859-1, Cp1252, or something else entirely.Find out what the correct character set is, and use
Files.lines(Path, Charset)
with the correct character set. In addition, make sure you close the file (fileOutputStream
) before reading it. I recommend you use try-with-resources to ensure you close resources correctly.