数据越界
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at img.test.main(test.java:14)
检查读取数组时是否试图取出索引没有的数据。
例:当list有2个数据,当你试图取出第三个数据则这个异常
解决方法:检查是否代码错写,或者加异常处理
io流的重复使用
例子:从网络获取图片流,将此图片进行截图,并且返回图片流的二进制数组
/**
* 通过图片url截图
* @param url_img 图片链接
* @param outFile输出文件
* @param ext 图片的格式
* @param x坐标
* @param y坐标
* @param width宽度
* @param height高度
* @return
*/
public static byte[] cutUrlPic(String url_img, String outPath, String ext, int x, int y, int width, int height) {
//outPath: 存储固定位置
ImageInputStream iis = null;
InputStream is = null;
String res = null;
byte[] data = null;
try {
URL url = new URL(url_img);
// 打开连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(6 * 1000);
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
conn.connect();
// 输入流
is = conn.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
data = outStream.toByteArray();
//System.out.println(data[0]);
// res = ImageByteUtil.byte2hex(data);
outStream.close();
is = new ByteArrayInputStream(data);
// ImageReader声称能够解码指定格式
Iterator it = ImageIO.getImageReadersByFormatName(ext);
ImageReader reader = it.next();
// 获取图片流
iis = ImageIO.createImageInputStream(is);
// 输入源中的图像将只按顺序读取
reader.setInput(iis, true);
// 描述如何对流进行解码
ImageReadParam param = reader.getDefaultReadParam();
// 图片裁剪区域
Rectangle rect = new Rectangle(x, y, width, height);
// 提供一个 BufferedImage,将其用作解码像素数据的目标
param.setSourceRegion(rect);
// 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象
BufferedImage bi = reader.read(0, param);
// 保存新图片
File tempOutFile = new File(outPath);
if (!tempOutFile.exists()) {
tempOutFile.mkdirs();
}
ImageIO.write(bi, ext, new File(outPath ));
// return true;
} catch (Exception e) {
logger.error(e.getMessage(), e);
//e.printStackTrace();
// return false;
} finally {
try {
if (iis != null) {
iis.close();
}
if(is!=null){
is.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
//e.printStackTrace();
// return false;
}
}
return data;
}
不合法参数
[2018/09/01 20:32:25]-[ERROR]-com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:181)
java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Unknown Source)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:159)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.shangbiaoCollect(TrademarkMainCollect.java:65)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.main(TrademarkMainCollect.java:39)
[2018/09/01 20:32:30]-[ERROR]-com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:181)
解决:
此次的问题是传入到数据库的参数不是数据库要的,所以报错。
将参数改成他能接收的就行
违反了键的唯一性
[2018/09/01 21:15:48]-[ERROR]-com.bayside.crawler.qingdaocollect.SimilarToPicture.db(SimilarToPicture.java:77) Duplicate entry 'e48b92f1655cc3a401655f9862966366-14258069' for key 'compare_unIque'
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'e48b92f1655cc3a401655f9862966366-14258069' for key 'compare_unIque'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2643)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at com.bayside.crawler.qingdaocollect.SimilarToPicture.db(SimilarToPicture.java:74)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:140)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.shangbiaoCollect(TrademarkMainCollect.java:61)
at com.bayside.crawler.qingdaocollect.StartCollect$CollectThread.shangBiaoCollect(StartCollect.java:66)
at com.bayside.crawler.qingdaocollect.StartCollect$CollectThread.run(StartCollect.java:52)
解决方法:
查看此键是否已经在数据库存在,如果已经存在则就会报这个错误,可以根据需要,将数据库里的删除或者做异常处理,将此id跳过
所谓的空指针异常,就是一个指针是空指针,你还要去操作它,既然它指向的是空对象,它就不能使用这个对象的方法。比如上面的s假如为null,你还要用s的方法,比如s.equals( String x);那么就会产生空指针异常。
[2018/09/01 21:15:56]-[ERROR]-com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:186)
java.lang.NullPointerException
at com.bayside.crawler.qingdaocollect.util.ImageByteUtil.byteToString(ImageByteUtil.java:47)
at com.bayside.crawler.qingdaocollect.ImgInfoCollect.ImagePage(ImgInfoCollect.java:36)
at com.bayside.crawler.qingdaocollect.TrademarkCollect.image(TrademarkCollect.java:47)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:147)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.shangbiaoCollect(TrademarkMainCollect.java:61)
at com.bayside.crawler.qingdaocollect.StartCollect$CollectThread.shangBiaoCollect(StartCollect.java:66)
at com.bayside.crawler.qingdaocollect.StartCollect$CollectThread.run(StartCollect.java:52)
解决:
在调用某个对象之前,要进行非空的验证,否则就容易出空指针异常。
json字符串格式的问题
[2018/09/01 22:00:02]-[ERROR]-com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:144) Unexpected character ('A' (code 65)): was expecting comma to separate OBJECT entries
at [Source: java.io.StringReader@40b7341a; line: 1, column: 17]
org.codehaus.jackson.JsonParseException: Unexpected character ('A' (code 65)): was expecting comma to separate OBJECT entries
at [Source: java.io.StringReader@40b7341a; line: 1, column: 17]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:406)
at org.codehaus.jackson.map.deser.std.MapDeserializer._readAndBind(MapDeserializer.java:304)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:249)
at org.codehaus.jackson.map.deser.std.MapDeserializer.deserialize(MapDeserializer.java:33)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.homeImg(TrademarkMainCollect.java:137)
at com.bayside.crawler.qingdaocollect.TrademarkMainCollect.shangbiaoCollect(TrademarkMainCollect.java:61)
at com.bayside.crawler.qingdaocollect.StartCollect$CollectThread.shangBiaoCollect(StartCollect.java:66)
at com.bayside.crawler.qingdaocollect.StartCollect$CollectThread.run(StartCollect.java:52)
解决:
检查json字符串的格式是否不对
The folder is already a source folder
https://www.cnblogs.com/loger1995/p/6539139.html
解决:适用于我自己,将maven的配置文件取消,然后将下载的文件从c:\user\xxxx.m2
中复制到自己设的本地仓库文件,将配置加入。更新,成功。
重载:重载的时候,方法名要一样,但是参数类型或个数不一样,返回值类型可以相同也可以不相同。无法以返回型别作为重载函数的区分标准。
覆写(重写):若子类中的方法与父类中的某一方法具有相同的方法名、返回类型和参数表,则新方法将覆盖原有的方法
1.新建一个子类对象时,它首先执行它父类的构造方法然后再执行子类的构造方法。
2.我们new一个子类对象赋给父类(父类的引用指向了子类的对象,也就是说java里说的多态),父类对象不可以调用子类的方法,但如果子类重写了父类的某个方法,则此时父类调用的是子类方法的内容。
1.改工作空间的编码
window - preferences - general - workspace
2.修改项目的编码
右键项目 - properties - resource
3.修改大哥文件的编码(当前两个不管用的时候,看看这个文件是不是编码没有更改)
右键单个文件 - properties - resource
1.好好找找在源码的另一个地方是不是存在
2.jsoup
3.httpclient
4.模拟浏览器
解决:
import com.alibaba.fastjson.JSONObject;
String string = JSONObject.parse(jsonVideo).toString();
final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
final Base64.Decoder decoder = Base64.getDecoder();
final Base64.Encoder encoder = Base64.getEncoder();
final String text = "字串文字";
final byte[] textByte = text.getBytes("UTF-8");
//编码
final String encodedText = encoder.encodeToString(textByte);
System.out.println(encodedText);
//解码
System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
String pdJson = "[{"1":"2"}]";
ObjectMapper mapper = new ObjectMapper();
List