将zip文件从服务器下载到设备时出错?

时间:2012-03-02 12:47:15

标签: java android zip

我正在使用此代码从服务器下载zip文件

private static InputStream OpenHttpConnection(String urlString) 
        throws IOException
        {
            InputStream in = null;
            int response = -1;

            URL url = new URL(urlString); 
            URLConnection conn = url.openConnection();

            if (!(conn instanceof HttpURLConnection))                     
                throw new IOException("Not an HTTP connection");

            try{
                System.out.println("OpenHttpConnection called");

                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.setDoOutput(true);
                httpConn.setDoInput(true);
                httpConn.setRequestProperty("content-type", "binary/data");

                httpConn.connect(); 

                response = httpConn.getResponseCode();

                System.out.println("response is"+response);
                System.out.println(HttpURLConnection.HTTP_OK);

                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();
                    System.out.println("Connection Ok");
                    return in;
                    }                     
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");            
            }
            return in;     
        }

                 private static void saveToInternalSorage(InputStream in,String filename,Context ctx){



//fos =openFileOutput(filename, Context.MODE_WORLD_READABLE);

try {
  //  System.out.println("mypath = "+mypath);
    //fos = new FileOutputStream(mypath);
    FileOutputStream fos = (ctx).openFileOutput(filename, Context.MODE_WORLD_READABLE);

    byte[] buffer=new byte[1024];






    int len1 ;
    while ( (len1 = in.read(buffer) )!=-1 ) {
        fos.write(buffer);


    }

    // Use the compress method on the BitMap object to write image to the OutputStream


} catch (Exception e) {
    e.printStackTrace();
}
}

下载的zip文件已损坏,文件的实际大小为3.5kb,但下载的文件大小为5kb。代码有什么问题请帮帮忙?

3 个答案:

答案 0 :(得分:3)

while ( (len1 = in.read(buffer) )!=-1 ) {
        fos.write(buffer);


    }

您正在每次迭代中写入整个缓冲区(1024字节)。您应该只写len1个字节(读取的字节数)。

在旁注中,您可能希望查看使用一些更高级别的抽象库来处理HTTP和流操作等内容。例如,Apache Commons HttpComponentsCommons IO

答案 1 :(得分:0)

            httpConn.setDoOutput(false);
            httpConn.setRequestProperty("Content-Type", "application/octet-stream");
            httpConn.setRequestProperty("Content-Length", String.valueOf(file.length());

while (... > 0) {
    fos.write(buffer, 0, len1);


fos.close();

答案 2 :(得分:0)

只写入填充在缓冲区中的字节,即只有len1字节。它将解决您的问题,好像缓冲区未完全填充,我们将只写入那些读取的字节。

    while ( (len1 = in.read(buffer) )!=-1 ) {
        fos.write(subArray(buffer,len1));    
    }

//Method to create su-array
    public byte[] subArray(byte[] arr, int length) {
        byte temp[] = new byte[length];
        for (int i = 0; i < length; i++) {
            temp[i] = arr[i];
        }
        return temp;
    }