All pastes #437319 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-04-12 23:47 UTC
rendered paste body
   public static long DownloadFile( String URLPath, String LocalFilePath)
    {
        if (URLPath == null)
            return -1;

        if (URLPath.trim().length() < 1)
            return -1;
        
        String dirLocalFilePath = LocalFilePath.substring(0, LocalFilePath.lastIndexOf(File.separator)) + File.separator;
        
        if (!new File(dirLocalFilePath).exists())
            new File(dirLocalFilePath).mkdirs();
        
        BufferedReader in = null;
        BufferedWriter out = null;
        
        HttpURLConnection connection = null;
        URL url = null;
              
        try
        {
            url = new URL(URLPath);
                       
            connection = (HttpURLConnection) url.openConnection();
               
            in  = new BufferedReader( new InputStreamReader(url.openStream()));
            out = new BufferedWriter( new FileWriter( LocalFilePath ));

            String str;
    
            while ((str = in.readLine()) != null)
                out.write( str + "\n" );
        }         
        catch (Exception a) {
            return -1;
        }         
        finally {
            try {
                if (connection != null)
                    connection.disconnect();
                if (out != null)
                    out.close();
                if (in != null)
                    in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        return new File(LocalFilePath).length();
    }