简体   繁体   中英

Character encoding issue while reading api response using NIO

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

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM