2011-01-13 5 views
-1

zip을 SD 카드로 다운로드하려면 다음 코드를 시도해 본 결과, NullpointerException이 발생합니다. 어떤 심문을 시도했을 때 zip 파일이 실제로 다운로드되지 않는다는 것을 알게되었습니다. 그렇다면 코드에 약간의 변경이나 zip에 문제가 있는지 여부를 알려주십시오. M은 그 지점에만 붙어 있습니다. 제발 도와주세요 ... 내 코드는 다음과 같습니다 ...Android 다운로드 Zip to SD card, zip이 읽히지 않음

{ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String from = "http://192.168.1.63/ZipFile/Text.zip" ; 
     String to = Environment.getExternalStorageDirectory() + "/newunzip/"; 

     try { 
      ((TextView) findViewById(R.id.display)).append("\n in try after function :"); 
      downloadFile(from, to); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      ((TextView) findViewById(R.id.display)).append("\n \n Exception occured :"); 
      ((TextView) findViewById(R.id.display)).append("\n \n Exception message is :"+e.getMessage()); 
      ((TextView) findViewById(R.id.display)).append("\n \n Exception is :"+e.toString()); 
     } 

    } 

    private void downloadFile(String from, String to) throws Exception 
    { 
     ((TextView) findViewById(R.id.display)).append("\n \n in function call :"); 
     HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection(); 
     conn.setDoInput(true); 
     conn.setConnectTimeout(100000); // timeout 100 secs 
     conn.connect(); 

     ((TextView) findViewById(R.id.display)).append("\n \n Connecting to url :"+ conn); 
     InputStream input = conn.getInputStream(); 

     byte[] b = null; 
     input.read(b); 
     ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b); 

     FileOutputStream fOut = new FileOutputStream(to); 

     byte[] b1 = null; 
     input.read(b1); 
     ((TextView) findViewById(R.id.display)).append("\n \n output method :"+ b1); 

     int byteCount = 0; 
     byte[] buffer = new byte[4096]; 
     int bytesRead = -1; 
     while ((bytesRead = input.read(buffer)) != -1) 
     { 
      ((TextView) findViewById(R.id.display)).append("\n \n reading/writing files :"); 
      fOut.write(buffer, 0, bytesRead); 
      byteCount += bytesRead; 
     } 
     fOut.flush(); 
     ((TextView) findViewById(R.id.display)).append("\n \n flush & close :"); 
     fOut.close(); 
    } 

} 

답변

0

스트림에서 읽으려면 null 바이트 데이터를 사용하고 있습니다.

byte[] b = null; 
    input.read(b); 
    ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b); 

이 줄을 주석 처리하십시오.