Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Image;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import javax.imageio.ImageIO;
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- import net.sf.json.JSONSerializer;
- public class GoogleApi {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- try{
- FileInputStream fstream = new FileInputStream("C:\\Users\\Rahul Akula\\workspace\\index\\src\\botanical-names.txt");
- // Get the object of DataInputStream
- String NotfoundIamges = null;
- int countNotFoiund =0;
- File fdemo = new File("Demo");
- DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- //Read File Line By Line
- while ((strLine = br.readLine()) != null) {
- // Print the content on the console
- System.out.println (strLine);
- String DirName = strLine;
- strLine=strLine.replaceAll(" ", "%20");
- System.out.println (strLine);
- URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q="+strLine+"&as_rights=%28cc_publicdomain|cc_attribute|cc_sharealike%29.-%28cc_noncommercial|cc_nonderived%29");
- URLConnection connection = url.openConnection();
- //connection.addRequestProperty("Referer", /* Enter the URL of your site here */);
- String line;
- StringBuilder builder = new StringBuilder();
- BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- while((line = reader.readLine()) != null) {
- builder.append(line);
- }
- JSONObject json = (JSONObject)JSONSerializer.toJSON(builder.toString());
- System.out.println(json);
- JSONArray results = json.getJSONObject("responseData").getJSONArray("results");
- if(results.size()>0){
- JSONObject result1 = results.getJSONObject(0);
- String url_escape= result1.getString("unescapedUrl");
- System.out.println(url_escape);
- new File(DirName).mkdir();
- System.out.println(fdemo.getAbsoluteFile().getParent()+"\\"+DirName);
- File f = new File(fdemo.getAbsoluteFile().getParent()+"\\"+DirName);
- try {
- URL url1 = new URL(url_escape);
- InputStream in1 = new BufferedInputStream(url1.openStream());
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- byte[] buf = new byte[1024];
- int n = 0;
- while (-1!=(n=in1.read(buf)))
- {
- out.write(buf, 0, n);
- }
- out.close();
- in1.close();
- byte[] response = out.toByteArray();
- FileOutputStream fos = new FileOutputStream(fdemo.getAbsoluteFile().getParent()+"\\"+DirName.trim()+"\\"+DirName.trim()+".jpg");
- fos.write(response);
- fos.close();
- } catch(IOException ioe) {
- ioe.printStackTrace();
- }
- } else {
- NotfoundIamges= NotfoundIamges+DirName+ "---";
- countNotFoiund++;
- }
- }
- System.out.println(NotfoundIamges);
- System.out.println(countNotFoiund);
- //Close the input stream
- in.close();
- }catch (Exception e){//Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment