pgc_tel 2015-09-21 05:50 采纳率: 0%
浏览 1721
已采纳

为什么下载一个TXT文本下载的内容不全

try {
// 1.以要下载文件的 URL 为参数创建 URL 对象
URL url = new URL(sfPath);
// 2.打开连接下载文件
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
String filename = sfPath.substring(sfPath.lastIndexOf("/") + 1);
File file = new File(sfFilePath);
if (!file.exists()) {
file.mkdir();
}
File sfFile = new File(sfFilePath + filename);
sfFile.createNewFile();
OutputStream os = new FileOutputStream(sfFile);
byte[] buff = new byte[1024 * 4];
while (is.read(buff) != -1) {
os.write(buff);
}
is.close();
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}

        为什么下载文本内容不全,求大神指导下。。。。。。。。。
  • 写回答

3条回答 默认 最新

  • myleo_lost-a-horse 2015-09-21 07:34
    关注

    byte[] buff = new byte[1024 * 4];
    int len = -1;
    while ((len = is.read(buff)) != -1) {
    os.write(buff, 0, len);//使用这个方法,能防止下载的文件变大
    }
    os.close();//这里要先关闭输出字节流
    is.close();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试