|
@@ -6,6 +6,8 @@
|
|
|
package cn.com.qmth.examcloud.core.questions.service.temp;
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
@@ -16,41 +18,7 @@ import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
public class ImgDataUtils {
|
|
|
-
|
|
|
- @SuppressWarnings("unused")
|
|
|
- public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
- /*String html = "ss<img src=\"http://fanyi.bdstatic.com/static/translation/img/header/logo_cbfea26.png\"/>aa"
|
|
|
- + "sss<img src=\"http://fanyi.bdstatic.com/static/translation/pkg/index_z_15cc24c.png\"/>ss";*/
|
|
|
- String html = "in its <img class=\"UnderLine15\" src=\"/Shared/UnderLine?index=1\" alt=\"\" />area."
|
|
|
- + "in its <img class=\"UnderLine15\" src=\"/Shared/UnderLine?index=1\" alt=\"\" />area. People could"
|
|
|
- + "nbsp;<img class=\"UnderLine15\" src=\"/Shared/UnderLine?index=2\" alt=\"\" /> than they cou"
|
|
|
- + "that led to downtown.<br /><img class=\"UnderLine15\" src=\"/Shared/UnderLine?index=3\" alt=\"\" />&nb,"
|
|
|
- + "people <img class=\"UnderLine15\" src=\"/Shared/UnderLine?index=4\" alt=\"\" /> ";
|
|
|
- List<String> srcList = parseImages(html);
|
|
|
- /*for (String src : srcList) {
|
|
|
- System.out.println(src);
|
|
|
- src = "http://ncexam.cug.edu.cn/UploadFiles/%E5%88%98%E6%98%8C%E8%BE%89/Images/OnlineExam20140923161629.jpg";
|
|
|
- //http://ncexam.cug.edu.cn%2FUploadFiles%2F %E5%88%98%E6%98%8C%E8%BE%89%2FImages%2FOnlineExam20140923161629.jpg
|
|
|
- String base64 = loadImageToBase64(src);
|
|
|
- //System.out.println(base64);
|
|
|
- List<String> parseImageSrc = parseImageSrc(src);
|
|
|
- System.out.println(parseImageSrc.get(0));
|
|
|
-
|
|
|
-
|
|
|
- }*/
|
|
|
- String ssString = "/UploadFiles/刘昌辉/Images/OnlineExam20140923161629.jpg";
|
|
|
- //String ssString = java.net.URLEncoder.encode("刘昌辉/Images/OnlineExam20140923161629.jpg", "GBK");
|
|
|
- System.out.println(ssString);
|
|
|
-
|
|
|
- String testTxt = "8个文件";
|
|
|
- Pattern pat = Pattern.compile("[\u4E00-\u9FA5]");
|
|
|
- Matcher mat = pat.matcher(ssString);
|
|
|
- while (mat.find()) {
|
|
|
- System.out.println(mat.group());
|
|
|
- }
|
|
|
- String ss = java.net.URLEncoder.encode("刘昌辉", "utf-8");
|
|
|
- System.out.println(ss);
|
|
|
- }
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ImgDataUtils.class);
|
|
|
|
|
|
/**
|
|
|
* 获取html中所有图片的src地址
|
|
@@ -127,7 +95,7 @@ public class ImgDataUtils {
|
|
|
InputStream inStream = conn.getInputStream();
|
|
|
return readInputStream(inStream);//得到图片的二进制数据
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.toString());
|
|
|
return null;
|
|
|
} finally {
|
|
|
if (conn != null) {
|
|
@@ -140,6 +108,7 @@ public class ImgDataUtils {
|
|
|
try {
|
|
|
return Base64.encodeBase64String(data);
|
|
|
} catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
@@ -148,6 +117,7 @@ public class ImgDataUtils {
|
|
|
try {
|
|
|
return Base64.decodeBase64(data);
|
|
|
} catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
@@ -162,14 +132,14 @@ public class ImgDataUtils {
|
|
|
outStream = new FileOutputStream(imageFile);
|
|
|
outStream.write(data);
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
} finally {
|
|
|
try {
|
|
|
if (outStream != null) {
|
|
|
outStream.close();
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -187,14 +157,14 @@ public class ImgDataUtils {
|
|
|
}
|
|
|
return outStream.toByteArray();
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
} finally {
|
|
|
try {
|
|
|
if (inStream != null) {
|
|
|
inStream.close();
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
return null;
|