为什么出现过早的EOF错误,在使用Springboot计划的任务读取CSV文件时
下面显示了相关的代码段。它与连接关闭或缓冲存储器有关吗? (该文件包含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)
Below shows the relevant code segments. Is it related to connection close or Buffer memory.? (The file, which contains 50MB+ AND 65000+ Text Lines)
@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();
}
}
}
Please advise on this.!
Thanks.
Updated
Please find here the Error Stack Trace. I've marked error lines from the source and mapped them with logs.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
异常由内部类
sun.net.www.http.chunkedinputstream
抛出。该类用于读取一块HTTP内容,其长度在开始时使用内容长度HTTP标头声明。但是,HTTP内容是不完整的,因为它在读取了预期的字节数量之前结束。这可能是由不良的HTTP连接或发送不完整HTTP内容的服务器引起的。使用调试工具,例如Wireshark并检查返回的RAW HTTP内容;查看内容长度,并确保可以阅读这些字符很多。
The exception is thrown by the internal class
sun.net.www.http.ChunkedInputStream
. This class is used to read a chunk of http content whose length was declared at the beginning using the Content-Length HTTP header. However, the http content was incomplete, because it ended before the expected amount of bytes was read.This is possibly caused by a bad http connection or a server that sends incomplete http content. Use a debugging tool such as Wireshark and examine the returned raw http content; look at the Content-Length and ensure that this many characters are available to read.