url格式不正确,可能有"www.baidu.com"    "这个不能有

// 下载pdf
public void downpdf(String URL, String fileName) {
try {
// byte[] data = jsonStr.getBytes(); java.net.URL url = new java.net.URL(URL);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url
.openConnection(); conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);// 设置连接超时时间为5秒
conn.setReadTimeout(20 * 1000);// 设置读取超时时间为20秒
// 使用 URL 连接进行输出,则将 DoOutput标志设置为 true
conn.setDoOutput(true); conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 如果请求响应码是200,则表示成功
if (conn.getResponseCode() == 200) {
InputStream stream = conn.getInputStream();
FileOutputStream fos = null;
BufferedInputStream bis = null;
// HttpURLConnection httpUrl = null;
// URL url = null;
int BUFFER_SIZE = 1024;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
bis = new BufferedInputStream(stream);
// //建立文件
fos = new FileOutputStream(fileName);
// //保存文件
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
// fos.close();
bis.close();
// httpUrl.disconnect();
}
conn.disconnect();// 断开连接
} catch (Exception e) {
System.out.println(e.getMessage());
} }
05-11 19:25