为什么在使用 Spring Boot 计划任务读取 CSV 文件时发生过早的 EOF 错误

Why Premature EOF Error has occur, while reading CSV file using SpringBoot Scheduled Task

下面显示了相关的代码段。它与连接关闭或缓冲区内存有关吗? (文件,包含 50MB+ 和 65000+ 文本行)

@Scheduled(cron = "0 0 9 * * *")
public void retrieveFile() throws IOException{
    try {
        RestTemplate restTemplate=new RestTemplate();
        String url="URL of the file, which contains 50MB+ AND 65000+ Text Lines";
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        URL targetFileUrl = new URL(url);
        URLConnection connection = targetFileUrl.openConnection();
        InputStream is = connection.getInputStream();
        handleFile(is);
    }catch (Exception e) {
        e.getMessage();
    }
}


public void handleFile(InputStream inputStream) throws IOException {
    BufferedReader reader = null;
    try {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        reader = new BufferedReader(inputStreamReader);
        String nextLine = null;
        while ((nextLine = reader.readLine()) != null) {
                //saving them in DB
            }
        }
        
    } catch (Exception ex) {
        ex.getMessage();//ERROR POPULATED FROM HERE
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

请指教! 谢谢

已更新 请在此处找到错误堆栈跟踪。我已经标记了来自源的错误行并用日志映射它们。

Error Stack Trace : java.io.IOException: Premature EOF
    at java.base/sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:568)
    at java.base/sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:612)
    at java.base/sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:699)
    at java.base/java.io.FilterInputStream.read(FilterInputStream.java:133)
    at java.base/sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3510)
    at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.base/java.io.InputStreamReader.read(InputStreamReader.java:181)
    at java.base/java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:326)
    at java.base/java.io.BufferedReader.readLine(BufferedReader.java:392)
    at com.blah.blah.blah.handleFile(FileOps.java:75)//This coding line -> ((nextLine = reader.readLine()) != null) {
    at com.blah.blah.blah.retrieveFile(FileOps.java:65)//This coding line -> handleFile(is);
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:95)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)

内部classsun.net.www.http.ChunkedInputStream抛出异常。此 class 用于读取一段 http 内容,其长度在开始时使用 Content-Length HTTP header 声明。但是,http 内容不完整,因为它在读取预期的字节数之前就结束了。

这可能是由于 HTTP 连接错误或服务器发送的 HTTP 内容不完整造成的。使用Wireshark等调试工具检查返回的原始http内容;查看 Content-Length 并确保有这么多字符可供阅读。