使用多线程通过HttpURLConnection 下载文件,也没有报错,老是出现下载下来的文件比实际的小,然后提示打不开。有什么好的方法没?谢谢

解决方案 »

  1.   

    我靠,是不是conn.getInputStream没读完?
    贴代码。
      

  2.   

    public void downLoadFile(String destUrl, String fullName,String userName,
    String password)
    throws ApplicationException {
    FileOutputStream out = null;
    BufferedInputStream in = null;
    byte[] buf = new byte[BUFFER_SIZE];
    int size = 0;
    try {
    connectServer(destUrl);
    in = new BufferedInputStream(httpUrl.getInputStream());
    out = new FileOutputStream(fullName);
    while ((size = in.read(buf)) != -1) {
    out.write(buf, 0, size);
    }
    out.flush();
    } catch (IOException e) {
    throw new ApplicationException("通过HTTP下载文件出现异常:"
    + e.getMessage());
    } finally {
    if ( in != null) { try { in.close(); } catch (IOException ex) {}}
            if ( out != null) { try { out.close(); } catch (IOException ex) {}}
            if(httpUrl != null) httpUrl.disconnect();
    }
    }