public void saveToDis() {
InputStream inputStream = getInputStream();
byte[] data = new byte[1024];
int len = 0;
FileOutputStream outputStream = null;
try {
// 读取后台数据存储位置
String fileName = "";
outputStream = new FileOutputStream(fileName);
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//数据输入流
public InputStream getInputStream() {
InputStream inputStream = null;
HttpURLConnection httpConnection = null;
try {
URL url = new URL(path);
if (url != null) {
httpConnection = (HttpURLConnection) url.openConnection();
// 设置连接网络时间超时
httpConnection.setConnectTimeout(3000);
//设置为true表示能够用URL连接进行输入
httpConnection.setDoInput(true);
httpConnection.setRequestMethod("GET");
int requestCode = httpConnection.getResponseCode();
if (200 == requestCode) {
// 从服务器端获取输入流
inputStream = httpConnection.getInputStream();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}
InputStream inputStream = getInputStream();
byte[] data = new byte[1024];
int len = 0;
FileOutputStream outputStream = null;
try {
// 读取后台数据存储位置
String fileName = "";
outputStream = new FileOutputStream(fileName);
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//数据输入流
public InputStream getInputStream() {
InputStream inputStream = null;
HttpURLConnection httpConnection = null;
try {
URL url = new URL(path);
if (url != null) {
httpConnection = (HttpURLConnection) url.openConnection();
// 设置连接网络时间超时
httpConnection.setConnectTimeout(3000);
//设置为true表示能够用URL连接进行输入
httpConnection.setDoInput(true);
httpConnection.setRequestMethod("GET");
int requestCode = httpConnection.getResponseCode();
if (200 == requestCode) {
// 从服务器端获取输入流
inputStream = httpConnection.getInputStream();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inputStream;
}