Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package models;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Serializable;
- import java.util.Base64;
- import java.util.zip.GZIPInputStream;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import sun.net.ftp.impl.FtpClient;
- public class Decode implements Serializable{
- private static final long serialVersionUID = -5577579081118070434L;
- private String hash;
- public Decode(String hash) {
- this.hash = hash;
- }
- public void decode(String strHash) throws IOException {
- //Receber String base64
- String base64string = strHash;
- System.out.println("Base64 Encoded String (Basic) :" + base64string);
- // Decriptografia String base 64
- byte[] base64decodedBytes = Base64.getDecoder().decode(base64string);
- //Arquivo de Saída .gz
- // FileOutputStream arquivo_gz = new FileOutputStream(new File("C:/Users/suporte/Desktop/FTPserver/arquivo.gz"));
- File file_gz = new File("C:/Users/suporte/Desktop/FTPserver/arquivo.gz");
- int count = 0;
- if(file_gz.exists()) {
- count++;
- file_gz = new File("C:/Users/suporte/Desktop/FTPserver/arquivo"+count+".gz");
- file_gz.createNewFile();
- }
- FileOutputStream fileOutputStream = new FileOutputStream(file_gz);
- fileOutputStream.write(base64decodedBytes);
- fileOutputStream.close();
- Unzip(count);
- String caminhoArqXml = "C:/Users/suporte/Desktop/FTPserver/receita.xml";
- sendFTP(caminhoArqXml,count);
- }
- public void Unzip(int count) {
- String arquivo_gzip = "";
- if(count==0) {
- arquivo_gzip = "C:/Users/suporte/Desktop/FTPserver/arquivo.gz";
- }else {
- arquivo_gzip = "C:/Users/suporte/Desktop/FTPserver/arquivo"+count+".gz";
- }
- // String arquivo_xml = "C:/Users/suporte/Desktop/FTPserver/receita.xml";
- byte[] buffer = new byte[1024];
- try {
- FileInputStream arquivo_entrada = new FileInputStream(arquivo_gzip);
- // cria arquivo zip
- GZIPInputStream gZIPInputStream = new GZIPInputStream(arquivo_entrada);
- File file_xml = new File("C:/Users/suporte/Desktop/FTPserver/receita.xml");
- if(file_xml.exists()) {
- file_xml = new File("C:/Users/suporte/Desktop/FTPserver/receita"+count+".xml");
- file_xml.createNewFile();
- }
- FileOutputStream fileOutputStream = new FileOutputStream(file_xml);
- int bytes_read = 0;
- while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {
- fileOutputStream.write(buffer, 0, bytes_read);
- System.out.println("Arquivo descompactado para xml com sucesso");
- }
- gZIPInputStream.close();
- fileOutputStream.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- // public void uploadFile(String localFileFullName, String fileName, String hostDir)
- // throws Exception {
- // try(InputStream input = new FileInputStream(new File(localFileFullName))){
- // this.ftp.storeFile(hostDir + fileName, input);
- // }
- // }
- public File sendFTP(String xmlArq,int count) {
- String server = "10.61.20.130";
- int port = 21;
- String user = "vendaweb";
- String pass = "@ftp123#";
- FTPClient ftpClient = new FTPClient();
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- ftpClient.enterLocalPassiveMode();
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- // APPROACH #2: uploads second file using an OutputStream
- String arquivo_xml = "";
- if(count==0) {
- arquivo_xml = "C:\\Users\\suporte\\Desktop\\FTPserver\\receita.xml";
- }else {
- arquivo_xml = "C:\\Users\\suporte\\Desktop\\FTPserver\\receita"+count+".xml";
- }
- File secondLocalFile = new File(arquivo_xml);
- String secondRemoteFile = "ftp://10.61.20.130/";
- // InputStream inputStream = new FileInputStream(secondLocalFile);
- System.out.println("fazendo o upload do arquivo");
- // OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
- // byte[] bytesIn = new byte[1024];
- // int read = 0;
- try(InputStream input = new FileInputStream(new File(arquivo_xml))){
- if(count==0) {
- ftpClient.storeFile(secondRemoteFile + "receita.xml", input);
- }else {
- ftpClient.storeFile(secondRemoteFile + "receita"+count+".xml", input);
- }
- }
- //testar metodo de gilberto
- // while ((read = input.read(bytesIn)) != -1) {
- // outputStream.write(bytesIn, 0, read);
- // }
- // input.close();
- // outputStream.close();
- boolean completed = ftpClient.completePendingCommand();
- if (completed) {
- System.out.println("Realizado o upload do arquivo.");
- }
- } catch (Exception ex) {
- System.out.println("Error: " + ex.getMessage());
- ex.printStackTrace();
- } finally {
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- return null;
- }
- public String getHash() {
- return hash;
- }
- public void setHash(String hash) {
- this.hash = hash;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement