title: Stuck with Java program to download xml file with no name or file type from url
body: <p>Basically, I want a simple program to save an XML file from a URL (API endpoint)</p>
  <p>I'd like to add that: The URL has no filename or filetype (you go to the link and it displays the XML format). I'm using eclipse with java</p>
  <p>I'm currently struggling with downloading an XML file from an endpoint (e.g. <a href="https://api.511.org/traffic/events?api_key=???" rel="nofollow noreferrer">https://api.511.org/traffic/events?api_key=???</a>) when I go to the URL link with the API key it displays XML on the page which I can select all and copy,I can also right-click the site and save as an XML file but with all the code I've found online about downloading from URL in java, it returns gibberish (symbols) as the string or even when I file stream to an XML file.</p>
  <p><a href="https://i.stack.imgur.com/efVhP.png" rel="nofollow noreferrer">Image of url in use, with what is displayed on the link above</a></p>
  <p>my current code:</p>
  <pre><code>package fyp_java_api;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
  
  public class Api {
  
      public static void main(String args[]) throws IOException {
  
          URL url = new URL(&quot;https://api.511.org/traffic/events?api_key=[key is here]&quot;);
          System.out.println(&quot;opening connection&quot;);
          InputStream in = url.openStream();
          FileOutputStream fos = new FileOutputStream(new File(&quot;test.xml&quot;));
  
          System.out.println(&quot;reading from resource and writing to file...&quot;);
          int length = -1;
          byte[] buffer = new byte[1024];// buffer for portion of data from connection
          while ((length = in.read(buffer)) &gt; -1) {
              fos.write(buffer, 0, length);
          }
          fos.close();
          in.close();
          System.out.println(&quot;File downloaded&quot;);
  
      }
  }
  </code></pre>
  <p>OUTPUT:</p>
  <blockquote>
  <p>‹      í[ínÛ8}Â¿:˜Ø!©o‹…“ºI‰cÄntQŒEÇjÑä¤™ O¶?æ‘æö’’,Û±ËÖ6ÉN€±É+Z$Ï9&lt;¼”þúÏŸ
  ~ËÃ$n¸ÿ~hL£ á6ö“ˆFþp?­Ù7i‰èzß¶LK£½Æ·iäÇž?L|^¦×Œ“dâîï³‰ßÊÃç£â}¸Ì÷ ðQcqÂ’)ü|£}88ùÔ’1g^à‡ÊY 7ÆÈE§&quot;¼n&amp;&lt;ºACh/‰¦ªaÿÛMê ¾˜&amp;ã+1
  =±›   J:iÚ}Fï&gt;± àßÄo-t(Â¥1Ã@ÄÜk¡Î    ”&quot;ì&gt;n¡‘¸A†«aÔ&gt;Cg&quot;ôØýúÀ¯¢)‹îµ÷Å”Ê_ ÔÅ<em>êCä«¨MCŽ4¬&quot;´tFáer?QÝ9ïö¡£çÝY]&lt;½’Õr«;Ùø</em>
  ÃÁ‘ŸÜC+Ãï¡¸¡aÄYÂå°ÊjbÚ¤ö€h.u¾@ítâ-ÔêM¬
  ¨æ¶¬epm:ë!»‘7×E6O†æ˜Žmí5¦÷š«ÌBö?áö æ:b“1ÜßC#ëmOøa¢î2ž+U¿¶×˜DbÂ£Äçª.»…iºâzèz|äÂUn§×?r]]£fãçOhHˆÈóCè”¼ñ&amp;¡´E5lèÆžf·°¥SÝù
  a¿Çb
  ùlÐ3ÁF‚y}Î U#˜s(H!ß=?âéàË‘É¡•áU
  ×ùYçò´Ýíô/OÏû÷P÷;Œ<code>ñ»X›ÕÎðž‚O–ËÛºdÞ­‹HNðe ƒ²ÑÚjÌãá˜{SYüÐ€‘æÑ-ŠÁ á/ûMl65&lt;À@öKã+è“„·mÓ1ë&quot;|ÞØzÂ÷€ë}  vŽ²ÛB, ¤ Ìd%‡&quot;üxÿ]åüåG$Fè=çê±è;ºðÐ»~‚ŽyÀCBÐ$§#FW~,ËÔ=éž¼ït+iûè¶ jU–ˆœ†¬àqÊTê«„Çú —Z+yÜe6c±eLÖ³X…&lt;/‹ lc€ˆ¥¸lhºi;”j&amp;¦d¥{ålîlžŸÿ%Np™ãôj&quot;Ï°ò˜Ãx*’8Çç&amp;,žƒÂþ¦¤µˆUiÓÆ6 í±=Fw~èÅ’”'M”tŽ”’¾sk4KÐý¡ÏÐ¬×½Ë¾;O3¡{0‚’š9¿òáE~ˆ¸ª­ÆÓâ—˜xÆ¾‰¨”ˆ ¡ÚD|•*ü'D­§ºVzÙtÔ[œÜRò-Á¢Êúzºš’9jVq²G‹eÉæÔTàØœš´67V™š “E'Å|æù·+žÜq¢&gt;ÑL…ÈùÙDXÞP/</code>0ôî˜Ýß±ÈûM]ýAÄ0:èÈ„‚[XkçJ^&quot;ŸmøÜà¯W¬­”Z<code>=×Z…&lt;³C&amp;à»(ÜˆæÐ=ÍjŽ k­‰    1±YimÐ|.¾&amp;Õm,cIJ0øBT cB)0ªHNj”ÕØ«´²]&amp;°F_øÃñ Ó}ÁFŒ³õ:¯{‚¯³ÎÀaìÆ‡-ÌØxÈÍ7¦½žæ*äyi®ëØ‘ôv´Õ›</code>Åê¼P¡‡R¬cíèÃ_sõš®ÀQP¾¯Š×¯î9PÚ®“Òö6”þØoÂ~nÉjŸñh8fa’îƒaö&gt;D,úñPdäãîûàòõØËÖã”x-[Éj=ßÂfÇyÊfËg¦«e˜Ž.  kc‹ZUÊl¬¢l&gt;ùgçf¾–¬–K5Z®ÙWa¢‚ÁæL´Œ™¨Û4Q}ý¿Ñ÷Ìjkž‹¹»î±˜ˆn&amp;½%Xw¢é©¡–×¡îByÖâëq¢2*&amp;‹ià»J‰h&amp;~€LÔ&gt;Ë2ØŽÌM÷ÎPšf°Û“°V¤¯Í,{ç¸³=¨ž¾Î»¾qÂ:ZÝÅf9ÿWsÊøŸ
  NA~
  ?µV«ç%?Ð×$Öžf¶ÇÑMc}ÒzA‰uÚnbŒI³pžMJ\àÆ%Æ]B°P„èåF=-
  QÓ*  ðÊ»8òYDe¿óB]ÃÙ8ûmY5f¿ÓÆ¶T‘Å.XÑËSÛj9ÏO¶Îáó»Ÿ¥¡‡ÒáŠ<code>Î¿:­ kœ}©V¼Â¤¸nY¶­«t8vLªWP‰uºJ%Œ *±x¶&amp;u¾ZNºG§©0\¶O‹n{ E›Ù‡r%x¿ÛŠÇÚ É’Íµ¡Î$»õt’}6Ì'Tß´áÿ_l–™•YŽ½…4\Mv8UûûIƒ]gfÏ~:³·áæƒÿð?¼–±rƒq&gt;¡‹â¹˜W½£Ðì¿ÕŽ‚8ÄV &gt;Ðm6æÖŠ÷9ëØ,hÿeûëõ?mlâgY 9S§. Ï D´oÕy{pš™f:èØ‚ØcÏŽðf&lt; ŸXä‹iœ=2³ F!?©ÆËÐkí   à:}H‡¤ªÓ§²2ä™Ï -L4•m4¨©‘j   LÚ T[Z.9üæ Øå   ‡e¼md$ž’˜V×c÷„ƒñH@H5©1m™6¶ƒ€¬È[®“‰ô°gìO&amp;P©æ‚C“ÂËX§&quot;Mõ´m!ÑÞ„ä×‰m9°=’B¢ë†µ•ŽØudÎi¬ %&amp;s «Å†ìª#)˜k—*ŸåÝ\JjÌ]¦ÕëEÞ‹(“‰%  éñÐãiÀ–kß|È+—pJ&gt;,Ë°«¤4gú¡íàCrÈåº±€´7’2½Æ´fÚX=„%</code>&gt;zb°¨š.¼9‹×!
  ¶‰-%
  ¶®i[Ym'kQ<code>ëÅzˆ*©ŒíC2¥ÕkÚ~4RÔùá'èÝøÂ«ˆñÔAHDÓP(¥è¤Z¡ Þ&lt;Ä« L„-…Â$¸Z¦3“  }1ºÜE,¡íÍG¤”wêÔ§öLÆPR¢/o^ãõKˆŽ©•z S#[¦Cõ¼ÆcÁP:²¤-/Ó‚üò4©ñ…©´± ©^ùžñÊ=IõÇ°«”Wg   Ñ3¨J˜¢O&gt;¿›½ñ¿yÍxé)káñHb£ŒçöÚì1ì²cQpÇÅsŽ†éÏI¨g&gt;µ›’é:Á¶Vå1kýècìè›Qzµð·äˆí^:®²wPHPÏZÃv-Uuä¡!F£˜' ÃHÞð„É²•$þ'0óò;¿ÿ‡CŒ7={ä4õ1›lHGMâ</code>îÏ6‡ÌT˜Ìˆ-‰ŽÓ‘»%0·ÿ˜x=w¨D</p>
  </blockquote>