java 下载文件

根据url下载文件

public static String downloadFile(String urlsrc, String filename, String filePath, String cookie) {
		// 输入流
		InputStream in = null;
		// 文件输出流
		FileOutputStream out = null;
		try{
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,5000); //设置连接超时为5秒
			HttpClient client = new DefaultHttpClient(httpParams); // 生成一个http客户端发送请求对象
			HttpGet httpget1 = new HttpGet(urlsrc); //对查询页面get
			HttpResponse httpResponse1 = client.execute(httpget1); // 发送请求并等待响应
			// 判断网络连接是否成功
			System.out.println("状态码:"+httpResponse1.getStatusLine().getStatusCode());
			if (httpResponse1.getStatusLine().getStatusCode() != 200)
				System.out.println("网络错误异常!!!!");
			else
				System.out.println("网络连接成功!!!");
			httpget1.abort(); //关闭get
			HttpGet httpget2 = new HttpGet(urlsrc); //对下载链接get实现下载
			// 在flipkart中需要cookie才能下载模板
			if (StringUtils.isNotBlank(cookie)){
				httpget2.addHeader("Cookie",cookie);
			}
			HttpResponse httpResponse2 = client.execute(httpget2);
			HttpEntity entity = httpResponse2.getEntity(); // 获取响应里面的内容
			in = entity.getContent(); // 得到服务端发回的响应的内容(都在一个流里面)
			File savePath = new File(filePath);
			if (!savePath.exists()) {
				// 如果不存在当前文件夹,则创建该文件夹
				boolean mkdir = savePath.mkdirs();
				if (!mkdir) {
					System.out.println("创建文件夹失败");
				}
			}
			out = new FileOutputStream(new File(filePath + File.separator + filename));
			byte[] b = new byte[1024];
			int len = 0;
			while((len=in.read(b))!= -1){
				out.write(b,0,len);
			}
			in.close();
			out.close();
		}catch(Exception e){
			e.printStackTrace();
		}
    System.out.println("download, success!!");
		return filePath + File.separator + filename;
	}

根据url下载图片

private String downloadImg(String imgUrl, String filename, String filePath) {
		URL url = null;
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			url = new URL(imgUrl);
			// 打开连接
			URLConnection con = url.openConnection();
			// 请求超时:5s
			con.setConnectTimeout(5 * 1000);
			inputStream = con.getInputStream();

			byte[] bytes = new byte[1024];
			// 读取到的数据长度
			int length;
			if (System.getProperty("os.name").toLowerCase().contains("windows")){
				// Windows系统
			}else if (System.getProperty("os.name").toLowerCase().contains("linux")){
				// Linux系统
			}
			File savePath = new File(filePath);
			if (!savePath.exists()) {
				// 如果不存在当前文件夹,则创建该文件夹
				boolean mkdir = savePath.mkdirs();
				if (!mkdir) {
					System.out.println("创建文件夹失败");
					return null;
				}
			}
			outputStream = new FileOutputStream(savePath.getPath() + "\\" + filename);
			// 读取
			while ((length = inputStream.read(bytes)) != -1) {
				outputStream.write(bytes, 0, length);
			}
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return null;
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return filePath + File.separator + filename;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值