0
我使用下面的代码,以确保文件内容被成功写入磁盘通过对异常获取连接复位(TopLink的)
public void copyFileFromUrl(URL source, File target, int count) throws IOException {
InputStream in = null;
OutputStream out = null;
if (target != null) {
try {
if (!target.exists()) {
target.createNewFile();
if (source == null) {
return;
} else {
in = source.openStream();
}
out = new FileOutputStream(target);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
log.debug("The contents from the URL: " + source + " are successfully written to the file " + target);
//add for successfull
} else {
log.debug("skipping creation of asset");
}
} catch (Exception e) {
if(count < 3){
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
// Attempt to delete it
boolean success = target.delete();
if (!success) {
log.debug("Unable to delete " + target);
} else {
copyFileFromUrl(source, target, ++count);
}
} else {
log.debug(e.getClass().getName());
e.printStackTrace();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
我打电话这段代码这样
while(iter.hasNext()) {
CourseMaterials cm = iter.next();
String url;
try {
Asset asset = cm.getAsset();
List<AssetVersion> av = asset.getAssetVersions();
} catch (Exception e1) {
log.debug("Bad asset so skipping...");
e1.printStackTrace();
continue;
}
....
try {
URL earl = new URL(visualElementURL);
scormFileWriter.copyFileFromUrl(earl, new File(absoluteFileName), 0);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
现在我试图如何,当我来功能copyFileFromUrl()
,我拔掉电缆,它尝试两次,然后在第三次我插入电缆。该函数成功返回。因为我在while循环中。现在之后,当我来到线
Asset asset = cm.getAsset();
我得到Connection Reset by peer exception
。它跳过这个资产,然后再次正常启动。为什么?为什么我得到connection Reset by peer exception
?如果我因为拔掉电缆而得到这个异常,那么我也应该为所有其他资产得到它,但我只是为了下一次迭代才得到这个异常,然后它开始工作正常,我的意思是然后行Asset asset = cm.getAsset();
投掷后不抛出异常第一次?
为什么会发生这种情况?我如何克服它?
我正在使用SQL Server 2008 for数据库。
感谢