URL:reportMainDodyUrl【例如:http://down10.zol.com.cn/skycndownernew/keaibizhi83_346188.exe】
求大神看一下!
private void downLoadNet(String reportMainDodyUrl) throws IOException {
int last = reportMainDodyUrl.lastIndexOf("/");
String filename = reportMainDodyUrl.substring(last+1, reportMainDodyUrl.length());
URL url = new URL(reportMainDodyUrl);
InputStream inStream = null;
ServletOutputStream ouputStream = null;
try {
URLConnection conn = url.openConnection();
inStream = conn.getInputStream();
response.reset();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + filename);
ouputStream = response.getOutputStream();
byte[] buffer = new byte[1204];
int byteread = 0;
while ((byteread = inStream.read(buffer)) != -1) {
ouputStream.flush();
ouputStream.write(buffer, 0, byteread);
}
} catch (Exception e) {
logger.error("File download failed!", e);
} finally {
if (inStream != null){
inStream.close();
}
if (ouputStream != null) {
ouputStream.flush();
ouputStream.close();
}
}
}
相关分类