小编Ry.*_*Ry.的帖子

如何在Java中使用泛型来转换列表?

请考虑以下代码段:

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列表转换为更通用的列表,而不必遍历列表中的每个项目

谢谢

java generics casting

26
推荐指数
3
解决办法
3万
查看次数

如何从Java登录和下载https网页中的文件?

我必须登录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,我无法进行文件下载.

谢谢!

java https

3
推荐指数
1
解决办法
9601
查看次数

标签 统计

java ×2

casting ×1

generics ×1

https ×1