package utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Urlpicture {
public String get_main_url(int num) {
String jpgurl = null;
try{
String url = "https://www.zigbang.com/items1/"+num;
jpgurl = "http://z1.zigbang.com/items/7697786/538ec7a16033f0c32eac29ac50deb04a1e6bf3ba.jpg?h=800&q=60";
Document doc = Jsoup.connect(url).get(); // this part require try catch
jpgurl = doc.select("img[src$=.jpg?h=800&q=60]").get(0).absUrl("src");
System.out.println(url);
System.out.println(jpgurl);
}catch(Exception e){
e.printStackTrace();
}
return jpgurl;
}
}
从Jsoup Connect获取文档。 但是如果Jsoup.connect发出错误。我想要 jgurl作为别的东西并继续运行代码。 这不可能吗? (此代码将从外部调用多次,但如果发生一个异常。它已停止)
外部来源称这个urlpicture
@RequestMapping("/search")
public ModelAndView searchHandler() throws JsonProcessingException {
ModelAndView mav = new ModelAndView("t_search");
List<HashMap<String, Object>> mlist = rd.test_room();
//=====================================================
List<String> urlcol = new ArrayList<>();
JSONArray json_arr=new JSONArray();//
for (Map<String, Object> map : mlist) {
JSONObject json_obj=new JSONObject();
String url = null;
System.out.println(map.get("SELL_NUM"));
int num = Integer.parseInt(map.get("SELL_NUM")+"");
String str = (urp.get_main_url(num).toString());// this calling the function
System.out.println(str);
}
urlcol.add(url);
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
try {
json_obj.put(key,value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
(json_arr).put(json_obj);
}
=============================================== ============修复后
@RequestMapping("/search")
public ModelAndView searchHandler() throws JsonProcessingException {
ModelAndView mav = new ModelAndView("t_search");
List<HashMap<String, Object>> mlist = rd.test_room();
List<String> urlcol = new ArrayList<>();
JSONArray json_arr=new JSONArray();
for (Map<String, Object> map : mlist) {
JSONObject json_obj=new JSONObject();
String url = null;
String input = null;
System.out.println(map.get("SELL_NUM"));
int num = 0;
num = Integer.parseInt(map.get("SELL_NUM")+"");
input = "https://www.zigbang.com/items1/"+num;
System.out.println(input);
try{
url= (urp.get_main_url(input));
System.out.println(url);
}catch(Exception e){
System.out.println("No=");
e.printStackTrace();
url = "http://z1.zigbang.com/items/7697786/538ec7a16033f0c32eac29ac50deb04a1e6bf3ba.jpg?h=800&q=60";
@Service
public class Urlpicture {
public String get_main_url(String url) {
Document doc;
String jpgurl = null;
try{
jpgurl = "http://z1.zigbang.com/items/7697786/538ec7a16033f0c32eac29ac50deb04a1e6bf3ba.jpg?h=800&q=60";
jpgurl = Jsoup.connect(url).get().select("img[src$=.jpg?h=800&q=60]").get(0).absUrl("src");
}catch(Exception e){
e.printStackTrace();
}finally{
return jpgurl;
}
=============================================== ====================== 修复后,它会在异常后继续运行。 但即使url链接存在有效数字 它将所有值作为异常返回。
7666495
https://www.zigbang.com/items1/7666495// this link is valid but throw exception
No=
java.lang.NullPointerException
at controller.main_controller.searchHandler(main_controller.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springf
我不知道为什么这会继续抛出所有值的异常
答案 0 :(得分:1)
问题在于jgurl
变量的范围,您需要在try
块之前声明,如下所示。
另外,要解决第二个问题,您需要将下载图像逻辑提取到单独的方法,并在找不到给定num
的图像时使用默认num
再次调用它,这也显示如下:
public String get_main_url(int num) {
int num = 7453201;
String jpgurl = null;
try {
downloadImage(num);
} catch (Exception e) {
e.printStackTrace();
num=0;//set the default value for which img exists
downloadImage(num);//call downloadImage with default num
}
}
private String downloadImage(int num) {
String url = "https://www.zigbang.com/items1/"+num;
System.out.println(url);
Document doc = Jsoup.connect(url).get();
jpgurl = doc.select("img[src$=.jpg?h=800&q=60]").get(0).absUrl("src");
System.out.println(i + "="+jpgurl);
}