1+ package com .convertapi .examples ;
2+
3+ import java .io .IOException ;
4+ import java .nio .file .Path ;
5+ import java .nio .file .Paths ;
6+ import java .util .concurrent .CompletableFuture ;
7+ import java .util .concurrent .ExecutionException ;
8+ import java .util .concurrent .TimeUnit ;
9+
10+ import com .convertapi .Config ;
11+ import com .convertapi .ConversionResult ;
12+ import com .convertapi .ConvertApi ;
13+ import com .convertapi .Param ;
14+
15+ /**
16+ * Example of HTTP client setup to use HTTP proxy server.
17+ */
18+
19+ public class Advanced {
20+ public static void main (String [] args ) throws IOException , ExecutionException , InterruptedException {
21+ Config .setDefaultSecret ("YOUR API SECRET" ); //Get your secret at https://www.convertapi.com/a
22+
23+ // Advanced HTTP client setup
24+ Config .setDefaultHttpBuilder (builder -> {
25+ return builder
26+ .proxy (new Proxy (Proxy .Type .HTTP , new InetSocketAddress ("10.0.0.1" , 8888 ))) // Setting Proxy server
27+ .connectTimeout (3 , TimeUnit .SECONDS ); // Setting connect timeout
28+ // More settings can be tuned here
29+ });
30+
31+ // Conversion
32+ Param fileParam = new Param ("file" , "https://cdn.convertapi.com/cara/testfiles/presentation.pptx" );
33+ System .out .println ("Converting remote PPTX to PDF" );
34+ CompletableFuture <ConversionResult > result = ConvertApi .convert ("pptx" , "pdf" , new Param []{fileParam });
35+ Path pdfFile = Paths .get (System .getProperty ("java.io.tmpdir" ) + "/myfile.pdf" );
36+ result .get ().saveFile (pdfFile ).get ();
37+
38+ // Leaving no files on convertapi.com server
39+ System .out .println ("Deleting source file from convertapi.com server" );
40+ fileParam .delete ().get ();
41+ System .out .println ("Deleting result files from convertapi.com server" );
42+ result .get ().deleteSync ();
43+
44+ System .out .println ("PDF file saved to: " + pdfFile .toString ());
45+ }
46+ }
0 commit comments