断点下载java实例

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;
import java.util.Queue;
import java.util.TimeZone;

import javax.crypto.Mac;
import javax.crypto.SecretKey;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;

import javax.crypto.spec.SecretKeySpec;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import Decoder.BASE64Encoder;

public class Authentication {

	private static final String UTF8_CHARSET = "UTF-8";
	private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
	public String endpoint = "http://10.192.52.167:7480";
	public String awsAccessKeyId = "HY3KI49WG8ETFEF7LAOM";
	public String awsSecretKey = "3AsKaGxfVrkCmrcmk5V9BHDP9rFzR3eNWlr5eKFE";
	public String DEL_METHOD = "DELETE";
	public String PUT_METHOD = "PUT";
	public String HEAD_METHOD = "HEAD";
	public String GET_METHOD = "GET";
	public String POST_METHOD = "POST";
	public String GET_ACL = "?acl";
	public String SET_ACL = "?acl";
	public String Etag = null;
	int object_size = 0;

	String timestamp() {
		String timestamp = null;
		Calendar cal = Calendar.getInstance();
		DateFormat dfm = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss '+0000'", Locale.ENGLISH);
		dfm.setTimeZone(TimeZone.getTimeZone("GMT"));
		timestamp = dfm.format(cal.getTime());
		return timestamp;
	}

	String get_range(String offset, String len) {
		return (offset + "-" + Integer.toString((Integer.parseInt(offset) + Integer.parseInt(len) - 1)));
	}

