展开全部
public static String downloadLog(String loadUrl,String fileName) throws Exception {
URL url = new URL(loadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(50 * 1000);
conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = null;
FileOutputStream fos = null;
inputStream = conn.getInputStream();
//路径目录
File saveDir = new File("D://test");
if (!saveDir.exists()) {
saveDir.mkdirs();
}
File file = new File(saveDir + File.separator + fileName);
fos = new FileOutputStream(file);
readInputStream(fos, inputStream);
return file.toString();
}
/**
* 用流把数据写到本地文件上
*
* @param inputStream
* @return
* @throws Exception
* @throws IOException
*/
public static void readInputStream(FileOutputStream fos,
InputStream inputStream) throws Exception {
byte[] buffer = new byte[1024];
int len = 0;
try {
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
} catch (Exception e) {
logger.error("readInputStream文件可能太大导致");
throw new Exception(e);
} finally {
try {
fos.close();
inputStream.close();
} catch (IOException e) {
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询