@@ -39,8 +39,6 @@ public class RequestHelper {
39
39
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0" ,
40
40
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.41 Safari/537.36 Edg/88.0.705.22"
41
41
};
42
- private final String path = System .getProperty ("user.dir" ) +
43
- System .getProperty ("file.separator" ) + "iconhash" + System .getProperty ("file.separator" );
44
42
45
43
private RequestHelper () {
46
44
this .logger = Logger .getLogger ("RequestHelper" );
@@ -124,17 +122,14 @@ public HashMap<String, String> getHTML(String url) {
124
122
/**
125
123
* 提取网站favicon 需要两步:
126
124
* 1. 直接访问url根目录的favicon,若404则跳转至第2步
127
- * 2. 访问网站,获取html页面,获取head头中的 link标签的ico 路径
125
+ * 2. 访问网站,获取html页面,获取head中的 link标签的ico 路径
128
126
*
129
127
* @param url
130
128
* @return
131
129
*/
132
130
public HashMap <String , String > getImageFavicon (String url ) {
133
- String filename = this .path + "favicon.ico" ;
134
- File file = new File (filename );
135
131
CloseableHttpResponse response = getResponse (url );
136
132
HashMap <String , String > result = new HashMap <>();
137
- int cache = 10240 ;
138
133
if (response != null ) {
139
134
int code = response .getStatusLine ().getStatusCode ();
140
135
result .put ("code" , String .valueOf (code ));
@@ -144,16 +139,18 @@ public HashMap<String, String> getImageFavicon(String url) {
144
139
logger .log (Level .FINE , url + "无响应内容" );
145
140
return null ;
146
141
}
147
- FileOutputStream fileout = new FileOutputStream (file );
148
142
InputStream is = response .getEntity ().getContent ();
149
- byte [] buffer = new byte [cache ];
150
- int ch = 0 ;
151
- while ((ch = is .read (buffer )) != -1 ) {
152
- fileout .write (buffer , 0 , ch );
143
+ byte [] buffer = new byte [1024 ];
144
+ ByteArrayOutputStream bos =new ByteArrayOutputStream ();
145
+ int len = 0 ;
146
+ while ((len = is .read (buffer ))!=-1 ){
147
+ bos .write (buffer ,0 , len );
153
148
}
154
- is .close ();
155
- fileout .flush ();
156
- fileout .close ();
149
+ bos .flush ();
150
+ String encoded = new BASE64Encoder ().encode (Objects .requireNonNull (bos .toByteArray ()));
151
+ String hash = getIconHash (encoded );
152
+ result .put ("msg" , "icon_hash=\" " + hash + "\" " );
153
+ return result ;
157
154
} catch (Exception e ) {
158
155
result .put ("code" , "error" );
159
156
result .put ("msg" , e .getMessage ());
@@ -166,11 +163,6 @@ public HashMap<String, String> getImageFavicon(String url) {
166
163
logger .log (Level .WARNING , ex .getMessage (), ex );
167
164
}
168
165
}
169
- String hash = getIconHash (filename );
170
- if (hash != null ) {
171
- result .put ("msg" , hash .replaceAll ("\n " , "" ));
172
- return result ;
173
- }
174
166
}
175
167
}
176
168
return null ;
@@ -202,18 +194,13 @@ public String getLinkIcon(String url) {
202
194
}
203
195
204
196
/**
205
- * 使用iconhash计算favicon hash
197
+ * 计算favicon hash
206
198
*
207
199
* @param f favicon的文件对象
208
200
* @return favicon hash值
209
201
*/
210
202
private String getIconHash (String f ) {
211
- String os = System .getProperty ("os.name" ).toLowerCase (Locale .US );
212
- String arch = System .getProperty ("os.arch" ).toLowerCase (Locale .US );
213
- StringBuilder filepath = new StringBuilder ();
214
- System .out .println (f );
215
- String base64Str = ImageToBase64 (f );
216
- int murmu = Hashing .murmur3_32 ().hashString (base64Str .replaceAll ("\r " , "" ) + "\n " , StandardCharsets .UTF_8 ).asInt ();
203
+ int murmu = Hashing .murmur3_32 ().hashString (f .replaceAll ("\r " , "" ) + "\n " , StandardCharsets .UTF_8 ).asInt ();
217
204
return String .valueOf (murmu );
218
205
}
219
206
@@ -232,22 +219,22 @@ public String getCertSerialNum(String host) {
232
219
return null ;
233
220
}
234
221
235
- private static String ImageToBase64 (String imgPath ) {
236
- byte [] data = null ;
237
- // 读取图片字节数组
238
- try {
239
- InputStream in = new FileInputStream (imgPath );
240
- data = new byte [in .available ()];
241
- in .read (data );
242
- in .close ();
243
- } catch (IOException e ) {
244
- e .printStackTrace ();
245
- }
246
- // 对字节数组Base64编码
247
- BASE64Encoder encoder = new BASE64Encoder ();
248
- // 返回Base64编码过的字节数组字符串
249
- return encoder .encode (Objects .requireNonNull (data ));
250
- }
222
+ // private static String ImageToBase64(InputStream imgPath) {
223
+ // byte[] data = null;
224
+ // // 读取图片字节数组
225
+ // try {
226
+ // InputStream in = new FileInputStream(imgPath);
227
+ // data = new byte[in.available()];
228
+ // in.read(data);
229
+ // in.close();
230
+ // } catch (IOException e) {
231
+ // e.printStackTrace();
232
+ // }
233
+ // // 对字节数组Base64编码
234
+ // BASE64Encoder encoder = new BASE64Encoder();
235
+ // // 返回Base64编码过的字节数组字符串
236
+ // return encoder.encode(Objects.requireNonNull(data));
237
+ // }
251
238
252
239
/**
253
240
* base64编码字符串
0 commit comments