Advertisement
Guest User

metodoFTP

a guest
Aug 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. package models;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.Serializable;
  11. import java.util.Base64;
  12. import java.util.zip.GZIPInputStream;
  13.  
  14. import org.apache.commons.net.ftp.FTP;
  15. import org.apache.commons.net.ftp.FTPClient;
  16.  
  17. import sun.net.ftp.impl.FtpClient;
  18.  
  19.  
  20.  
  21.  
  22.  
  23. public class Decode implements Serializable{
  24.  
  25.     private static final long serialVersionUID = -5577579081118070434L;
  26.    
  27.     private String hash;
  28.    
  29.     public Decode(String hash) {
  30.         this.hash = hash;
  31.        
  32.     }
  33.    
  34.     public void decode(String strHash) throws IOException {
  35.        
  36.           //Receber String base64
  37.         String base64string = strHash;
  38.         System.out.println("Base64 Encoded String (Basic) :" + base64string);
  39.        
  40.         // Decriptografia String base 64
  41.         byte[] base64decodedBytes = Base64.getDecoder().decode(base64string);
  42.        
  43.        
  44.         //Arquivo de Saída .gz
  45.        
  46.       //  FileOutputStream arquivo_gz = new FileOutputStream(new File("C:/Users/suporte/Desktop/FTPserver/arquivo.gz"));
  47.         File file_gz = new File("C:/Users/suporte/Desktop/FTPserver/arquivo.gz");
  48.         int count = 0;
  49.         if(file_gz.exists()) {
  50.             count++;
  51.             file_gz = new File("C:/Users/suporte/Desktop/FTPserver/arquivo"+count+".gz");
  52.             file_gz.createNewFile();
  53.         }
  54.         FileOutputStream fileOutputStream = new FileOutputStream(file_gz);    
  55.          
  56.        
  57.         fileOutputStream.write(base64decodedBytes);
  58.         fileOutputStream.close();    
  59.         Unzip(count);
  60.        
  61.         String caminhoArqXml = "C:/Users/suporte/Desktop/FTPserver/receita.xml";
  62.         sendFTP(caminhoArqXml,count);
  63.        
  64.        
  65.     }  
  66.     public void Unzip(int count) {
  67.         String arquivo_gzip = "";
  68.         if(count==0) {
  69.              arquivo_gzip = "C:/Users/suporte/Desktop/FTPserver/arquivo.gz";
  70.         }else {
  71.              arquivo_gzip = "C:/Users/suporte/Desktop/FTPserver/arquivo"+count+".gz";
  72.         }
  73.         // String arquivo_xml = "C:/Users/suporte/Desktop/FTPserver/receita.xml";
  74.            
  75.         byte[] buffer = new byte[1024];
  76.        
  77.         try {
  78.             FileInputStream arquivo_entrada = new FileInputStream(arquivo_gzip);
  79.             // cria arquivo zip
  80.             GZIPInputStream gZIPInputStream = new GZIPInputStream(arquivo_entrada);
  81.  
  82.             File file_xml = new File("C:/Users/suporte/Desktop/FTPserver/receita.xml");
  83.            
  84.             if(file_xml.exists()) {
  85.                 file_xml = new File("C:/Users/suporte/Desktop/FTPserver/receita"+count+".xml");
  86.                 file_xml.createNewFile();
  87.             }
  88.             FileOutputStream fileOutputStream = new FileOutputStream(file_xml);
  89.  
  90.              int bytes_read = 0;
  91.                
  92.              while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {
  93.                         fileOutputStream.write(buffer, 0, bytes_read);
  94.                         System.out.println("Arquivo descompactado para xml com sucesso");
  95.              }
  96.              
  97.              gZIPInputStream.close();
  98.              fileOutputStream.close();
  99.            
  100.            
  101.            
  102.         } catch (Exception ex) {
  103.             ex.printStackTrace();
  104.         }
  105.  
  106.     }
  107. //  public void uploadFile(String localFileFullName, String fileName, String hostDir)
  108. //            throws Exception {
  109. //        try(InputStream input = new FileInputStream(new File(localFileFullName))){
  110. //        this.ftp.storeFile(hostDir + fileName, input);
  111. //        }
  112. //    }
  113.    
  114.     public File sendFTP(String xmlArq,int count) {
  115.        
  116.          String server = "10.61.20.130";
  117.             int port = 21;
  118.             String user = "vendaweb";
  119.             String pass = "@ftp123#";
  120.      
  121.             FTPClient ftpClient = new FTPClient();
  122.             try {
  123.      
  124.                 ftpClient.connect(server, port);
  125.                 ftpClient.login(user, pass);
  126.                 ftpClient.enterLocalPassiveMode();
  127.      
  128.                 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  129.      
  130.                 // APPROACH #2: uploads second file using an OutputStream
  131.                
  132.                 String arquivo_xml = "";
  133.                 if(count==0) {
  134.                      arquivo_xml = "C:\\Users\\suporte\\Desktop\\FTPserver\\receita.xml";
  135.                 }else {
  136.                      arquivo_xml = "C:\\Users\\suporte\\Desktop\\FTPserver\\receita"+count+".xml";
  137.                 }
  138.                
  139.                 File secondLocalFile = new File(arquivo_xml);
  140.                 String secondRemoteFile = "ftp://10.61.20.130/";
  141. //              InputStream inputStream = new FileInputStream(secondLocalFile);
  142.      
  143.                 System.out.println("fazendo o upload do arquivo");
  144. //              OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
  145. //              byte[] bytesIn = new byte[1024];
  146. //              int read = 0;
  147.                
  148.                     try(InputStream input = new FileInputStream(new File(arquivo_xml))){
  149.                     if(count==0) {
  150.                        
  151.                         ftpClient.storeFile(secondRemoteFile + "receita.xml", input);
  152.                     }else {
  153.                     ftpClient.storeFile(secondRemoteFile + "receita"+count+".xml", input);
  154.                     }
  155.                 }
  156.                 //testar metodo de gilberto
  157. //              while ((read = input.read(bytesIn)) != -1) {
  158. //                  outputStream.write(bytesIn, 0, read);
  159. //              }
  160. //              input.close();
  161. //              outputStream.close();
  162.      
  163.                 boolean completed = ftpClient.completePendingCommand();
  164.                 if (completed) {
  165.                     System.out.println("Realizado o upload do arquivo.");
  166.                 }
  167.      
  168.             } catch (Exception ex) {
  169.                 System.out.println("Error: " + ex.getMessage());
  170.                 ex.printStackTrace();
  171.             } finally {
  172.                 try {
  173.                     if (ftpClient.isConnected()) {
  174.                         ftpClient.logout();
  175.                         ftpClient.disconnect();
  176.                     }
  177.                 } catch (Exception ex) {
  178.                     ex.printStackTrace();
  179.                 }
  180.             }
  181.        
  182.        
  183.         return null;
  184.        
  185.        
  186.     }
  187.  
  188.     public String getHash() {
  189.         return hash;
  190.     }
  191.  
  192.     public void setHash(String hash) {
  193.         this.hash = hash;
  194.     }
  195.    
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement