Advertisement
vitareinforce

file transfer

Apr 21st, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. public class FileTransfer {
  2.     Settings gb = new Settings();
  3.     RMQ rmq = new RMQ();
  4.     Long tsLong = System.currentTimeMillis()/1000;
  5.  
  6.     private static final String TAG = null;
  7.  
  8.     /**
  9.      * Function register untuk menyimpan gambar ke ftp dan mengirimkan data ke RMQ
  10.      * @param srcFilePath
  11.      * @param Imei
  12.      * @param nama
  13.      * @param txtTelpText
  14.      * @param kodeProv
  15.      * @param context
  16.      * @return
  17.      */
  18.  
  19.     /**
  20.      * Function Send Report dan menyimpan file gambar di FTP dan mengirim data ke RMQ
  21.      * @param srcFilePath
  22.      * @param comment
  23.      * @param context
  24.      * @param imei
  25.      * @return
  26.      */
  27.     public boolean ftpReport(String srcFilePath, Editable comment,Editable nilai2, Context context,String imei){
  28.         try {
  29.             String komentar = String.valueOf(comment);
  30.             String Nilai2 = String.valueOf(nilai2);
  31.             FTPClient ftpClient = new FTPClient();
  32.             ftpClient.connect(gb.getHostFTP());
  33.             Log.d("host11", "ftpReport: HOST "+gb.getHostFTP());
  34.             Log.d("user11", "ftpReport: user "+gb.getUserFTP());
  35.             Log.d("pw11", "ftpReport: Pw "+gb.getPwFTP());
  36.             if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
  37.                 boolean status1 = ftpClient.login(gb.getUserFTP(),gb.getPwFTP());
  38.                 ftpClient.enterLocalPassiveMode();
  39.                 Log.d("Connectionsuccess", "ftpConnect: berhasil status = "+status1);
  40.  
  41.                 FileInputStream srcFileStream = new FileInputStream(srcFilePath);
  42.                 BufferedInputStream bis = new BufferedInputStream(srcFileStream);
  43.  
  44.                 Bitmap bmp = BitmapFactory.decodeStream(bis);
  45.                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
  46.                 bmp.compress(Bitmap.CompressFormat.JPEG,10,stream);
  47.                 InputStream iss = new ByteArrayInputStream(stream.toByteArray());
  48.                 BufferedInputStream bii = new BufferedInputStream(iss);
  49.  
  50.                 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  51.                 String ts = tsLong.toString();
  52.  
  53.                 boolean  status = ftpClient.storeFile("Panwaslu/gambar/"+ts+".jpg",bii);
  54.                 rmq.setupConnectionFactory();
  55.                 ArrayList<String> list = new ArrayList<String>();
  56.                 list.add("Panwaslu/gambar/"+ts+".jpg");
  57.                 JSONObject obj = new JSONObject();
  58.                 obj.put("NILAI 1",komentar);
  59.                 obj.put("NILAI 2",Nilai2);
  60.                 obj.put("PATHIMAGE",list);
  61.  
  62.  
  63.                 Log.d("TAGJsonData", "ftpReport: "+obj.toString());
  64.                 rmq.setupConnectionFactory();
  65.                 rmq.publish(obj.toString());
  66.                 bis.close();
  67.  
  68.  
  69.                 return status;
  70.             }
  71.         } catch (SocketException e) {
  72.             Log.d("FTP1", "Error: could not connect to socket " + e );
  73.         } catch (IOException e) {
  74.             Log.d("FTP2", "Error: could not connect to host " + e);
  75.         } catch (NoSuchAlgorithmException e) {
  76.             e.printStackTrace();
  77.         } catch (URISyntaxException e) {
  78.             e.printStackTrace();
  79.         } catch (TimeoutException e) {
  80.             e.printStackTrace();
  81.         } catch (InterruptedException e) {
  82.             e.printStackTrace();
  83.         } catch (KeyManagementException e) {
  84.             e.printStackTrace();
  85.         } catch (JSONException e) {
  86.             e.printStackTrace();
  87.         }
  88.         return false;
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement