All pastes #456839 Raw Edit

Mine

public text v1 · immutable
#456839 ·published 2007-04-25 01:13 UTC
rendered paste body
    public synchronized static DownloadStatus DownloadFile(String URLPath, String LocalFilePath)
    {
        assert (URLPath != null);
        assert (LocalFilePath != null);

        try {

            URL url = new URL(URLPath);

            URLConnection connection = url.openConnection();
            
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.connect();
            
            BufferedReader input = new BufferedReader( new InputStreamReader( connection.getInputStream()));            

            String output = "";
            String totalOutput = "";
            
            while ( (output = input.readLine()) != null) {
                totalOutput += (output + "\n");
            }
            
            String dirLocalFilePath = LocalFilePath.substring(0, LocalFilePath.lastIndexOf(File.separator)) + File.separator;

            if (!new File(dirLocalFilePath).exists()) {
                new File(dirLocalFilePath).mkdirs();
            }

            BufferedWriter fileOut  = new BufferedWriter( new FileWriter(LocalFilePath));

            fileOut.write( totalOutput );

            fileOut.close();
            
        } catch (UnknownHostException e ) {
            return DownloadStatus.NO_CONNECTION;
        } catch (FileNotFoundException e) {
            return DownloadStatus.FILE_NOT_FOUND;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return DownloadStatus.UNKNOWN_ERROR;
        }

        return DownloadStatus.SUCCESS;
    }