集册 Java实例教程 从URL下载并设置连接属性

从URL下载并设置连接属性

欢马劈雪     最近更新时间:2020-01-02 10:19:05

509
从URL下载并设置连接属性

/*
时   代     Java  公  众  号 - nowjava.com 提供
*/

//package com.nowjava;


import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;


public class Main {

    public static void main(String[] argv) throws Exception {

        String URL = "nowjava.com";

        long nPos = 2;

        String savePathAndFile = "nowjava.com";

        down(URL, nPos, savePathAndFile);

    }


    public static void down(String URL, long nPos, String savePathAndFile) {

        try {

            URL url = new URL(URL);
            /** 
            来 自 
            n o w j a v a . c o m
            **/

            HttpURLConnection httpConnection = (HttpURLConnection) url

                    .openConnection();

            // User-Agent

            httpConnection.setRequestProperty("User-Agent", "NetFox");

            // 

            httpConnection.setRequestProperty("RANGE", "bytes=" + nPos);

            // 

            InputStream input = httpConnection.getInputStream();

            RandomAccessFile oSavedFile = new RandomAccessFile(

                    savePathAndFile, "rw");

            // nPos?

            oSavedFile.seek(nPos);

            byte[] b = new byte[1024];

            int nRe
展开阅读全文