Dilan1991

Untitled

May 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.38 KB | None | 0 0
  1. package id.pptik.panwasluvillage.features;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.os.Environment;
  9. import android.telephony.TelephonyManager;
  10. import android.text.Editable;
  11. import android.util.Log;
  12.  
  13.  
  14. import org.apache.commons.net.ftp.FTP;
  15. import org.apache.commons.net.ftp.FTPClient;
  16. import org.apache.commons.net.ftp.FTPReply;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import java.io.BufferedInputStream;
  21. import java.io.BufferedOutputStream;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.OutputStream;
  30. import java.net.SocketException;
  31. import java.net.URISyntaxException;
  32. import java.security.KeyManagementException;
  33. import java.security.NoSuchAlgorithmException;
  34. import java.util.UUID;
  35. import java.util.concurrent.TimeoutException;
  36.  
  37. import id.pptik.panwasluvillage.globalVariable.GlobalVariable;
  38. import id.pptik.panwasluvillage.R;
  39. import id.pptik.panwasluvillage.helpers.RMQ;
  40.  
  41. public class FileTransfer {
  42. GlobalVariable gb = new GlobalVariable();
  43. RMQ rmq = new RMQ();
  44. Long tsLong = System.currentTimeMillis()/1000;
  45.  
  46. private static final String TAG = null;
  47.  
  48. /**
  49. * Function register untuk menyimpan gambar ke ftp dan mengirimkan data ke RMQ
  50. * @param srcFilePath
  51. * @param Imei
  52. * @param nama
  53. * @param txtTelpText
  54. * @param kodeProv
  55. * @param context
  56. * @return
  57. */
  58. public boolean ftpConnect(String srcFilePath, String Imei, Editable nama, Editable txtTelpText,Editable txtTPS, String kodeProv,String kodeKab,String kodeKec,String kodeKel,Context context){
  59. try {
  60.  
  61. File root = new File(String.valueOf(Environment.getExternalStorageDirectory())+"/tmpPanwaslu/Kelurahan");
  62. try {
  63. if (root.mkdir()){
  64. Log.d("FolderDone", "Succes Create Folder: "+root);
  65. }else{
  66. Log.d("FolderDone", "Succes exist Folder: "+root);
  67. }
  68. }catch (Exception e){
  69. Log.d("Arrgghghg", "ftpConnect: "+e);
  70. }
  71.  
  72.  
  73. String names = String.valueOf(nama);
  74. String telp = String.valueOf(txtTelpText);
  75. String tps = String.valueOf(txtTPS);
  76. String mBitmap =null;
  77. FTPClient ftpClient = new FTPClient();
  78. ftpClient.connect(gb.getHostFTP());
  79. if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
  80. boolean status1 = ftpClient.login(gb.getUserFTP(),gb.getPwFTP());
  81. ftpClient.enterLocalPassiveMode();
  82. Log.d("Connection success", "ftpConnect: berhasil status = "+status1);
  83.  
  84. FileInputStream srcFileStream = new FileInputStream(srcFilePath);
  85. BufferedInputStream bis = new BufferedInputStream(srcFileStream);
  86. ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  87. String ts = tsLong.toString();
  88.  
  89. Bitmap bmp = BitmapFactory.decodeStream(bis);
  90. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  91. bmp.compress(Bitmap.CompressFormat.JPEG,10,stream);
  92. InputStream iss = new ByteArrayInputStream(stream.toByteArray());
  93. BufferedInputStream bii = new BufferedInputStream(iss);
  94.  
  95. TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
  96. @SuppressLint("MissingPermission")
  97.  
  98.  
  99. boolean status = ftpClient.storeFile("Panwaslu/gambar/"+kodeProv+"/"+kodeKab+"/"+kodeKec+"/"+kodeKel+"/KTP-"+ts+"_"+Imei+".jpg", bii);
  100. SharedPreferences pref =context.getSharedPreferences("myToken", 0); // 0 - for private mode
  101. SharedPreferences.Editor editor = pref.edit();
  102.  
  103. String randomUUID = UUID.randomUUID().toString();
  104. editor.putString(String.valueOf(R.string.pref_nama),names);
  105. editor.putString(String.valueOf(R.string.pref_tlp),telp);
  106. editor.putString(String.valueOf(R.string.pref_prov),kodeProv);
  107. editor.putString(String.valueOf(R.string.pref_guid),randomUUID);
  108. editor.putString(String.valueOf(R.string.prev_kab),kodeKab);
  109. editor.putString(String.valueOf(R.string.prev_kec),kodeKec);
  110. editor.putString(String.valueOf(R.string.prev_kel),kodeKel);
  111. editor.commit();
  112.  
  113. rmq.setupConnectionFactory();
  114. JSONObject obj = new JSONObject();
  115. JSONObject gps = new JSONObject();
  116. gps.put("LONG","90001928219");
  117. gps.put("LAT","-8882717229");
  118.  
  119. obj.put("MSG_TYPE",0);
  120. obj.put("CMD_TYPE",0);
  121. obj.put("TIMESTAMP",ts);
  122. obj.put("IMEI1",Imei);
  123. obj.put("IMEI2","");
  124. obj.put("TLP1","");
  125. obj.put("TLP2","");
  126. obj.put("GPS",gps);
  127. obj.put("NAMA",names);
  128. obj.put("TLP",telp);
  129. obj.put("PROVINSI",kodeProv);
  130. obj.put("KABUPATEN",kodeKab);
  131. obj.put("KECAMATAN",kodeKec);
  132. obj.put("KELURAHAN",kodeKel);
  133. obj.put("TPS",tps);
  134. obj.put("KTP","Panwaslu/gambar/"+kodeProv+"/"+kodeKab+"/"+kodeKec+"/"+kodeKel+"/KTP-"+pref.getString(String.valueOf(R.string.pref_guid),null)+".jpg");
  135. obj.put("GUID",pref.getString(String.valueOf(R.string.pref_guid),null));
  136. Log.d("DataPobjeck", "ftpConnect: "+obj.toString());
  137. rmq.publish(obj.toString());
  138.  
  139. bis.close();
  140.  
  141.  
  142. return status;
  143. }
  144. } catch (SocketException e) {
  145. Log.d("FTP1", "Error: could not connect to socket " + e );
  146. } catch (IOException e) {
  147. Log.d("FTP2", "Error: could not connect to host " + e );
  148. } catch (JSONException e) {
  149. e.printStackTrace();
  150. Log.d("FTP3", "Error: could not connect to host " + e );
  151. } catch (NoSuchAlgorithmException e) {
  152. e.printStackTrace();
  153. } catch (URISyntaxException e) {
  154. e.printStackTrace();
  155. } catch (TimeoutException e) {
  156. e.printStackTrace();
  157. } catch (InterruptedException e) {
  158. e.printStackTrace();
  159. } catch (KeyManagementException e) {
  160. e.printStackTrace();
  161. }
  162. return false;
  163. }
  164.  
  165. /**
  166. * Function Send Report dan menyimpan file gambar di FTP dan mengirim data ke RMQ
  167. * @param srcFilePath
  168. * @param comment
  169. * @param context
  170. * @param imei
  171. * @return
  172. */
  173. public boolean ftpReport(String srcFilePath, Editable comment, Context context,String imei){
  174. try {
  175. SharedPreferences pref = context.getSharedPreferences("myToken",0);
  176. String guid = pref.getString(String.valueOf(R.string.pref_guid),null);
  177. String tlp = pref.getString(String.valueOf(R.string.pref_tlp),null);
  178. String prov = pref.getString(String.valueOf(R.string.pref_prov),null);
  179. String kab =pref.getString(String.valueOf(R.string.prev_kab),null);
  180. String kec = pref.getString(String.valueOf(R.string.prev_kec),null);
  181. String kel = pref.getString(String.valueOf(R.string.prev_kel),null);
  182. String komentar = String.valueOf(comment);
  183. FTPClient ftpClient = new FTPClient();
  184. ftpClient.connect(gb.getHostFTP());
  185. Log.d("host11", "ftpReport: HOST "+gb.getHostFTP());
  186. Log.d("user11", "ftpReport: user "+gb.getUserFTP());
  187. Log.d("pw11", "ftpReport: Pw "+gb.getPwFTP());
  188. if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
  189. boolean status1 = ftpClient.login(gb.getUserFTP(),gb.getPwFTP());
  190. ftpClient.enterLocalPassiveMode();
  191. Log.d("Connectionsuccess", "ftpConnect: berhasil status = "+status1);
  192.  
  193. FileInputStream srcFileStream = new FileInputStream(srcFilePath);
  194. BufferedInputStream bis = new BufferedInputStream(srcFileStream);
  195.  
  196. Bitmap bmp = BitmapFactory.decodeStream(bis);
  197. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  198. bmp.compress(Bitmap.CompressFormat.JPEG,10,stream);
  199. InputStream iss = new ByteArrayInputStream(stream.toByteArray());
  200. BufferedInputStream bii = new BufferedInputStream(iss);
  201.  
  202. ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
  203. String ts = tsLong.toString();
  204.  
  205. boolean status = ftpClient.storeFile("Panwaslu/gambar/"+prov+"/"+kab+"/"+kec+"/"+kel+"/"+ts+"-"+guid+".jpg",bii);
  206. rmq.setupConnectionFactory();
  207. JSONObject obj = new JSONObject();
  208. JSONObject gps = new JSONObject();
  209. gps.put("LONG","XXXXXXXXXXX");
  210. gps.put("LAT","XXXXXXXXXX");
  211.  
  212. obj.put("MSG_TYPE",1);
  213. obj.put("CMD_TYPE",0);
  214. obj.put("TIMESTAMP",ts);
  215. obj.put("IMEI1",imei);
  216. obj.put("IMEI2","");
  217. obj.put("TLP",tlp);
  218. obj.put("TLP1","");
  219. obj.put("TLP2","");
  220. obj.put("GPS",gps);
  221. obj.put("KOMENTAR",komentar);
  222. obj.put("FILENAME","Panwaslu/gambar/"+prov+"/"+kab+"/"+kec+"/"+kel+"/"+ts+"-"+guid+".jpg");
  223. obj.put("PROVINSI",prov);
  224. obj.put("KABUPATEN",kab);
  225. obj.put("KECAMATAN",kec);
  226. obj.put("KELURAHAN",kel);
  227.  
  228. obj.put("GUID",guid);
  229. Log.d("TAGJsonData", "ftpReport: "+obj.toString());
  230. rmq.publish(obj.toString());
  231. bis.close();
  232.  
  233.  
  234. return status;
  235. }
  236. } catch (SocketException e) {
  237. Log.d("FTP1", "Error: could not connect to socket " + e );
  238. } catch (IOException e) {
  239. Log.d("FTP2", "Error: could not connect to host " + e );
  240. } catch (JSONException e) {
  241. e.printStackTrace();
  242. Log.d("FTP3", "Error: could not connect to host " + e );
  243. } catch (NoSuchAlgorithmException e) {
  244. e.printStackTrace();
  245. } catch (URISyntaxException e) {
  246. e.printStackTrace();
  247. } catch (TimeoutException e) {
  248. e.printStackTrace();
  249. } catch (InterruptedException e) {
  250. e.printStackTrace();
  251. } catch (KeyManagementException e) {
  252. e.printStackTrace();
  253. }
  254. return false;
  255. }
  256.  
  257. public boolean downloadAndSaveFile(String server, int portNumber,
  258. String user, String password, String filename, File localFile)
  259. throws IOException {
  260. FTPClient ftp = null;
  261.  
  262. try {
  263. ftp = new FTPClient();
  264. try {
  265. ftp.connect(server, portNumber);
  266. Log.d("kasih_ibu", "Connected. Reply: " + ftp.getReplyString());
  267.  
  268. ftp.login(user, password);
  269. Log.d("kasih_ibu", "Logged in");
  270. ftp.setFileType(FTP.BINARY_FILE_TYPE);
  271. Log.d("kasih_ibu", "Downloading");
  272. ftp.enterLocalPassiveMode();
  273. }
  274. catch (Exception ex) {
  275. Log.d("ibu_marah", "downloadAndSaveFile: "+ex.getMessage());
  276. }
  277.  
  278. OutputStream outputStream = null;
  279. boolean success = false;
  280. try {
  281. outputStream = new BufferedOutputStream(new FileOutputStream(
  282. localFile));
  283. success = ftp.retrieveFile(filename, outputStream);
  284. } finally {
  285. if (outputStream != null) {
  286. outputStream.close();
  287. }
  288. }
  289.  
  290. return success;
  291. } finally {
  292. if (ftp != null) {
  293. ftp.logout();
  294. ftp.disconnect();
  295. }
  296. }
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment