Главная » soft, programming » Java скачивание рандомных файлов с ресурса

Java скачивание рандомных файлов с ресурса

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Random;

/**
* ICQ File Scanner v0.3
*
*/
public class IFS {

private static final int THREADS = 50;

private static class MyRunnable implements Runnable {

private static final String CHARS = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
private static final int ID_LENGTH = 6;
private static final String STR_MESSAGE = "(%d) %s";
private static final String STR_SUCCESS = "(%d) %s %d/%d=%f %s %d %s";
private static final String FILE_NAME = "ifs%d.txt";
private static final String URL = "http://files.goodresource.net/files/get?fileId=%s";
private static final int MAX_SIZE = 20 * 1024 * 1024;

private final Random random = new Random();
private final int mThreadId;
private int mSuccess = 0;
private int mTotal = 0;

public MyRunnable(int id) {
mThreadId = id;
}

@Override
public void run() {

final String path = "out";
File f = new File(path);
f.mkdirs();

final String fileName = String.format(FILE_NAME, mThreadId);
while (true) {
mTotal++;
String s = generateString(random, CHARS, ID_LENGTH);
final String url = String.format(URL, s);
try {
URL u = new URL(url);

URLConnection uc = u.openConnection();
uc.connect();
uc.getContent();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
String status = "done    ";
if(contentLength < MAX_SIZE) {
if ("image/jpeg".equals(contentType)){
File file = new File(path, s + ".jpg");
saveBinaryFile(uc, contentLength, u, file);
}else if("image/tiff".equals(contentType)){
File file = new File(path, s + ".tiff");
saveBinaryFile(uc, contentLength, u, file);
}else if("image/x-ms-bmp".equals(contentType)){
File file = new File(path, s + ".bmp");
saveBinaryFile(uc, contentLength, u, file);
}else if("image/png".equals(contentType)){
File file = new File(path, s + ".png");
saveBinaryFile(uc, contentLength, u, file);
}else if("image/gif".equals(contentType)){
File file = new File(path, s + ".gif");
saveBinaryFile(uc, contentLength, u, file);
}else if("application/x-rar-compressed".equals(contentType)){
File file = new File(path, s + ".gif");
saveBinaryFile(uc, contentLength, u, file);
}else if("application/zip".equals(contentType)){
File file = new File(path, s + ".gif");
saveBinaryFile(uc, contentLength, u, file);
}else if("application/7z".equals(contentType)){
File file = new File(path, s + ".gif");
saveBinaryFile(uc, contentLength, u, file);
}else{
status = "unknown";
}
}else{
status = "too big";
}
mSuccess++;
final double p = mSuccess / mTotal;
final String out = String.format(STR_SUCCESS,
mThreadId, status, mSuccess, mTotal, p, contentType,
contentLength, url);
System.out.println(out);
writeToFile(fileName, out);
} catch (MalformedURLException e) {
System.out.println(String.format(STR_MESSAGE, mThreadId,
e.getMessage()));
} catch (FileNotFoundException e) {

} catch (IOException e) {
System.out.println(String.format(STR_MESSAGE, mThreadId,
e.getMessage()));
}
}
}

private void saveBinaryFile(URLConnection uc, int contentLength, URL u, File fileName) throws IOException {
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1)
break;
offset += bytesRead;
}
in.close();

if (offset != contentLength) {
throw new IOException("Only read " + offset
+ " bytes; Expected " + contentLength + " bytes");
}

FileOutputStream out = new FileOutputStream(fileName);
out.write(data);
out.flush();
out.close();
}

private void writeToFile(final String fileName, final String out) {
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter(fileName, true);
writer = new BufferedWriter(fWriter);
writer.append(out);
writer.newLine();
} catch (IOException e) {
System.out.println(String.format(STR_MESSAGE, mThreadId,
e.getMessage()));
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
}
}
}

}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("ICQ Files Scanner 0.3");
for (int i = 0; i < THREADS; i++) {
new Thread(new MyRunnable(i)).start();
}
}

public static String generateString(Random rng, String characters,
int length) {
char[] text = new char[length];
for (int i = 0; i < length; i++) {
text[i] = characters.charAt(rng.nextInt(characters.length()));
}
return new String(text);
}

}

Реклама

Добавить комментарий

Заполните поля или щелкните по значку, чтобы оставить свой комментарий:

Логотип WordPress.com

Для комментария используется ваша учётная запись WordPress.com. Выход / Изменить )

Фотография Twitter

Для комментария используется ваша учётная запись Twitter. Выход / Изменить )

Фотография Facebook

Для комментария используется ваша учётная запись Facebook. Выход / Изменить )

Google+ photo

Для комментария используется ваша учётная запись Google+. Выход / Изменить )

Connecting to %s