从网络上进行下载图片,并保存到本地

/**
	 * 从网络上进行下载图片,并保存到本地
	 * @param fromUrl 从哪下载的图片的地址 
	 * @param toPath 保存的位置
	 * @return true:下载保存成功; false:下载保存失败
	 * @author Saindy
	 */
	public static boolean downloadImage(String fromUrl, String toPath){
		try {
			URL url = new URL(fromUrl);
			File outFile = new File(toPath);
			OutputStream os = new FileOutputStream(outFile);
			InputStream is = url.openStream();
			byte[] buff = new byte[1024];
			while(true) {
				int readed = is.read(buff);
				if(readed == -1) {
					break;
				}
				byte[] temp = new byte[readed];
				System.arraycopy(buff, 0, temp, 0, readed);
				os.write(temp);
			}
			is.close(); 
	        os.close();
	        return true;
		} catch (Exception e) {
			return false;
		}
	}


你可能感兴趣的:(从网络上进行下载图片,并保存到本地)