2015-09-10 4 views
12

Я пытаюсь загрузить mp3-файл со следующего URL-адреса. Я нашел много статей и примеров загрузки файлов. Эти примеры основаны на URL-адресах, которые заканчиваются расширением файла, например: -yourdomain.com/filename.mp3, но я хочу загрузить файл из следующего URL-адреса, который обычно не заканчивается расширением файла.Загрузите файл без расширения с сервера

youtubeinmp3.com/download/get/?i=1gsE32jF0aVaY0smDVf%2BmwnIZPrMDnGmchHBu0Hovd3Hl4NYqjNdym4RqjDSAis7p1n5O%2BeXmdwFxK9ugErLWQ%3D%3D

** Пожалуйста, обратите внимание, что я использую выше URL как есть без использования StackOverflow метода URL форматирования легко понять вопрос.

** Я попробовал решение в @Arsal имама следующим образом до сих пор не работает

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); 
     } 
    }); 

и код асинхронной задача заключается в следующем

class DownloadFileFromURL extends AsyncTask<String, String, String> { 

    @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 > 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); 

    } 
} 
+0

Интересный вопрос. Знаете ли вы заранее расширение файла? –

+0

его mp3-файл. Я пробую это от 'youtubeinmp3.com/api', чтобы получить mp3-версию youtube api. этот URL-адрес прекрасно работает, когда копировать его в URL-адрес браузера. но мне нужно загрузить его из приложения Android. –

+0

Попробуйте получить динамический тип ответа MIME. Этот [Link] (http://stackoverflow.com/questions/9077933/how-to-find-mimetype-of-response) может вам помочь. – avinash

ответ

5

Хотя Volley library не рекомендуется для больших загрузки или однако, я хотел бы поделиться своим следующим рабочий образец кода.

Предположим, мы загружаем только файлы MP3, поэтому я жестко программирую расширение. И, конечно же, мы должны тщательно проверить, чтобы избежать исключений (NullPointer ...), например, проверить, содержат ли заголовки "Content-Disposition" или нет.

Надеюсь, это поможет!

Volley Пользовательский класс:

public class BaseVolleyRequest extends Request<NetworkResponse> { 

    private final Response.Listener<NetworkResponse> mListener; 
    private final Response.ErrorListener mErrorListener; 

    public BaseVolleyRequest(String url, Response.Listener<NetworkResponse> listener, Response.ErrorListener errorListener) { 
     super(0, url, errorListener); 
     this.mListener = listener; 
     this.mErrorListener = errorListener; 
    } 

    @Override 
    protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) { 
     try { 
      return Response.success(
        response, 
        HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (JsonSyntaxException e) { 
      return Response.error(new ParseError(e)); 
     } catch (Exception e) { 
      return Response.error(new ParseError(e)); 
     } 
    } 

    @Override 
    protected void deliverResponse(NetworkResponse response) { 
     mListener.onResponse(response); 
    } 

    @Override 
    protected VolleyError parseNetworkError(VolleyError volleyError) { 
     return super.parseNetworkError(volleyError); 
    } 

    @Override 
    public void deliverError(VolleyError error) { 
     mErrorListener.onErrorResponse(error); 
    } 
} 

Затем в деятельности:

public class BinaryVolleyActivity extends AppCompatActivity { 

    private final Context mContext = this; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_binary_volley); 
     RequestQueue requestQueue = Volley.newRequestQueue(mContext); 
     String url = "http://www.youtubeinmp3.com/download/get/?i=3sI2yV5mJ0kQ8CnddqmANZqK8a%2BgVQJ%2Fmg3xwhHTUsJKuusOCZUzebuWW%2BJSFs0oz8VTs6ES3gjohKQMogixlQ%3D%3D"; 
     BaseVolleyRequest volleyRequest = new BaseVolleyRequest(url, new Response.Listener<NetworkResponse>() { 
      @Override 
      public void onResponse(NetworkResponse response) {      
       Map<String, String> headers = response.headers; 
       String contentDisposition = headers.get("Content-Disposition"); 
       // String contentType = headers.get("Content-Type"); 
       String[] temp = contentDisposition.split("filename="); 
       String fileName = temp[1].replace("\"", "") + ".mp3"; 
       InputStream inputStream = new ByteArrayInputStream(response.data); 
       createLocalFile(inputStream, fileName); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e("Volley", error.toString()); 
      } 
     }); 

     volleyRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 10, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 

     requestQueue.add(volleyRequest); 
    } 

    private String createLocalFile(InputStream inputStream, String fileName) { 
     try { 
      String folderName = "MP3VOLLEY"; 
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
      File folder = new File(extStorageDirectory, folderName); 
      folder.mkdir(); 
      File file = new File(folder, fileName); 
      file.createNewFile(); 
      FileOutputStream f = new FileOutputStream(file); 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = inputStream.read(buffer)) > 0) { 
       f.write(buffer, 0, length); 
      } 
      //f.flush(); 
      f.close(); 
      return file.getPath(); 
     } catch (IOException e) { 
      return e.getMessage(); 
     } 
    } 
} 

