/**
* 复制图片
* @param start
* @param end
* @return
*/
public static boolean Copy(String start,String end)
{
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(start);
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置网络连接超时时间
httpURLConnection.setConnectTimeout(3000);
// 设置应用程序要从网络连接读取数据
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
}
//要拷贝的图片
BufferedInputStream bis = new BufferedInputStream(inputStream);
//目标的地址
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(end)));
try {
int val = -1;
while((val=bis.read())!=-1){
bos.write(val);
}
bos.flush();
bos.close();
bis.close();
return true;
} catch (IOException e) {
e.printStackTrace(); }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
将服务器中的图片复制到本地
最新推荐文章于 2024-03-17 20:35:51 发布