List of usage examples for java.net URLConnection guessContentTypeFromStream
public static String guessContentTypeFromStream(InputStream is) throws IOException
From source file:Main.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.google.com"); URLConnection httpCon = (URLConnection) url.openConnection(); System.out.println(URLConnection.guessContentTypeFromStream(httpCon.getInputStream())); }
From source file:WebCrawler.java
public void run() { String strURL = "http://www.google.com"; String strTargetType = "text/html"; int numberSearched = 0; int numberFound = 0; if (strURL.length() == 0) { System.out.println("ERROR: must enter a starting URL"); return;//from www .j av a2s.c o m } vectorToSearch = new Vector(); vectorSearched = new Vector(); vectorMatches = new Vector(); vectorToSearch.addElement(strURL); while ((vectorToSearch.size() > 0) && (Thread.currentThread() == searchThread)) { strURL = (String) vectorToSearch.elementAt(0); System.out.println("searching " + strURL); URL url = null; try { url = new URL(strURL); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } vectorToSearch.removeElementAt(0); vectorSearched.addElement(strURL); try { URLConnection urlConnection = url.openConnection(); urlConnection.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = urlConnection.guessContentTypeFromStream(urlStream); if (type == null) break; if (type.compareTo("text/html") != 0) break; byte b[] = new byte[5000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead); while (numRead != -1) { if (Thread.currentThread() != searchThread) break; numRead = urlStream.read(b); if (numRead != -1) { String newContent = new String(b, 0, numRead); content += newContent; } } urlStream.close(); if (Thread.currentThread() != searchThread) break; String lowerCaseContent = content.toLowerCase(); int index = 0; while ((index = lowerCaseContent.indexOf("<a", index)) != -1) { if ((index = lowerCaseContent.indexOf("href", index)) == -1) break; if ((index = lowerCaseContent.indexOf("=", index)) == -1) break; if (Thread.currentThread() != searchThread) break; index++; String remaining = content.substring(index); StringTokenizer st = new StringTokenizer(remaining, "\t\n\r\">#"); String strLink = st.nextToken(); URL urlLink; try { urlLink = new URL(url, strLink); strLink = urlLink.toString(); } catch (MalformedURLException e) { System.out.println("ERROR: bad URL " + strLink); continue; } if (urlLink.getProtocol().compareTo("http") != 0) break; if (Thread.currentThread() != searchThread) break; try { URLConnection urlLinkConnection = urlLink.openConnection(); urlLinkConnection.setAllowUserInteraction(false); InputStream linkStream = urlLink.openStream(); String strType = urlLinkConnection .guessContentTypeFromStream(linkStream); linkStream.close(); if (strType == null) break; if (strType.compareTo("text/html") == 0) { if ((!vectorSearched.contains(strLink)) && (!vectorToSearch.contains(strLink))) { vectorToSearch.addElement(strLink); } } if (strType.compareTo(strTargetType) == 0) { if (vectorMatches.contains(strLink) == false) { System.out.println(strLink); vectorMatches.addElement(strLink); numberFound++; if (numberFound >= SEARCH_LIMIT) break; } } } catch (IOException e) { System.out.println("ERROR: couldn't open URL " + strLink); continue; } } } catch (IOException e) { System.out.println("ERROR: couldn't open URL " + strURL); break; } numberSearched++; if (numberSearched >= SEARCH_LIMIT) break; } if (numberSearched >= SEARCH_LIMIT || numberFound >= SEARCH_LIMIT) System.out.println("reached search limit of " + SEARCH_LIMIT); else System.out.println("done"); searchThread = null; }
From source file:WebCrawler.java
public void run() { String strURL = textURL.getText(); String strTargetType = choiceType.getSelectedItem(); int numberSearched = 0; int numberFound = 0; if (strURL.length() == 0) { setStatus("ERROR: must enter a starting URL"); return;/*from w w w. ja v a 2s .com*/ } // initialize search data structures vectorToSearch.removeAllElements(); vectorSearched.removeAllElements(); vectorMatches.removeAllElements(); listMatches.removeAll(); vectorToSearch.addElement(strURL); while ((vectorToSearch.size() > 0) && (Thread.currentThread() == searchThread)) { // get the first element from the to be searched list strURL = (String) vectorToSearch.elementAt(0); setStatus("searching " + strURL); URL url; try { url = new URL(strURL); } catch (MalformedURLException e) { setStatus("ERROR: invalid URL " + strURL); break; } // mark the URL as searched (we want this one way or the other) vectorToSearch.removeElementAt(0); vectorSearched.addElement(strURL); // can only search http: protocol URLs if (url.getProtocol().compareTo("http") != 0) break; // test to make sure it is before searching if (!robotSafe(url)) break; try { // try opening the URL URLConnection urlConnection = url.openConnection(); urlConnection.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = urlConnection.guessContentTypeFromStream(urlStream); if (type == null) break; if (type.compareTo("text/html") != 0) break; // search the input stream for links // first, read in the entire URL byte b[] = new byte[1000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead); while (numRead != -1) { if (Thread.currentThread() != searchThread) break; numRead = urlStream.read(b); if (numRead != -1) { String newContent = new String(b, 0, numRead); content += newContent; } } urlStream.close(); if (Thread.currentThread() != searchThread) break; String lowerCaseContent = content.toLowerCase(); int index = 0; while ((index = lowerCaseContent.indexOf("<a", index)) != -1) { if ((index = lowerCaseContent.indexOf("href", index)) == -1) break; if ((index = lowerCaseContent.indexOf("=", index)) == -1) break; if (Thread.currentThread() != searchThread) break; index++; String remaining = content.substring(index); StringTokenizer st = new StringTokenizer(remaining, "\t\n\r\">#"); String strLink = st.nextToken(); URL urlLink; try { urlLink = new URL(url, strLink); strLink = urlLink.toString(); } catch (MalformedURLException e) { setStatus("ERROR: bad URL " + strLink); continue; } // only look at http links if (urlLink.getProtocol().compareTo("http") != 0) break; if (Thread.currentThread() != searchThread) break; try { // try opening the URL URLConnection urlLinkConnection = urlLink.openConnection(); urlLinkConnection.setAllowUserInteraction(false); InputStream linkStream = urlLink.openStream(); String strType = urlLinkConnection.guessContentTypeFromStream(linkStream); linkStream.close(); // if another page, add to the end of search list if (strType == null) break; if (strType.compareTo("text/html") == 0) { // check to see if this URL has already been // searched or is going to be searched if ((!vectorSearched.contains(strLink)) && (!vectorToSearch.contains(strLink))) { // test to make sure it is robot-safe! if (robotSafe(urlLink)) vectorToSearch.addElement(strLink); } } // if the proper type, add it to the results list // unless we have already seen it if (strType.compareTo(strTargetType) == 0) { if (vectorMatches.contains(strLink) == false) { listMatches.add(strLink); vectorMatches.addElement(strLink); numberFound++; if (numberFound >= SEARCH_LIMIT) break; } } } catch (IOException e) { setStatus("ERROR: couldn't open URL " + strLink); continue; } } } catch (IOException e) { setStatus("ERROR: couldn't open URL " + strURL); break; } numberSearched++; if (numberSearched >= SEARCH_LIMIT) break; } if (numberSearched >= SEARCH_LIMIT || numberFound >= SEARCH_LIMIT) setStatus("reached search limit of " + SEARCH_LIMIT); else setStatus("done"); searchThread = null; // searchThread.stop(); }
From source file:es.caib.sgtsic.util.DataHandlers.java
public static DataHandler byteArrayToDataHandler(byte[] arrayByte) { InputStream is = new BufferedInputStream(new ByteArrayInputStream(arrayByte)); String mimetype = ""; try {//from ww w.j a v a2 s .c om mimetype = URLConnection.guessContentTypeFromStream(is); } catch (IOException ex) { Logger.getLogger(DataHandlers.class.getName()).log(Level.SEVERE, null, ex); } DataSource dataSource = new ByteArrayDataSource(arrayByte, mimetype); return new DataHandler(dataSource); }
From source file:com.iskyshop.manage.seller.action.OrderSellerAction.java
private TransInfo query_ship_getData(String id) { TransInfo info = new TransInfo(); OrderForm obj = this.orderFormService.getObjById(CommUtil.null2Long(id)); try {//from w ww.j av a 2s. c om ExpressCompany ec = this.queryExpressCompany(obj.getExpress_info()); URL url = new URL("http://api.kuaidi100.com/api?id=" + this.configService.getSysConfig().getKuaidi_id() + "&com=" + (ec != null ? ec.getCompany_mark() : "") + "&nu=" + obj.getShipCode() + "&show=0&muti=1&order=asc"); URLConnection con = url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = con.guessContentTypeFromStream(urlStream); String charSet = null; if (type == null) type = con.getContentType(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) return info; if (type.indexOf("charset=") > 0) charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead, charSet); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { // String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, charSet); content += newContent; } } info = Json.fromJson(TransInfo.class, content); urlStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return info; }
From source file:org.apache.drill.exec.server.rest.WebResourceServer.java
@GET @Path("/{path}") @Produces(MediaType.TEXT_PLAIN)/*from w w w . j a va 2 s. com*/ public Response getResource(@PathParam("path") String path) throws IOException { try { String s = "rest/www/" + path; ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = new BufferedInputStream(cl.getResource(s).openStream()); String mime = "text/plain"; if (s.endsWith(".js")) { mime = "text/javascript"; } else if (s.endsWith(".css")) { mime = "text/css"; } else { mime = URLConnection.guessContentTypeFromStream(is); } byte[] d = IOUtils.toByteArray(is); return Response.ok(d).type(mime).build(); } catch (Exception e) { e.printStackTrace(); e.printStackTrace(System.out); } return Response.noContent().status(Status.NOT_FOUND).build(); }
From source file:org.kie.workbench.common.stunner.core.backend.util.URLUtils.java
public static String guessContentType(final String fileName, final InputStream stream) throws Exception { final String contentType = URLConnection.guessContentTypeFromStream(stream); if (null == contentType) { final int index = fileName.lastIndexOf("."); return index >= 0 ? "image/" + fileName.substring(index + 1, fileName.length()) : null; }/*from w w w .ja v a2 s.co m*/ return contentType; }
From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java
private TransInfo query_Returnship_getData(String id) { TransInfo info = new TransInfo(); ReturnGoodsLog obj = this.returnGoodsLogService.getObjById(CommUtil.null2Long(id)); try {/*from w w w . j ava2 s . c o m*/ ExpressCompany ec = this.queryExpressCompany(obj.getReturn_express_info()); String query_url = "http://api.kuaidi100.com/api?id=" + this.configService.getSysConfig().getKuaidi_id() + "&com=" + (ec != null ? ec.getCompany_mark() : "") + "&nu=" + obj.getExpress_code() + "&show=0&muti=1&order=asc"; URL url = new URL(query_url); URLConnection con = url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = con.guessContentTypeFromStream(urlStream); String charSet = null; if (type == null) type = con.getContentType(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) return info; if (type.indexOf("charset=") > 0) charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead, charSet); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { // String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, charSet); content += newContent; } } info = Json.fromJson(TransInfo.class, content); urlStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return info; }
From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java
private TransInfo query_ship_getData(String id) { TransInfo info = null;/* ww w .j a va 2 s . c o m*/ OrderForm obj = this.orderFormService.getObjById(CommUtil.null2Long(id)); if (obj != null && !CommUtil.null2String(obj.getShipCode()).equals("")) { info = new TransInfo(); try { ExpressCompany ec = this.queryExpressCompany(obj.getExpress_info()); String query_url = "http://api.kuaidi100.com/api?id=" + this.configService.getSysConfig().getKuaidi_id() + "&com=" + (ec != null ? ec.getCompany_mark() : "") + "&nu=" + obj.getShipCode() + "&show=0&muti=1&order=asc"; URL url = new URL(query_url); URLConnection con = url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = con.guessContentTypeFromStream(urlStream); String charSet = null; if (type == null) type = con.getContentType(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) return info; if (type.indexOf("charset=") > 0) charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead, charSet); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { // String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, charSet); content += newContent; } } info = Json.fromJson(TransInfo.class, content); urlStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return info; }
From source file:com.indusborn.service.FileStoragePostingImageService.java
private String guessContentType(byte[] imageBytes) throws IOException { return URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(imageBytes)); }