我正在使用Java Mail API下载附件,每当网络状态发生微小变化时,我的应用就会卡住,必须重新启动它,甚至不会崩溃。
这是代码 fragment :
InputStream is = bodyPart.getInputStream();
String fileName = MimeUtility.decodeText(bodyPart.getFileName());
// Downloading the file
File f = new File(Constants.getPath() + fileName);
try {
FileOutputStream fos;
fos = new FileOutputStream(f);
byte[] buf = new byte[8*1024];
int bytesRead;
while ((bytesRead = is.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
}
解决此问题的最佳方法是什么?谢谢。
最佳答案
您的应用程序被卡住。解决方案是设置读取超时,如this question中所述。如果发生超时,则将抛出SocketTimeoutException。
关于java - InputStream-处理网络更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14155471/