日期:2014-05-20  浏览次数:20675 次

大家看一下,这个异常yuany
package http;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class HttpUtils {
private static String URL_PATH = "http://10.109.2.45:8080/mthttp/wallpaper3.jpg";

public HttpUtils() {
// TODO 自动生成的构造函数存根
}

public static void SavaImageToDisk() {
InputStream inputStream = getInputStream();
byte[] date = new byte[1024];
int len = 0;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("E:\\test.jpg");
while ((len = inputStream.read(date)) != -1) {
fileOutputStream.write(date, 0, len);
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}

public static InputStream getInputStream() {
InputStream inputStream = null;
HttpsURLConnection httpsURLConnection = null;
try {
URL url = new URL(URL_PATH);
if (url != null) {
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(3000);
httpsURLConnection.setDoInput(true);
httpsURLConnection.setRequestMethod("GET");
int responsecode = httpsURLConnection.getResponseCode();
if (responsecode == 200) {
inputStream = httpsURLConnection.getInputStream();
}
}
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return inputStream;

}

public static void main(String[] args) {
SavaImageToDisk();
}
}


以下是错误信息
Exception in thread "main" java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection
at http.HttpUtils.getInputStream(HttpUtils.java:57)
at http.HttpUtils.SavaImageToDisk(HttpUtils.java:19)
at http.HttpUtils.main(HttpUtils.java:78)




------解决方案--------------------