-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
2,751 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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.iveely.analyzer; | ||
|
||
import com.iveely.framework.net.websocket.EchoWebSocket; | ||
import com.iveely.analyzer.service.SCallback; | ||
import com.iveely.analyzer.service.WebSocketRequest; | ||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
* @author X1 Carbon | ||
*/ | ||
public class Program { | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main2(String[] args) { | ||
start(); | ||
} | ||
|
||
private static void start() { | ||
try { | ||
SCallback callback = new SCallback(); | ||
Thread syncThread = new Thread(callback); | ||
syncThread.start(); | ||
|
||
WebSocketRequest request = new WebSocketRequest(callback); | ||
System.out.println("Web socket is starting."); | ||
EchoWebSocket socket = new EchoWebSocket(request, 9011); | ||
socket.service(); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
149 changes: 149 additions & 0 deletions
149
Iveely.Analyzer/src/com/iveely/analyzer/common/HtmlDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* 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.iveely.analyzer.common; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import org.apache.log4j.Logger; | ||
|
||
/** | ||
* Html downloader. | ||
* | ||
* @author [email protected] | ||
* @date 2014-10-21 21:09:52 | ||
*/ | ||
public class HtmlDownloader { | ||
|
||
/** | ||
* Logger. | ||
*/ | ||
private static final Logger logger = Logger.getLogger(HtmlDownloader.class.getName()); | ||
|
||
/** | ||
* Get web source code. | ||
* | ||
* @param url | ||
* @param charset | ||
* @return | ||
*/ | ||
public static String getHtmlContent(String url, String charset) { | ||
if (!url.toLowerCase().startsWith("http://")) { | ||
url = "http://" + url; | ||
} | ||
try { | ||
URL rUrl = new URL(url); | ||
return getHtmlContent(rUrl, charset, 0); | ||
} catch (MalformedURLException e) { | ||
} | ||
return ""; | ||
} | ||
|
||
/** | ||
* Get web source code. | ||
* | ||
* @param url | ||
* @param charset | ||
* @param timestamp | ||
* @param tryCount | ||
* @return | ||
*/ | ||
private static String getHtmlContent(URL url, String charset, Integer tryCount) { | ||
if (tryCount > 3) { | ||
return null; | ||
} | ||
StringBuilder contentBuffer = new StringBuilder(); | ||
int responseCode; | ||
HttpURLConnection con = null; | ||
try { | ||
con = (HttpURLConnection) url.openConnection(); | ||
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0;) like Gecko"); | ||
con.setConnectTimeout(5000); | ||
con.setReadTimeout(5000); | ||
responseCode = con.getResponseCode(); | ||
if (responseCode == -1) { | ||
con.disconnect(); | ||
return null; | ||
} | ||
if (responseCode >= 400) { | ||
con.disconnect(); | ||
return null; | ||
} | ||
if (responseCode == 304) { | ||
return ""; | ||
} | ||
String responseMsg = con.getContentType(); | ||
if (responseMsg == null || (responseMsg != null && !responseMsg.contains("text/html"))) { | ||
con.disconnect(); | ||
return null; | ||
} | ||
|
||
String charsetString = con.getContentEncoding(); | ||
boolean isSureCharset = false; | ||
if (responseMsg.toUpperCase().contains("GB2312")) { | ||
charsetString = "GB2312"; | ||
isSureCharset = true; | ||
} else if (responseMsg.toUpperCase().contains("GBK")) { | ||
charsetString = "GBK"; | ||
isSureCharset = true; | ||
} else { | ||
charsetString = charset; | ||
} | ||
try (InputStream inStr = con.getInputStream()) { | ||
InputStreamReader istreamReader = new InputStreamReader(inStr, charsetString); | ||
try (BufferedReader buffStr = new BufferedReader(istreamReader)) { | ||
String str; | ||
while ((str = buffStr.readLine()) != null) { | ||
contentBuffer.append(str); | ||
} | ||
if (!isSureCharset) { | ||
String metaCharset = getEncoding(contentBuffer.toString()).toUpperCase(); | ||
if (!metaCharset.equals(charsetString)) { | ||
inStr.close(); | ||
String tryResult = getHtmlContent(url, metaCharset, tryCount + 1); | ||
if (tryResult != null) { | ||
return tryResult; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} catch (IOException e) { | ||
logger.error(e); | ||
} finally { | ||
if (con != null) { | ||
con.disconnect(); | ||
} | ||
} | ||
return contentBuffer.toString(); | ||
} | ||
|
||
/** | ||
* Get encoding of the page. | ||
* | ||
* @param html | ||
* @return | ||
*/ | ||
private static String getEncoding(String html) { | ||
int charsetStartIndex = html.toUpperCase().indexOf("CHARSET="); | ||
if (charsetStartIndex > 0) { | ||
int charsetEndIndex = html.indexOf(">", charsetStartIndex); | ||
if (charsetEndIndex - charsetStartIndex > 20) { | ||
charsetEndIndex = html.indexOf(" ", charsetStartIndex); | ||
} | ||
if (charsetEndIndex < html.length()) { | ||
String metaCharset = html.substring(charsetStartIndex + 8, charsetEndIndex).replace("\"", "").replace("/", "").replace("'", "").trim(); | ||
return metaCharset; | ||
} | ||
} | ||
return "UTF-8"; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
Iveely.Analyzer/src/com/iveely/analyzer/common/Thumbnail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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.iveely.analyzer.common; | ||
|
||
import java.awt.Graphics2D; | ||
import java.awt.Image; | ||
import java.awt.RenderingHints; | ||
import java.awt.geom.AffineTransform; | ||
import java.awt.image.BufferedImage; | ||
import java.awt.image.ColorModel; | ||
import java.awt.image.WritableRaster; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.Base64; | ||
import javax.imageio.ImageIO; | ||
|
||
/** | ||
* Thumbnail for image. | ||
* | ||
* @author [email protected] | ||
* @date 2014-11-22 18:35:31 | ||
*/ | ||
public class Thumbnail { | ||
|
||
/** | ||
* Build thumbnail from local file. | ||
* | ||
* @param imagePath | ||
* @param toWidth | ||
* @param toHeight | ||
* @return Image data in base64. | ||
* @throws java.lang.Exception | ||
*/ | ||
public static String getImageBase64(String imagePath, | ||
int toWidth, int toHeight) throws Exception { | ||
BufferedImage srcImage; | ||
File fromFile = new File(imagePath); | ||
srcImage = ImageIO.read(fromFile); | ||
if (toWidth > 0 && toHeight > 0) { | ||
srcImage = resize(srcImage, toWidth, toHeight, true); | ||
} | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
ImageIO.write(srcImage, "jpg", outputStream); | ||
byte[] bytes = outputStream.toByteArray(); | ||
return Base64.getEncoder().encodeToString(bytes); | ||
} | ||
|
||
/** | ||
* Build thumbnail from online file. | ||
* | ||
* @param url | ||
* @param toWidth | ||
* @param toHeight | ||
* @param filterSmall | ||
* @return Image data in base64. | ||
*/ | ||
public static String getImageBase64FromNet(String url, int toWidth, int toHeight, boolean filterSmall) { | ||
try { | ||
URL curl = new URL(url); | ||
URLConnection con = curl.openConnection(); | ||
con.setConnectTimeout(10000); | ||
con.setReadTimeout(10000); | ||
InputStream in = con.getInputStream(); | ||
BufferedImage image = ImageIO.read(in); | ||
if (image == null || ((image.getWidth() < 75 || image.getHeight() < 75) && filterSmall)) { | ||
return ""; | ||
} | ||
image = resize(image, toWidth, toHeight, true); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
ImageIO.write(image, "gif", outputStream); | ||
byte[] bytes = outputStream.toByteArray(); | ||
return Base64.getEncoder().encodeToString(bytes); | ||
} catch (IOException e) { | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Resize image. | ||
* | ||
* @param source | ||
* @param targetW | ||
* @param targetH | ||
* @param equalProportion | ||
* @return | ||
*/ | ||
public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean equalProportion) { | ||
int type = source.getType(); | ||
BufferedImage target; | ||
double sx = (double) targetW / source.getWidth(); | ||
double sy = (double) targetH / source.getHeight(); | ||
if (equalProportion) { | ||
if (sx > sy) { | ||
sx = sy; | ||
targetW = (int) (sx * source.getWidth()); | ||
} else { | ||
sy = sx; | ||
targetH = (int) (sx * source.getHeight()); | ||
} | ||
} | ||
if (type == BufferedImage.TYPE_CUSTOM) { | ||
ColorModel cm = source.getColorModel(); | ||
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); | ||
boolean alphaPremultiplied = cm.isAlphaPremultiplied(); | ||
target = new BufferedImage(cm, raster, alphaPremultiplied, null); | ||
} else { | ||
target = new BufferedImage(targetW, targetH, Image.SCALE_SMOOTH); | ||
Graphics2D g = target.createGraphics(); | ||
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | ||
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy)); | ||
g.dispose(); | ||
} | ||
return target; | ||
} | ||
} | ||
|
Oops, something went wrong.