sabato 8 luglio 2017

Java - Scarica file da domini con protocollo https - Download files from domains with https protocol











Italiano
Ciao a tutti,
il download di file, reperiti da domini o siti internet che utilizzano il protocollo https da Java, prevede l'importazione del certificato ssl all'interno di un keystore noto all'utente, in altre parole al keystore utilizzato di solito oppure in un nuovo keystore, e di un semplice programmino che scarica un elemento dopo l'altro, niente di trascendentale, ma per chi non ha familirità con certificati ssl, potrebbe un attimo rallentare l'attività.

procedendo per passi, cercando di snelline al massimo i processi vedremo come:
1) scaricare il certificato in base 64, per questo utilizzeremo google chrome, tab security --> view certificate --> download base 64
2) import del certificato attraverso l'utility keytool

3) applicazione java ( un semplice main va benissimo) e setting del keystore da utilizzare...

Passo 1

sul sito che contiene i file da scaricare, premere F12,

navigare fino al tab security e premere il pulsante "View Certificate"... seguire le indicazioni nelle immagini imm1, 2.. 3




image 1




imm2

imm3



successivamente, salvare il certificato, esempio downcert.cer



Passo 2

l'import del certificato nel keystore è una operazione molto semplice, accertatevi di avere nel path configurata una jdk o jre.... quindi una una shell eseguire :


 keytool -importcert -file downcert.cer -keystore keystore.jks -alias "AliasName"


(si suppone di essere posizionati nella cartella che contiene il certificato quindi il keystore, sebbene non sia corretto ma per comodità....)

Lanciato il comando verrà chiesto se:

il certificato sia attendibile, scrivere si e
la password del keystore, che verrà utilizzata nel codice al passo 3




English 

Hello to all,
Downloading files from domains or websites that use https from Java suggests importing the ssl certificate within a known keystore, in other words to the keystore usually used or in a new keystore , And a simple program that downloads an item after another, nothing transcendental, but for those who do not have familiarity with ssl certificates, it may be a moment to slow down the activity.

By proceeding in steps, trying to streamline the processes at maximum we will see how:
1) download certificate based 64, for this we will use google chrome, security tab -> view certificate -> 64 download base
2) Import the certificate through the keytool utility

3) java application (a simple main goes fine) and keystore setting to use ...

Step 1

On the site that contains the files to download, press F12,

Navigate to the security tab and press the "View Certificate" button ... follow the directions in the images imm1, 2 .. 3
(see previous images)
Then save the certificate.

Step 2

Importing the certificate in the keystore is a very simple operation, make sure you have a jdk or jre configured path ... so a shell execute:

  keytool -importcert -file downcert.cer -keystore keystore.jks -alias "AliasName"

(It is supposed to be placed in the folder that contains the certificate then the keystore, although it is not correct but for convenience ....)

Launched the command will ask if:
The certificate is trusted, write yes and
The keystore password, which will be used in the code at step 3





PASSO 3 / STEP 3



import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

public class main {
public static String[] links=new String[]{
"https://www.domainexample.com/resource1.jpg",
"https://www.domainexample.com/resource2.jpg",
"",
"",
"https://www.domainexample.com/resourceN.jpg" };

public static void main(String[] args) throws MalformedURLException, IOException{
String  ks= System.getProperty("user.home") + "/Desktop/keystore.jks";
String  folder= System.getProperty("user.home") + "/Desktop/downloadfoldercontainer";

new File(folder).mkdirs();
System.setProperty("javax.net.ssl.trustStore", ks);
System.setProperty("javax.net.ssl.trustStorePassword", "passwordStep2Passo2");

System.out.println("start:"+System.currentTimeMillis());
for(int i=0;i<links.length;i++){
try(InputStream in = new URL(links[i]).openStream()){
   Files.copy(in, Paths.get(folder+"/photo"+i+"image.jpg"));
}
}
System.out.println("stop:"+System.currentTimeMillis());
}
}



ciaoooo ragazzi :)