Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.josue.lottery.eap.service.core; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.Iterator; import java.util.logging.Level; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import javax.ejb.Asynchronous; import javax.ejb.Stateless; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.cookie.BasicClientCookie; import org.apache.log4j.Logger; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import com.josue.lottery.eap.service.core.i.ILotoImporterLocal; /** * * @author Josue */ @Stateless @Deprecated public class LotoImporter implements ILotoImporterLocal { private static Logger logger = Logger.getLogger(LotoImporter.class); @Override @Asynchronous public void importFile() { try { String urlString = "http://www1.caixa.gov.br/loterias/_arquivos/loterias/D_lotfac.zip"; DefaultHttpClient client = new DefaultHttpClient(); CookieStore cookieStore = client.getCookieStore(); BasicClientCookie cookie = new BasicClientCookie("abc", "123"); cookie.setDomain("xyz.net"); cookie.setPath("/"); cookieStore.addCookie(cookie); client.setCookieStore(cookieStore); HttpGet request = new HttpGet(urlString); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); storeFile(entity.getContent()); } System.out.println("### FINISHED ###"); } catch (IOException ex) { logger.error(ex); } } private void storeFile(InputStream fileStream) { File dataDir = new File(System.getProperty("jboss.server.data.dir")); File dest = new File(dataDir, "filename.zip"); if (dest.exists()) { throw new RuntimeException( String.format("Unable to store file under id '%s'. File already exists.", "123")); } OutputStream oStr = null; try { oStr = new FileOutputStream(dest); IOUtils.copy(fileStream, oStr); readZip(dest); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(oStr); } } private void readZip(File file) { ZipFile zipFile = null; try { logger.info("unzipping ************"); zipFile = new ZipFile(file); File dataDir = new File(System.getProperty("jboss.server.data.dir")); File dest = new File(dataDir, "deziped.htm"); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().endsWith(".HTM")) { InputStream stream = zipFile.getInputStream(entry); OutputStream oStr = null; try { oStr = new FileOutputStream(dest); IOUtils.copy(stream, oStr); parseHtml(dest); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(oStr); } } } } catch (IOException ex) { logger.error(ex); } finally { try { zipFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void parseHtml(File file) { // String html = "<html><head><title>First parse</title></head>" // + "<body><p>Parsed HTML into a doc.</p>" // + // " <table><tr><td>satu</td><td>satu-1</td></tr><tr><td>dua</td><td>dua-1</td></tr><tr><td>tiga</td><td>tiga-1</td></tr></table> " // + "</body></html>"; StringBuilder sb = new StringBuilder(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException ex) { java.util.logging.Logger.getLogger(LotoImporter.class.getName()).log(Level.SEVERE, null, ex); } String line; try { while ((line = br.readLine()) != null) { sb.append(line); } } catch (IOException ex) { java.util.logging.Logger.getLogger(LotoImporter.class.getName()).log(Level.SEVERE, null, ex); } Document doc = Jsoup.parse(sb.toString()); Element table = doc.select("table").first(); Iterator<Element> iterator = table.select("td").iterator(); while (iterator.hasNext()) { logger.info("text : " + iterator.next().text()); } String title = doc.title(); System.out.println("Document title : " + title); } }