使用Java从MediaFire下载文件 [英] Download a file from MediaFire with Java

查看:107
本文介绍了使用Java从MediaFire下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,MediaFire不允许直接下载链接,但是您实际上必须单击下载"按钮以生成引用该文件的随机链接.有没有办法获取该链接并下载它?

As we all know, or might know, MediaFire does not allow direct download links, but you actually have to click the Download button to generate a random link that refers to the file. Is there a way to fetch that link and download it?

推荐答案

在编写自动更新应用程序的过程中,我写了一个简短的Java应用程序,可以从MediaFire下载文件.这是完整的代码:

In despair of writing an auto-updating application, I have written a short Java application which allows the download of files from MediaFire. Here is the full code:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class mainwindow {

    /**
     * Launch the application.
     */
    static mainwindow thisInstance;
    public static void main(String[] args) {
        new mainwindow();

    }
    public mainwindow() 
{
        otherthread();
    }
    public void otherthread()
    {
        navigate("http://www.mediafire.com/download/aqtmhwvb8yvqclu/SmartSharePC.jar","downloadedFromMediafire");
//      navigate("http://www.mediafire.com/download/j7e4wh6hbdhdj84/Minecraft+1.5.2-+C.H.T.zip","mediafire");
    }
    private void navigate(String url,String sufix)
    {
        try 
        {
            String downloadLink = fetchDownloadLink(getUrlSource(url));
            saveUrl(downloadLink,sufix);
        } catch (Exception e) 
        {
            e.printStackTrace();
        }

    }
    public void saveUrl(final String urlString,String sufix) throws Exception 
    {
        System.out.println("Downloading...");
        String filename = urlString.substring(urlString.lastIndexOf("/")+1, urlString.lastIndexOf("."))+"_"+sufix+urlString.substring(urlString.lastIndexOf("."), urlString.length());
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(urlString).openStream());
            fout = new FileOutputStream(filename);

            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) 
            {
                fout.write(data, 0, count);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (fout != null) {
                fout.close();
            }
        }
        System.out.println("Success!");
    }
    private static String getUrlSource(String url) throws IOException 
    {
        System.out.println("Connecting...");
        URL yahoo = new URL(url);
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                yc.getInputStream(), "UTF-8"));
        String inputLine;
        String total="";
        while ((inputLine = in.readLine()) != null)
           total+=inputLine;
        in.close();

        return total;
    }
    private static String fetchDownloadLink(String str)
    {
        System.out.println("Fetching download link");
        try {
            String regex = "(?=\\<)|(?<=\\>)";
            String data[] = str.split(regex);
            String found = "NOTFOUND";
            for (String dat : data) {
                if (dat.contains("DLP_mOnDownload(this)")) {
                    found = dat;
                    break;
                }
            }
            String wentthru = found.substring(found.indexOf("href=\"") + 6);
            wentthru = wentthru.substring(0, wentthru.indexOf("\""));
            return wentthru;
        } catch (Exception e) 
        {
            e.printStackTrace();
            return "ERROR";
        }
    }
}

这篇关于使用Java从MediaFire下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