ورود

View Full Version : مشکل کندی اتصال اینترنتی



siampour
پنج شنبه 27 شهریور 1393, 10:57 صبح
سلام من یه کد نوشتم میخوام یه سری فایل رو از تو یه سایت پیدا کنه
آدرس فایل ها به این صورته که http://exm.com/000000000001.mp3

اسم فایل یه عدد 12 رقمیه ولی همه اعداد معتبر نیست

حالا من کدی نوشتم که از 000000000000 تا 999999999999 شروع کنه به پیدا کردن و هر کدوم معتبر بود و فایل وجود داشت شماره معتبر به اضافه حجمش رو بده من .

ولی کنده خیلی طول میکشه تا این تعداد فایل رو پیدا کنه

من به صورت Thread نوشتم که برای پیدا کردن هر فایل یه Thread درست کنه . سرعتش بیشتر شد چون اینجوری همزمان تعداد فایل بیشتری رو دنبالش میگرده ولی اینجوری حجم دریافتی میره بالا. ولی بازم سرعتش قابل قبول نیست

برای من فقط حجم فایل های معتبر به شمارشون مهمه خود فایل مهم نیست . هر چی مخواد باشه

این کدیه که حجم فایل رو پیدا میکنه


int tryGetFileSize(URL url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
conn.getInputStream();
return conn.getContentLength();
} catch (IOException e) {
return -1;
} finally {
conn.disconnect();
}
}

siampour
پنج شنبه 27 شهریور 1393, 13:04 عصر
package bazar;

import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JApplet;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import static bazar.SSSS.i;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;



public class BBB extends JApplet {

SSSS baz;

public void init() {
baz = new SSSS();
}

public void start() {
baz.start();
}

public void paint(Graphics g) {
}
}



public class SSSS extends Thread {

static long i = 815739349022l;
boolean isrun = true;
public static int counter = 0;

public void run() {
while (isrun) {
while (counter < 1000) {
try {
i++;
Baz nll = new Baz(i);
Thread aa = new Thread(nll);
aa.start();
counter++;

Thread.sleep(10);
} catch (InterruptedException ex) {
}
}
if (counter >= 1000) {
try {
Thread.sleep(40);
} catch (InterruptedException ex) {
}

counter = 0;
}
}
}
}






public class Baz implements Runnable {

long m;

public Baz(long vo) {
m = vo;
}

@Override
public void run() {


DecimalFormat twoDigits = new DecimalFormat("000000000000");
long a = 1;
String base = "http://ex.com/";
String plus = ".mp3";
String address = "";
String out = "";


address += base;
address += twoDigits.format(m);
address += plus;
URL s = null;
try {
s = new URL(address);
} catch (MalformedURLException ex) {
}
int fsize = tryGetFileSize(s);
if (fsize != 0) {
// out += twoDigits.format(m) + "\t";
// out += fsize;
System.out.println(twoDigits.format(m) + "\t"+fsize);
}
address = "";
SSSS.counter--;


}

int tryGetFileSize(URL url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
conn.getInputStream();
return conn.getContentLength();
} catch (IOException e) {
return -1;
} finally {
conn.disconnect();
}
}
}