скачать файлы из контакта |
Cообщение скрыто для удобства комментирования.
Прочитать сообщение
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vkphoto.english;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.json.JSONException;
import vkphoto.JsonPerserUtil;
import vkphoto.JsonPhoto;
/**
*
* @author isalnikov
*/
public class NewMain {
public static String path = "c:/vkphoto/all/";
public static String path_patern = "c:/vkphoto/%s/";
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, JSONException, Exception {
// TODO code application logic here
URL url = NewMain.class.getResource("english.txt");
System.out.println(url.getFile());
File file = new File(url.getFile());
FileInputStream fStream = new FileInputStream(file);
InputStreamReader iReader = new InputStreamReader(fStream, "windows-1251");
BufferedReader br = new BufferedReader(iReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
// System.out.println(sb.toString());
ArrayList<JsonPhoto> all = JsonPerserUtil.findObjectByText(sb.toString(),JsonPerserUtil.ALL);
for (JsonPhoto jsonPhoto : all) {
System.out.println(jsonPhoto.getBiggerSrc());
NewMain.saveFile(jsonPhoto.getBiggerSrc());
}
}
public static void saveFile(String url) throws Exception{
URL u = new URL(url);
String filename = u.getFile();
System.out.println("**** filename " +path+filename + "****");
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
if ( contentLength == -1) {
throw new IOException("This is not a binary file.");
}
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
double max = 100/contentLength;
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");
}
filename = u.getFile().substring(filename.lastIndexOf('/') + 1);
FileOutputStream out = new FileOutputStream(path+filename);
out.write(data);
out.flush();
out.close();
System.out.println("**** done ****");
}
public static void setDirName(String dirname) {
path = String.format(path_patern,dirname);
File file = new File(path);
file.mkdirs();
}
public static void setResource(String resource) throws IOException{
URL url = NewMain.class.getResource("english.txt");
System.out.println(url.getFile());
File file = new File(url.getFile());
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Write some data to file: " + file);
bw.write(resource);
bw.close();
fw.close();
}
}
* This source code was highlighted with Source Code Highlighter.| Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |