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數據庫。
感謝