导入的包都是java.io中的
/**
* 下载远程图片
* @param remPicURL 远程图片文件路径
* @param savePath 本地保存路径
*/
public static Map<String, Object> download(String remPicURL ,String savePath){
Map<String, Object> res = new HashMap<String, Object>();
String code = "1";
String msg = "下载成功:图片存放在:C://";
// 构造URL
InputStream is = null;
OutputStream os = null;
try {
java.net.URL url = new java.net.URL(remPicURL);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
os = new FileOutputStream(savePath);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
code = "0";
msg = "下载失败";
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
res.put("code", code);
res.put("msg", msg);
return res;
}