Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ai;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Iterator;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.json.simple.JSONObject;
- import org.apache.commons.net.ftp.FTPClient;
- import org.json.simple.JSONArray;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- /**
- *
- * @author rashad
- *
- */
- public class project {
- /**
- * @param args the command line arguments
- * @throws java.io.IOException
- */
- static String ftpUrl = "ftp://%s:%s@%s/%s;type=i";
- static String host = "http://3lawad3k.com";
- static String user = "ai@3lawad3k.com";
- static String pass = "I6ZIAJ(p@rmi";
- static String filePath = "/home/rashad/NetBeansProjects/AI Project/query.txt";
- static String uploadPath = "/FolderPath";
- public static void main(String[] args) throws IOException {
- ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter a query:\t");
- String sentence = scanner.nextLine();
- if (sentence != null) {
- createJSON(sentence);
- uploadQuery();
- }
- Thread Read = new Thread(() -> {
- while (true) {
- listenToServer();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- Logger.getLogger(project.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- });
- }
- public static void createJSON(String str) throws IOException {
- JSONObject query = new JSONObject();
- query.put("Name", "query");
- query.put("search", str);
- try (FileWriter file = new FileWriter("query.txt")) {
- file.write(query.toJSONString());
- }
- }
- private static void uploadQuery() {
- try {
- URL url = new URL(ftpUrl);
- URLConnection conn = url.openConnection();
- OutputStream outputStream = conn.getOutputStream();
- FileInputStream inputStream = new FileInputStream(filePath);
- byte[] buffer = new byte[9600];
- int bytesRead = -1;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- }
- inputStream.close();
- outputStream.close();
- System.out.println("Query uploaded");
- } catch (IOException ex) {
- }
- }
- private static void listenToServer() {
- FTPClient client = new FTPClient();
- FileOutputStream fos = null;
- try {
- client.connect(ftpUrl);
- client.login(user, pass);
- String filename = "query.txt";
- fos = new FileOutputStream(filename);
- client.retrieveFile("/" + filename, fos);
- } catch (IOException e) {
- } finally {
- try {
- if (fos != null) {
- fos.close();
- readResponse();
- }
- client.disconnect();
- } catch (IOException e) {
- }
- }
- }
- private static void readResponse() {
- JSONParser parser = new JSONParser();
- try {
- Object obj = parser.parse(new FileReader("query.txt"));
- JSONObject jsonObject = (JSONObject) obj;
- String answer = (String) jsonObject.get("response");
- System.out.println("answer: " + answer);
- } catch (IOException | ParseException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement