2012-09-28 2 views

답변

0

이 시도,

public String downloadImages(String serverUrl,String directory) 
    { 

     if (serverUrl == null) 
      return null; 

     String LocalFilePath=serverUrl.substring(serverUrl.lastIndexOf("/")+1, serverUrl.length()); 
     File file = new File(directory, LocalFilePath); 
     if (!file.exists()) 
     { 
       int bytesread = 0; 
       byte[] bytes = new byte[1024]; 
       InputStream strm = null; 
       HttpResponse response = null; 
       HttpEntity entity = null; 
       String LocalFile = null; 
       FileOutputStream foStream; 
       BufferedOutputStream writer = null; 
       try { 
        DefaultHttpClient httpclient = new DefaultHttpClient(); 
        serverUrl = serverUrl.replace(" ", "%20"); 
        HttpGet httpget = new HttpGet(serverUrl); 

        response = httpclient.execute(httpget); 

        if (response == null) 
        { 
         return null; 
        } 
        entity = response.getEntity(); 
        strm = entity.getContent();   
        File dire = new File(directory); 
        if (!dire.isDirectory()) 
        { 
         dire.mkdirs(); 
        } 
        LocalFile = directory+LocalFilePath; 
        directory = null; 
        foStream = new FileOutputStream(LocalFile); 
        writer = new BufferedOutputStream(foStream);    

        do 
        { 
         bytesread = strm.read(bytes, 0, bytes.length); 
         if(bytesread > 0) 
         { 
          writer.write(bytes, 0, bytesread); 
          writer.flush(); 
         } 
        } while (bytesread > 0);      

        writer.close(); 
        foStream.close(); 

        strm.close();    
        return LocalFile; 
       } 
       catch(Exception e) 
       { 
        file.delete(); 
        e.printStackTrace(); 
        return null; 
       } 
     } 
     else 
     { 
      return "file exist"; 
     } 
    } 

서버에서 파일을 다운로드하여 주어진 파일 경로에

+0

감사를 저장합니다, 나는 이것을 시도. –

관련 문제