1.批量下载文件写入到本地指定目录,偶现报错
2.写入文件代码
in = httpURLConnection.getInputStream();
file = new File(audioPath);
if (!file.exists()) {
file.mkdirs();
}
out = new FileOutputStream(audioPath + "/" + audioName);
int index;
byte[] bytes = new byte[1024];
while ((index = in.read(bytes)) != -1) {
out.write(bytes, 0, index);
}
out.flush();
out.close();
3.报错异常
java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:609)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:696)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3375)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3368)