Вот скриншот результата:

Volley download file

Примечание:

Как я комментировал ниже, поскольку прямой URL загрузки регулярно меняется, вы должны проверить новый URL-адрес с некоторыми инструментами, такими как Postman for Chrome, если это ответы двоичная вместо веб-страницы (истекли URL), а затем Url действителен, и мой код работает для этого Url.

См двух следующих скриншотах:

Истекло URL:

Expired url

Un-истекли URL:

Un-expired url

UPDATE BASIC LOGIC ДЛЯ ЧЕГО Прямая ссылка ОТ ДОКУМЕНТАЦИИ САЙТА:

Согласно Create Your Own YouTube To MP3 Downloader For Free

Вы посмотрите на

JSON Пример

Вы также можете получить данные в формате JSON, установив "формат" параметр в "JSON" может. http://YouTubeInMP3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=i62Zjga8JOM

Во-первых, вы создаете JsonObjectRequest при получении ответа от указанной выше ссылки на файл. Затем, внутри onResponse этого JsonObjectRequest вы получите ссылку для скачивания прямой, как этот directUrl = response.getString("link"); и использовать BaseVolleyRequest volleyRequest

Я просто сказал логике для получения прямой ссылки, ИМО, вы должны реализовать ее самостоятельно. Удачи!

+0

это не сработало для меня .. Я пробовал. Он всегда выбрасывает исключения с нулевым указателем в 'String [] temp = contentDisposition.split (" filename = ");' this line.because 'contentDisposition' переменная' null' –

+0

Вы пытались с моим Url? Это реальный Url, который вы можете протестировать с помощью Postman в Chrome :) – BNK

+0

да Я пробовал с вашим адресом –

4

Используйте ниже код он прекрасно работает для зашифрованные URL-адреса

public class HttpDownloadUtility { 
    private static final int BUFFER_SIZE = 4096; 

    /** 
    * Downloads a file from a URL 
    * @param fileURL HTTP URL of the file to be downloaded 
    * @param saveDir path of the directory to save the file 
    * @throws IOException 
    */ 
    public static void downloadFile(String fileURL, String saveDir) 
      throws IOException { 
     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 > 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(saveFilePath); 

      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(); 
    } 
} 
+0

Хороший вопрос ...! –

+0

, когда загрузка url копирует в адресную строку браузера .. его загрузка .. но не работает так –

0

URL-адрес возвращается перенаправление 302 t o фактический .mp3. Браузер выполняет перенаправление в фоновом режиме для вас, но в вашем приложении вам нужно сделать это самостоятельно. Вот пример того, как это сделать с HttpUrlConnection http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/

+0

Я проверил код ответа, его '200' –

0

Если вы знаете тип файла заранее, вы можете загрузить его с URL-адреса, который не имеет расширения.

.java Сервис загрузки

