请考虑以下代码段:
public interface MyInterface {
public int getId();
}
public class MyPojo implements MyInterface {
private int id;
public MyPojo(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
public ArrayList<MyInterface> getMyInterfaces() {
ArrayList<MyPojo> myPojos = new ArrayList<MyPojo>(0);
myPojos.add(new MyPojo(0));
myPojos.add(new MyPojo(1));
return (ArrayList<MyInterface>) myPojos;
}
Run Code Online (Sandbox Code Playgroud)
return语句执行不编译的转换.如何将myPojos列表转换为更通用的列表,而不必遍历列表中的每个项目?
谢谢
我必须登录https网页并使用Java下载文件.我事先知道所有的URL:
baseURL = // a https URL;
urlMap = new HashMap<String, URL>();
urlMap.put("login", new URL(baseURL, "exec.asp?login=username&pass=XPTO"));
urlMap.put("logout", new URL(baseURL, "exec.asp?exec.asp?page=999"));
urlMap.put("file", new URL(baseURL, "exec.asp?file=111"));
Run Code Online (Sandbox Code Playgroud)
如果我在像Firefox这样的网络浏览器中尝试所有这些链接,它们都能正常工作.
现在我做的时候:
urlConnection = urlMap.get("login").openConnection();
urlConnection.connect();
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
Run Code Online (Sandbox Code Playgroud)
我刚刚再次返回登录页面HTML,我无法进行文件下载.
谢谢!