SHARE
TWEET

Untitled

a guest Feb 16th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.FileOutputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.TimeZone;
  10.  
  11. public class FileDownloader {
  12.  
  13.   public static void main(String[] args) throws Exception {
  14.     downloadWebFile(" https://www.wsitrader.com/Services/CSVDownloadService.svc/GetWindcastIQHourlyForecast?Account=transaltaw&Profile=robert_nagel@transalta.com&Password=clipperw&ForecastDate=2020-02-13&ForecastType=Latest&SiteIds=e44e15bc-4200-11ea-965b-0a68756b68b8",
  15.       "/Users/laichian/Desktop/JavaPractice/", true);
  16.   }
  17.  
  18.   public static void downloadWebFile(String url, String savePath, boolean appendDateTime) throws Exception {
  19.     URL sURL = new URL(url);
  20.     String time = "";
  21.     String fileExtension = "";
  22.  
  23. //    System.out.println("URL: " + sURL);
  24. //    System.out.println("save path: " + savePath);
  25.  
  26.     HttpURLConnection httpConn = (HttpURLConnection) sURL.openConnection();
  27. //    System.out.println("HTTP: " + httpConn);
  28.  
  29.     String fieldValue = httpConn.getHeaderField("Content-Disposition");
  30. //    System.out.println(fieldValue);
  31.     String fileName = fieldValue.substring(fieldValue.indexOf("filename=\"") + 10, fieldValue.length() - 1);
  32. //    System.out.println(fileName);
  33.  
  34.     if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
  35.       fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
  36.     }
  37.  
  38.     String fileNameWithoutExtension = fileName.replaceFirst("[.][^.]+$", "");
  39.  
  40. //    System.out.println(fileExtension);
  41. //    System.out.println(fileNameWithoutExtension);
  42.     if (appendDateTime) {
  43.       Date date = new Date();
  44.       DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
  45.       formatter.setTimeZone(TimeZone.getTimeZone("EST"));
  46.       time = formatter.format(date);
  47.       System.out.println(time);
  48.     }
  49.  
  50.     BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream());
  51.     FileOutputStream fos = new FileOutputStream(savePath + fileNameWithoutExtension + time + "." +fileExtension);
  52.     BufferedOutputStream out = new BufferedOutputStream(fos, 1024);
  53.  
  54.     byte[] buffer = new byte[1024];
  55.     int read = 0;
  56.     while((read = in.read(buffer, 0, 1024)) >= 0) {
  57.       out.write(buffer, 0, read);
  58.     }
  59.  
  60.     out.close();
  61.     in.close();
  62.     System.out.println("Completed");
  63.  
  64.   }
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top