Έτσι έχω το πακέτο net που περιλαμβάνει τις Κλάσεις:
Downloader.java
- Κώδικας: Επιλογή όλων
package net;
import net.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.*;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
public class Downloader implements Runnable
{
private class ProgressListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
// e.getSource() gives you the object of DownloadCountingOutputStream
// because you set it in the overriden method, afterWrite().
if(total>0)
{
percent=100*((DownloadCountingOutputStream) e.getSource()).getByteCount()/(float)total;
}
}
}
private class SSLManager implements X509TrustManager
{
public X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
{
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
{
}
}
/**
*Shows the total size of file
*/
private long total=0;
/**
*Displays the total percent of file
*/
private float percent=0;
/**
*The remote url of file
*/
private URL dl;
/**
*The file that we will store
*/
private File fl;
/**
*Stores the status
*/
private String status="Waiting";
/**
*Stores the mime type
*/
private String mime="";
/**
*Tells if it is a sslk connection or not
*/
private boolean ssl=false;
/**
*Variable that configures the connection
*/
private URLConnection conn=null;
public Downloader(String url, String path)
{
this(url,path,null,(long)0);
}
public Downloader(String url, String path,String[] allowedType,long allowedSize)
{
boolean ok=true;
//Needed for ssl
try
{
if(!url.startsWith("http://"))
{
url="http://"+url;
}
else if(url.startsWith("https://"))
{
ssl=true;
}
dl=new URL(url);
fl=new File(path);
conn=dl.openConnection();
if(conn!=null)
{
mime=conn.getContentType();
for(int i=0;allowedType!=null && mime!=null && i<allowedType.length;i++)
{
if(mime.equals(allowedType[i]))
{
System.out.println("Mime OK");
ok=true;
break;
}
else
{
System.out.println("Mime Not OK");
ok=false;
}
}
String s=conn.getHeaderField("Content-Length");
if(s!=null)
{
total=Long.parseLong(s);
if(ok)
{
if(allowedSize>0 && total<allowedSize)
{
System.out.println("OK");
ok=true;
}
else if(allowedSize<=0)
{
System.out.println("No Limit");
ok=true;
}
else
{
System.out.println("Not OK");
ok=false;
}
}
}
}
if(ok)
{
Thread t=new Thread(this);
t.start();
}
else
{
status="Error";
}
}
catch(UnknownHostException u)
{
System.err.println("Uknown Host");
u.printStackTrace();
}
catch(Exception m)
{
m.printStackTrace();
}
}
public void run()
{
OutputStream os = null;
InputStream is = null;
ProgressListener progressListener = new ProgressListener();
try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new SSLManager()};
// Install the all-trusting trust manager
final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier()
{
public boolean verify(String hostname, SSLSession session)
{
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
os = new FileOutputStream(fl);
is = conn.getInputStream();
DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os);
dcount.setListener(progressListener);
status="Downloading";
// begin transfer by writing to dcount, not os.
IOUtils.copy(is, dcount);
}
catch(UnknownHostException u)
{
System.err.println("Uknown Host2");
u.printStackTrace();
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
try
{
status="Finished";
if (os != null)
{
os.close();
}
if (is != null)
{
is.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
public float getPercent()
{
return percent;
}
public String getStatus()
{
return status;
}
public long getSize()
{
return total;
}
public String getMime()
{
return mime;
}
}
Και την DownloadCountingOutputStream
- Κώδικας: Επιλογή όλων
package net;
import java.awt.event.ActionListener;
import org.apache.commons.io.output.CountingOutputStream;
import java.io.*;
import java.awt.event.ActionEvent;
public class DownloadCountingOutputStream extends CountingOutputStream {
private ActionListener listener = null;
public DownloadCountingOutputStream(OutputStream out) {
super(out);
}
public void setListener(ActionListener listener) {
this.listener = listener;
}
@Override
protected void afterWrite(int n) throws IOException {
super.afterWrite(n);
if (listener != null) {
listener.actionPerformed(new ActionEvent(this, 0, null));
}
}
}
Καθώς και την Κλάση Main για τεστς:
- Κώδικας: Επιλογή όλων
import net.Downloader;
public class Main
{
public static void main(String[] args)
{
if(args.length>0)
{
try
{
String[] allowed={"text/plain","text/xml","application/xml","application/octet-stream"};
Downloader d = new Downloader(args[0],args[1],allowed,5*(long)Math.pow(2,10));
float percent=0;
System.out.println("Mime: "+d.getMime());
System.out.println("Filesize: "+d.getSize());
if(!d.getStatus().equals("Error"))
{
while(!d.getStatus().equals("Finished"))
{
percent=d.getPercent();
System.out.println("Κατέβηκαν: "+percent+"%");
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
else
{
System.err.println("Error occured durong download");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Compile help
Θέλει την βιβλιοθήκη [url="http://commons.apache.org/io/download_io.cgi"]Apache commons IO[/url]
Έγω στο ίδιο φάκελο έχω το commons-io-2.4.jar,έναν φάκελο με όνομα net που περιέχει το αρχείο DownloadCountingOutputStream.java (για την DownloadCountingOutputStream Κλάση) και το Downloader.java για την κλάση Downloader, και έζω από το net το αρχείο Main.java που περιέχει την κλάση Main.
Το πακέτο net το κάνω compile με:
- Κώδικας: Επιλογή όλων
javac -cp commons-io-2.4.jar:. ./net/*.java
Και την main με:
- Κώδικας: Επιλογή όλων
javac Main.java
Αυτό που κάνει η Main είναι να κατεβάζει αρχεία .guma (Για την εγαρμογή GUMA
μως όταν πάω να του βάλω δυνατότητα να υποστηρίζει SSL παίρνω:
- Κώδικας: Επιλογή όλων
java -cp commons-io-2.4.jar:. Main https://raw.github.com/facebook/hadoop-20/master/singleNodeHadoop/mapredConf/ssl-server.xml.example sample.xml
Mime: null
Filesize: 0
Κατέβηκαν: 0.0%
Uknown Host2
java.net.UnknownHostException: https
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at net.Downloader.run(Downloader.java:209)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.net.UnknownHostException: https
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:203)
at sun.net.www.http.HttpClient.New(HttpClient.java:290)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:995)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:849)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1299)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2677)
at java.net.URLConnection.getContentType(URLConnection.java:522)
at net.Downloader.<init>(Downloader.java:116)
at Main.main(Main.java:12)
Γιατί όμως; Αυτό δεν μπορώ να καταλάβω...
Ενώ χωρίς ssl μπορώ να κατεβάζω τα αρχεία:
- Κώδικας: Επιλογή όλων
pcmagas@pcmagas-destop:~/Kwdikas/java/Libs/commons-io-2.4$ java -cp commons-io-2.4.jar:. Main http://ubuntuone.com/5hHq6apAbIeekIcAtUaiT7 g.guma
Mime Not OK
Mime Not OK
Mime Not OK
Mime OK
OK
Mime: application/octet-stream
Filesize: 1665
Κατέβηκαν: 0.0%