public class DownloadService extends IntentService { 
    public static final int UPDATE_PROGRESS = 8344; 
    private Context context; 
    private PowerManager.WakeLock mWakeLock; 
    ProgressDialog mProgressDialog; 
    String filename; 
    File mypath; 
    String urlToDownload; 
    BroadcastReceiver broadcaster; 
    Intent intent1; 
    static final public String BROADCAST_ACTION = "com.example.app.activity.test.broadcast"; 
    public DownloadService() { 
     super("DownloadService"); 
    } 
    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     intent1 = new Intent(BROADCAST_ACTION); 
    } 
    @Override 
    protected void onHandleIntent(Intent intent) { 

     ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); 
     try { 
      intent1 = new Intent(BROADCAST_ACTION); 
      urlToDownload = intent.getStringExtra("url"); 
      filename= intent.getStringExtra("filename"); 

      BufferedWriter out; 
      try { 
       File path=new File("/sdcard/","folder name"); 
       path.mkdir(); 
       mypath=new File(path,filename); 
       Log.e("mypath",""+mypath); 
       if (!mypath.exists()) { 
        out= new BufferedWriter(new FileWriter(mypath)); 
        //ut = new OutputStreamWriter(context.openFileOutput(mypath.getAbsolutePath() ,Context.MODE_PRIVATE)); 
        out.write("test"); 
        out.close(); 

       } 

      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      URL url = new URL(urlToDownload); 
      URLConnection connection = url.openConnection(); 
      connection.connect(); 
      // this will be useful so that you can show a typical 0-100% progress bar 
      int fileLength = connection.getContentLength(); 

      // download the file 
      InputStream input = new BufferedInputStream(connection.getInputStream()); 
      OutputStream output = new FileOutputStream(mypath); 

      byte data[] = new byte[4096]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       Bundle resultData = new Bundle(); 
       resultData.putInt("progress" ,(int) (total * 100/fileLength)); 


       //Log.e("mypath",""+mypath); 
       resultData.putString("mypath", ""+mypath); 
       receiver.send(UPDATE_PROGRESS, resultData);   

       output.write(data, 0, count); 
      } 

      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     Bundle resultData = new Bundle(); 
     resultData.putInt("progress" ,100); 
     resultData.putString("mypath", ""+mypath); 
     receiver.send(UPDATE_PROGRESS, resultData); 
     intent1.putExtra("progressbar", 100); 
     sendBroadcast(intent1); 
    } 
} 

DownloadReceiver.java

public class DownloadReceiver extends ResultReceiver{ 

    private Context context; 
    private PowerManager.WakeLock mWakeLock; 
    ProgressDialog mProgressDialog; 
    String filename; 

    String mypath; 
    public DownloadReceiver(Handler handler ,String filename ,Context context) { 
     super(handler); 

    this.context = context; 
     this.filename = filename; 

     mProgressDialog = new ProgressDialog(context); 
    } 

    @Override 
    protected void onReceiveResult(int resultCode, Bundle resultData) { 
     super.onReceiveResult(resultCode, resultData); 
     if (resultCode == DownloadService.UPDATE_PROGRESS) { 
      int progress = resultData.getInt("progress"); 
      mypath = resultData.getString("mypath"); 
      mProgressDialog.setProgress(progress); 
      //Log.e("progress","progress"); 
      mProgressDialog.setMessage("App name"); 
      mProgressDialog.setIndeterminate(true); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(true); 
      if (progress == 100) { 
       mProgressDialog.dismiss(); 

      Log.e("download","download"); 

      } 
     } 
    } 
} 

Теперь запустите службу в вашем mainactivity на код ниже:

Intent miIntent = new Intent(mContext, DownloadService.class); 
          miIntent.putExtra("url", url); 
          miIntent.putExtra("filename", id+".mp3"); 

          miIntent.putExtra("receiver", new DownloadReceiver(new Handler() , id,mContext)); 
          startService(miIntent); 
Смежные вопросы