下载文件阻塞的问题

a5114122 2017-08-30 10:47:42
用这段代码,想下载文件,但是下了几kb就不下了,求大神解答

public static void downloadFile(String remoteFilePath, String localFilePath){
URL urlfile = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File f = new File(localFilePath);
try
{
urlfile = new URL(remoteFilePath);
httpUrl = (HttpURLConnection)urlfile.openConnection();
httpUrl.connect();

bis = new BufferedInputStream(httpUrl.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(f));
int len = 1024;
byte[] b = new byte[len];
while ((len = bis.read(b)) != -1)
{
bos.write(b, 0, len);
}
System.out.println("上传成功");
bos.flush();
bis.close();
httpUrl.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
bis.close();
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

public static void main(String[] args) {
String remoteFilePath="http://admin.geely.com/videoFile/geelyxingxiang.flv";
String localFilePath="d:/c.flv";
downloadFile(remoteFilePath,localFilePath);
}
...全文
187 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjjk 2017-08-31
  • 打赏
  • 举报
回复
int len = 1024; byte[] b = new byte[len]; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } 这段有问题,改成 int content = -1; int len = 1024; byte[] b = new byte[len]; while ((content = bis.read(b)) != -1) { bos.write(b, 0, content ); }

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