	public void combination_file(Element root, String path, String objectname) {
		InputStream in = null;
		OutputStream out = null;
		byte[] bytes = new byte[2048];
		File f = new File(path + objectname);
		f.delete();
		String range = null;
		String file_path = null;

		try {
			out = new FileOutputStream(path + objectname, true);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		Iterator<?> ruleNodes = root.elementIterator("Part");
		while (ruleNodes.hasNext()) {
			Element ruleElement = (Element) ruleNodes.next();
			if (!(ruleElement.element("is_ok").getStringValue().equals("true"))) {
				
				continue;
			}
			
			range = get_range(ruleElement.element("offset").getStringValue(),
					ruleElement.element("lenth").getStringValue());
			file_path = path + range + "-" + objectname;

			try {
				in = new FileInputStream(file_path);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			int n = -1;
			try {
				while ((n = in.read(bytes, 0, bytes.length)) != -1) {
					try {
						out.write(bytes, 0, n);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} catch (UnsupportedEncodingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			f = new File(file_path);
			f.delete();
		}

		try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	void write_xml_to_file(String path, Document document) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(path);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		OutputFormat format = OutputFormat.createPrettyPrint();
		// 1.创建写出对象
		XMLWriter writer = null;
		try {
			writer = new XMLWriter(out, format);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 2.写出Document对象
		try {
			writer.write(document);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 3.关闭流
		try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	int getfile_size(String path) {
		File file = new File(path);
		if (file.exists() && file.isFile()) {
			return (int) file.length();
		} else {
			return -1;
		}
	}

	String to_utf8(String stringToSign) {
		String out = null;

		try {
			out = URLEncoder.encode(stringToSign, UTF8_CHARSET);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		return out;
	}

	String calculateRFC2104HMAC(String data, String key) {
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

		Mac mac = null;
		try {
			mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		try {
			mac.init(signingKey);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}

		byte[] rawHmac = mac.doFinal(data.getBytes());

		BASE64Encoder BASE64Encoder = new BASE64Encoder();
		return BASE64Encoder.encode(rawHmac);
	}

	byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
		byte[] data = encryptKey.getBytes(UTF8_CHARSET);
		SecretKey secretKey = new SecretKeySpec(data, HMAC_SHA1_ALGORITHM);
		Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
		mac.init(secretKey);

		byte[] text = encryptText.getBytes(UTF8_CHARSET);
		return mac.doFinal(text);
	}

	byte[] hmacSHA1(String data, String key) throws java.security.SignatureException {
		try {
			SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

			Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
			mac.init(signingKey);

			byte[] rawHmac = mac.doFinal(data.getBytes());

			return rawHmac;

		} catch (Exception e) {
			throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
		}
	}

	HttpURLConnection connectEndpoint(String get_url, String method, String Authorization, String ti) {
		URL url = null;
		HttpURLConnection connection = null;

		try {
			url = new URL(get_url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		try {
			connection = (HttpURLConnection) url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			connection.setRequestMethod(method);
		} catch (ProtocolException e) {
			e.printStackTrace();
		}

		connection.addRequestProperty("Authorization", Authorization);
		connection.addRequestProperty("x-amz-date", ti);
		connection.setDoInput(true);
		connection.setDoOutput(true);
		try {
			connection.connect();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return connection;
	}

	HttpURLConnection connectEndpoint(String get_url, String method, String Authorization, String ti, String range) {
		URL url = null;
		HttpURLConnection connection = null;

		try {
			url = new URL(get_url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		try {
			connection = (HttpURLConnection) url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			connection.setRequestMethod(method);
		} catch (ProtocolException e) {
			e.printStackTrace();
		}

		connection.addRequestProperty("Authorization", Authorization);
		connection.addRequestProperty("x-amz-date", ti);
		String tmp = "bytes=" + range;
		connection.addRequestProperty("Range", tmp);
		connection.setDoInput(true);
		connection.setDoOutput(true);
		try {
			connection.connect();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return connection;
	}

	void downloadObjectPart(String get_url, String method, String Authorization, String ti, String path, String range) {
		HttpURLConnection connection = null;
		InputStream is = null;
		FileOutputStream fos = null;
		int len = 0;
		byte[] buff = new byte[8 * 1024];

		URL url = null;

		connection = connectEndpoint(get_url, method, Authorization, ti, range);
		try {
			url = new URL(get_url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		try {
			connection = (HttpURLConnection) url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			connection.setRequestMethod(method);
		} catch (ProtocolException e) {
			e.printStackTrace();
		}

		connection.addRequestProperty("Authorization", Authorization);
		connection.addRequestProperty("x-amz-date", ti);
		String tmp = "bytes=" + range;
		connection.addRequestProperty("Range", tmp);

		try {
			connection.connect();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			fos = new FileOutputStream(new File(path), false);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			is = connection.getInputStream();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			while ((len = is.read(buff)) != -1) {
				fos.write(buff, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		getConnectionRes(connection);

		try {
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		connection.disconnect();

		return;
	}

	void downloadObject(String get_url, String method, String Authorization, String ti, String path) {
		HttpURLConnection connection = null;
		InputStream is = null;
		FileOutputStream fos = null;
		int len = 0;
		byte[] buff = new byte[8 * 1024];

		URL url = null;

		connection = connectEndpoint(get_url, method, Authorization, ti);
		try {
			url = new URL(get_url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		try {
			connection = (HttpURLConnection) url.openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			connection.setRequestMethod(method);
		} catch (ProtocolException e) {
			e.printStackTrace();
		}

		connection.addRequestProperty("Authorization", Authorization);
		connection.addRequestProperty("x-amz-date", ti);

		try {
			connection.connect();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			fos = new FileOutputStream(new File(path), false);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		try {
			is = connection.getInputStream();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			while ((len = is.read(buff)) != -1) {
				fos.write(buff, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		getConnectionRes(connection);

		try {
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		connection.disconnect();

		return;
	}

	/*
	 * private static String MD5(byte[] sourceStr) throws Exception {
	 * MessageDigest mdInst = MessageDigest.getInstance("MD5");
	 * mdInst.update(sourceStr); byte[] md = mdInst.digest(); StringBuffer buf =
	 * new StringBuffer(); for (int i = 0; i < md.length; i++) { int tmp =
	 * md[i]; if (tmp < 0) tmp += 256; if (tmp < 16) buf.append("0");
	 * buf.append(Integer.toHexString(tmp)); } return buf.toString(); }
	 */
	public static String MD5(String path) {
		FileInputStream fis = null;
		File f = new File(path);
		try {
			fis = new FileInputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		int size = 0;
		try {
			size = fis.available();
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] buffer = new byte[size];
		try {
			fis.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		MessageDigest mdInst = null;
		try {
			mdInst = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		mdInst.update(buffer);
		byte[] md = mdInst.digest();
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < md.length; i++) {
			int tmp = md[i];
			if (tmp < 0)
				tmp += 256;
			if (tmp < 16)
				buf.append("0");
			buf.append(Integer.toHexString(tmp));
		}
		return buf.toString();
	}

	public String parse_xml(String path) {
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document = reader.read(new File(path));
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}

		Element root = document.getRootElement();

		Element conElem = root.element("UploadId");
		return conElem.getText();
	}

	int multiple_put(String get_url, String method, String Authorization, String ti, byte[] buffer, int size) {
		HttpURLConnection connection = null;
		OutputStream outputStream = null;
		String etag = null;
		int status = 0;

		// System.out.println(get_url);
		connection = connectEndpoint(get_url, method, Authorization, ti);

		try {
			outputStream = connection.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outputStream.write(buffer, 0, size);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		etag = connection.getHeaderField("ETag");
		Etag = etag;
		InputStream errorInputStream = null;
		try {
			try {
				status = connection.getResponseCode();
				if (status >= 400) {
					errorInputStream = connection.getErrorStream();
					throw new Exception("xxxxxxxxx ");
				}

			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} finally {
			if (errorInputStream != null) {
				try {
					errorInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			connection.disconnect();
		}

		try {
			System.out.println(connection.getResponseCode());
		} catch (IOException e) {
			e.printStackTrace();
		}

		return status;
	}

	public void createXML(String part_num, String etag, Element root) {
		Element part = root.addElement("Part");
		part.addElement("PartNumber").addText(part_num);
		part.addElement("ETag").addText(etag);
	}

	public void createXML(String offset, String len, String is_ok, Element root) {
		Element part = root.addElement("Part");
		part.addElement("offset").addText(offset);
		part.addElement("lenth").addText(len);
		part.addElement("is_ok").addText(is_ok);
	}

	String putobject(String get_url, String method, String Authorization, String ti, String path) {
		HttpURLConnection connection = null;
		FileInputStream fis = null;
		File f = null;
		OutputStream outputStream = null;

		connection = connectEndpoint(get_url, method, Authorization, ti);

		f = new File(path);
		try {
			fis = new FileInputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		int size = 0;
		try {
			size = fis.available();
		} catch (IOException e) {
			e.printStackTrace();
		}
		byte[] buffer = new byte[size];
		try {
			fis.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			outputStream = connection.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outputStream.write(buffer, 0, size);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		getConnectionRes(connection);

		try {
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		connection.disconnect();
		return null;
	}

	void getConnectionRes(HttpURLConnection connection) {
		InputStream errorInputStream = null;
		try {
			try {
				if (connection.getResponseCode() >= 400) {
					errorInputStream = connection.getErrorStream();
					throw new Exception("connect err ");
				}

			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} finally {
			if (errorInputStream != null) {
				try {
					errorInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

		try {
			System.out.println(connection.getResponseCode());
			object_size = connection.getContentLength();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	String putMethodBody(String get_url, String method, String Authorization, String ti, String body) {
		HttpURLConnection connection = null;
		OutputStream outputStream = null;

		connection = connectEndpoint(get_url, method, Authorization, ti);

		try {
			outputStream = connection.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outputStream.write(body.getBytes(), 0, body.length());
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			outputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		getConnectionRes(connection);

		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		connection.disconnect();

		return null;
	}

	String putMethod(String get_url, String method, String Authorization, String ti) {
		HttpURLConnection connection = null;

		connection = connectEndpoint(get_url, method, Authorization, ti);
		BufferedReader br = null;

		try {
			br = new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF8_CHARSET));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String line;
		StringBuilder sb = new StringBuilder();
		try {
			while ((line = br.readLine()) != null) {
				sb.append(line);
				sb.append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		getConnectionRes(connection);

		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		connection.disconnect();
		return sb.toString();

	}

}


























import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class get_object_part {
	private static String bucket_name = "/d_bucket/";
	private static String object_name = "20180831test.mp4";
	private static String GET_URL = null;
	private static String path = "F:/test/";
	private static String xml_path = "F:/test/" + object_name + "_map.xml";
	static int len = 100 * 1024 * 1024;

	public static void main(String[] args) {
		Authentication auth = new Authentication();
		String ti = auth.timestamp();
		String toSign = null;
		int i = 0;
		int size = 0;
		object_name = auth.to_utf8(object_name);
		toSign = auth.HEAD_METHOD + "\n" + "\n" + "\n" + "\n" + "x-amz-date:" + ti + "\n" + bucket_name + object_name;

		String sign = auth.calculateRFC2104HMAC(toSign, auth.awsSecretKey);
		String id = "AWS" + " " + auth.awsAccessKeyId + ":" + sign;

		GET_URL = auth.endpoint + bucket_name + object_name;
		// 获取到object的size
		auth.putMethod(GET_URL, auth.HEAD_METHOD, id, ti);
		size = auth.object_size;

		File file = new File(xml_path);
		Document document = null;
		Element root = null;
		if (!file.exists()) {
			document = DocumentHelper.createDocument();
			root = document.addElement("download");

			while (len <= size) {
				auth.createXML(Integer.toString(i * len), Integer.toString(len), "false", root);
				size = size - len;
				i++;
			}
			if (size > 0) {
				auth.createXML(Integer.toString(i * len), Integer.toString(size), "false", root);
			}
			auth.write_xml_to_file(xml_path, document);
		}

		// open file
		try {
			document = new SAXReader().read(new File(xml_path));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String range = null;
		Queue<String> queue = new LinkedList<>();

		root = document.getRootElement();
		Element root1 = root;
		Iterator<?> ruleNodes = root.elementIterator("Part");
		while (ruleNodes.hasNext()) {
			Element ruleElement = (Element) ruleNodes.next();

			if (ruleElement.element("is_ok").getStringValue().equals("true")) {
				continue;
			}

			range = auth.get_range(ruleElement.element("offset").getStringValue(), ruleElement.element("lenth").getStringValue());
			//System.out.println("range:" + range);
			ti = auth.timestamp();

			object_name = auth.to_utf8(object_name);
			toSign = auth.GET_METHOD + "\n" + "\n" + "\n" + "\n" + "x-amz-date:" + ti + "\n" + bucket_name
					+ object_name;

			sign = auth.calculateRFC2104HMAC(toSign, auth.awsSecretKey);
			id = "AWS" + " " + auth.awsAccessKeyId + ":" + sign;

			GET_URL = auth.endpoint + bucket_name + object_name;

			String path_tmp = path  + range + "-" + object_name;
			// System.out.printl1n(path_tmp);
			queue.offer(path_tmp);
			auth.downloadObjectPart(GET_URL, auth.GET_METHOD, id, ti, path_tmp, range);

			ruleElement.element("is_ok").setText("true");
			auth.write_xml_to_file(xml_path, document);
		}

		float startTime=System.nanoTime();
		auth.combination_file(root1, path, object_name);
		System.out.println(System.nanoTime() - startTime);
		file.delete();
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值