public class TestFileDownload {
public static void main(String []args) throws IOException{
//url中间有空格,所以需要作一下处理
String timestamp = URLEncoder.encode("2017-02-07 14:47:07","UTF-8");
String url = "http://localhost:8080/niukou/" + timestamp + "/Q.jpg";
System.out.println(timestamp);
System.out.println(urlStr1);
String savePath = "D:\\";
System.out.println(downloadFile(url, savePath));
}
private static boolean downloadFile(String httpUrl, String savePath) {
int byteread = 0;
try {
URL url = new URL(httpUrl);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream(savePath+"t.jpg");
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
System.out.println(savePath+" download finished!");
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
public static void main(String []args) throws IOException{
//url中间有空格,所以需要作一下处理
String timestamp = URLEncoder.encode("2017-02-07 14:47:07","UTF-8");
String url = "http://localhost:8080/niukou/" + timestamp + "/Q.jpg";
System.out.println(timestamp);
System.out.println(urlStr1);
String savePath = "D:\\";
System.out.println(downloadFile(url, savePath));
}
private static boolean downloadFile(String httpUrl, String savePath) {
int byteread = 0;
try {
URL url = new URL(httpUrl);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream(savePath+"t.jpg");
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
System.out.println(savePath+" download finished!");
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}