Advertisement
Guest User

AI

a guest
Feb 20th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. package ai;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.util.Iterator;
  12. import java.util.Scanner;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import org.json.simple.JSONObject;
  16. import org.apache.commons.net.ftp.FTPClient;
  17. import org.json.simple.JSONArray;
  18. import org.json.simple.parser.JSONParser;
  19. import org.json.simple.parser.ParseException;
  20.  
  21. /**
  22.  *
  23.  * @author rashad
  24.  *
  25.  */
  26. public class project {
  27.  
  28.     /**
  29.      * @param args the command line arguments
  30.      * @throws java.io.IOException
  31.      */
  32.    
  33.        static String ftpUrl = "ftp://%s:%s@%s/%s;type=i";
  34.        static String host = "http://3lawad3k.com";
  35.        static String user = "ai@3lawad3k.com";
  36.        static String pass = "I6ZIAJ(p@rmi";
  37.        static String filePath = "/home/rashad/NetBeansProjects/AI Project/query.txt";
  38.        static String uploadPath = "/FolderPath";
  39.        
  40.        
  41.     public static void main(String[] args) throws IOException {
  42.  
  43.  
  44.  
  45.         ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);
  46.        
  47.         Scanner scanner = new Scanner(System.in);
  48.  
  49.         System.out.print("Enter a query:\t");
  50.  
  51.         String sentence = scanner.nextLine();
  52.  
  53.         if (sentence != null) {
  54.  
  55.             createJSON(sentence);
  56.             uploadQuery();
  57.  
  58.         }
  59.  
  60.         Thread Read = new Thread(() -> {
  61.             while (true) {
  62.  
  63.                 listenToServer();
  64.  
  65.                 try {
  66.  
  67.                     Thread.sleep(1000);
  68.  
  69.                 } catch (InterruptedException ex) {
  70.  
  71.                     Logger.getLogger(project.class.getName()).log(Level.SEVERE, null, ex);
  72.  
  73.                 }
  74.  
  75.             }
  76.         });
  77.  
  78.     }
  79.  
  80.     public static void createJSON(String str) throws IOException {
  81.  
  82.         JSONObject query = new JSONObject();
  83.  
  84.         query.put("Name", "query");
  85.         query.put("search", str);
  86.  
  87.         try (FileWriter file = new FileWriter("query.txt")) {
  88.  
  89.             file.write(query.toJSONString());
  90.  
  91.         }
  92.  
  93.     }
  94.  
  95.     private static void uploadQuery() {
  96.  
  97.         try {
  98.  
  99.             URL url = new URL(ftpUrl);
  100.             URLConnection conn = url.openConnection();
  101.  
  102.             OutputStream outputStream = conn.getOutputStream();
  103.             FileInputStream inputStream = new FileInputStream(filePath);
  104.  
  105.             byte[] buffer = new byte[9600];
  106.  
  107.             int bytesRead = -1;
  108.  
  109.             while ((bytesRead = inputStream.read(buffer)) != -1) {
  110.  
  111.                 outputStream.write(buffer, 0, bytesRead);
  112.  
  113.             }
  114.  
  115.             inputStream.close();
  116.             outputStream.close();
  117.  
  118.             System.out.println("Query uploaded");
  119.  
  120.         } catch (IOException ex) {
  121.  
  122.         }
  123.  
  124.     }
  125.  
  126.     private static void listenToServer() {
  127.  
  128.         FTPClient client = new FTPClient();
  129.  
  130.         FileOutputStream fos = null;
  131.  
  132.         try {
  133.  
  134.             client.connect(ftpUrl);
  135.  
  136.             client.login(user, pass);
  137.  
  138.             String filename = "query.txt";
  139.  
  140.             fos = new FileOutputStream(filename);
  141.  
  142.             client.retrieveFile("/" + filename, fos);
  143.  
  144.         } catch (IOException e) {
  145.  
  146.  
  147.         } finally {
  148.  
  149.             try {
  150.  
  151.                 if (fos != null) {
  152.  
  153.                     fos.close();
  154.  
  155.                    
  156.                     readResponse();
  157.  
  158.                 }
  159.  
  160.                 client.disconnect();
  161.  
  162.             } catch (IOException e) {
  163.  
  164.  
  165.             }
  166.  
  167.         }
  168.  
  169.     }
  170.  
  171.     private static void readResponse() {
  172.  
  173.  
  174.  
  175.                 JSONParser parser = new JSONParser();
  176.  
  177.         try {
  178.  
  179.             Object obj = parser.parse(new FileReader("query.txt"));
  180.  
  181.             JSONObject jsonObject = (JSONObject) obj;
  182.  
  183.             String answer = (String) jsonObject.get("response");
  184.  
  185.             System.out.println("answer: " + answer);
  186.          
  187.         } catch (IOException | ParseException e) {
  188.  
  189.  
  190.  
  191.         }
  192.        
  193.        
  194.        
  195.        
  196.        
  197.        
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement