Guest User

Untitled

a guest
Dec 30th, 2010
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package programaAcessaSite;
  6.  
  7. import java.io.BufferedInputStream;
  8. import java.io.BufferedOutputStream;
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileOutputStream;
  14. import java.io.FileReader;
  15. import java.io.IOException;
  16. import java.net.URISyntaxException;
  17. import java.net.URL;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. /**
  22.  *
  23.  * @author Usuário
  24.  */
  25. public class ManipuladorLink {
  26.  
  27.     private String pastaOrigem = "file://184.82.88.45/dofus/lang/swf/";
  28.     private String pastaDestino = "C:/Documents and Settings/Usuário/Desktop/legalize lang/swf";
  29.  
  30.     public static void main(String[] args) {
  31.         try {
  32.             ManipuladorLink ml = new ManipuladorLink();
  33.             ml.baixaArquivos();
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.  
  38.     }
  39.  
  40.     public void baixaArquivos() throws FileNotFoundException, IOException, URISyntaxException {
  41.         List<URL> urls = getURLs();
  42.         int totalArquivos = urls.size();
  43.         int arquivoAtual = 0;
  44.         for (URL url : urls) {
  45.             File arquivoOriginal = new File(url.toURI());
  46.             FileInputStream in = new FileInputStream(arquivoOriginal);
  47.             BufferedInputStream bin = new BufferedInputStream(in);
  48.  
  49.             String destino = pastaDestino+getNomeArquivo(url);
  50.             destino = destino.replaceAll("\"", "");
  51.             FileOutputStream out = new FileOutputStream(destino);
  52.             BufferedOutputStream bout = new BufferedOutputStream(out);
  53.  
  54.             int data = 0;
  55.  
  56.             while((data = bin.read()) != -1){
  57.                 bout.write(data);
  58.                 System.out.println("Arquivos salvos: "+(++arquivoAtual)+" de "+totalArquivos);
  59.             }
  60.         }
  61.     }
  62.  
  63.     private String getNomeArquivo(URL url){
  64.         String path = url.getPath();
  65.         String nomeArquivo = path.substring(path.lastIndexOf("/")+1, path.length());
  66.         return nomeArquivo;
  67.     }
  68.  
  69.     public void imprimeURLs() throws FileNotFoundException, IOException, URISyntaxException {
  70.         List<URL> urls = getURLs();
  71.         for (URL url : urls) {
  72.             String path = url.getPath();
  73.             System.out.println(path.substring(path.lastIndexOf("/")+1, path.length()));
  74.         }
  75.         System.out.println("Total arquivos: " + urls.size());
  76.     }
  77.  
  78.     public List<URL> getURLs() throws FileNotFoundException, IOException, URISyntaxException {
  79.         List<URL> urls = new ArrayList<URL>();
  80.  
  81.         File codigoFonte = new File(getClass().getResource("CodigoFonteManipulaLink.html").toURI());
  82.         BufferedReader br = new BufferedReader(new FileReader(codigoFonte));
  83.  
  84.         String linha = null;
  85.         while ((linha = br.readLine()) != null) {
  86.             String nomeArquivo = getNomeArquivoSWF(linha);
  87.             if (nomeArquivo != null) {
  88.                 urls.add(new URL((pastaOrigem + nomeArquivo).replaceAll("\"", "")));
  89.             }
  90.         }
  91.  
  92.         return urls;
  93.     }
  94.  
  95.     public String getNomeArquivoSWF(String link) {
  96.         String nomeArquivo = null;
  97.  
  98.         if (link.contains(".swf")) {
  99.             int indexInicio = link.indexOf("href=");
  100.             int indexFim = link.indexOf("</a>");
  101.             nomeArquivo = link.substring(indexInicio, indexFim);
  102.             nomeArquivo = nomeArquivo.replaceAll("href=", "");
  103.             nomeArquivo = nomeArquivo.replaceAll("'", "");
  104.             nomeArquivo = nomeArquivo.split(">")[0];
  105.         }
  106.  
  107.         return nomeArquivo;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment