title: Download a file with no extension from a server
body: <p>I'm try to download a mp3 file from following URL. I found lot of articles and examples regarding file download. Those examples are based on URLs that end with a file extension, e.g.:- <code>yourdomain.com/filename.mp3</code> but I want to download a file from following url which typically does not end with file extension.</p>
  
  <p><code>youtubeinmp3.com/download/get/?i=1gsE32jF0aVaY0smDVf%2BmwnIZPrMDnGmchHBu0Hovd3Hl4NYqjNdym4RqjDSAis7p1n5O%2BeXmdwFxK9ugErLWQ%3D%3D</code> </p>
  
  <p>**Please note that I use the above url as-is without using Stackoverflow url formatting method to easily understand the question.</p>
  
  <p>** <strong>I have tried the @Arsal Imam's solution as follows still not working</strong></p>
  
  <pre><code>   btnShowProgress.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // starting new Async Task
              File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"Folder Name");
              if(!cacheDir.exists())
                  cacheDir.mkdirs();
  
              File f=new File(cacheDir,"ddedddddd.mp3");
              saveDir=f.getPath();
  
              new DownloadFileFromURL().execute(fileURL);
          }
      });
  </code></pre>
  
  <p>and the async task code is as follows</p>
  
  <pre><code>class DownloadFileFromURL extends AsyncTask&lt;String, String, String&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          showDialog(progress_bar_type);
      }
  
      @Override
      protected String doInBackground(String... f_url) {
          try{
  
              URL url = new URL(fileURL);
              HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
              int responseCode = httpConn.getResponseCode();
  
              // always check HTTP response code first
              if (responseCode == HttpURLConnection.HTTP_OK) {
                  String fileName = "";
                  String disposition = httpConn.getHeaderField("Content-Disposition");
                  String contentType = httpConn.getContentType();
                  int contentLength = httpConn.getContentLength();
  
                  if (disposition != null) {
                      // extracts file name from header field
                      int index = disposition.indexOf("filename=");
                      if (index &gt; 0) {
                          fileName = disposition.substring(index + 10,
                                  disposition.length() - 1);
                      }
                  } else {
                      // extracts file name from URL
                      fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                              fileURL.length());
                  }
  
                  System.out.println("Content-Type = " + contentType);
                  System.out.println("Content-Disposition = " + disposition);
                  System.out.println("Content-Length = " + contentLength);
                  System.out.println("fileName = " + fileName);
  
                  // opens input stream from the HTTP connection
                  InputStream inputStream = httpConn.getInputStream();
                  String saveFilePath = saveDir + File.separator + fileName;
  
                  // opens an output stream to save into file
                  FileOutputStream outputStream = new FileOutputStream(saveDir);
  
                  int bytesRead = -1;
                  byte[] buffer = new byte[BUFFER_SIZE];
                  while ((bytesRead = inputStream.read(buffer)) != -1) {
                      outputStream.write(buffer, 0, bytesRead);
                  }
  
                  outputStream.close();
                  inputStream.close();
  
                  System.out.println("File downloaded");
              } else {
                  System.out.println("No file to download. Server replied HTTP code: " + responseCode);
              }
              httpConn.disconnect();
  
          }catch(Exception e){
              e.printStackTrace();
          }
          return null;
      }
  
      protected void onProgressUpdate(String... progress) {
          pDialog.setProgress(Integer.parseInt(progress[0]));
      }
  
      @Override
      protected void onPostExecute(String file_url) {
          dismissDialog(progress_bar_type);
  
      }
  }
  </code></pre>