java写的Http服务器下载工具

发布时间:2019-06-11 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了java写的Http服务器下载工具脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

这个工具比较简单,用于配合另外一个工具进行文件传送,废话少说,上代码

import java.net.URL;
import java.net.URLConnection;
import java.io.File;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.COMmons.io.FileUtils;

public class HttpUtil{
    PRivate String httppath = "";

    public void setHttpPath(String httppath){
        this.httppath = httppath;
    }

    public String getHttpPath(){
        return this.httppath;
    }

    public HttpUtil(String httppath){
        this.httppath = httppath;
    }

    public InputStream getStream(String url){
        InputStream inStream = null;
        try{
            URL httpurl = new URL(url);
            URLConnection conn = httpurl.oPEnConnection();
            inStream = conn.getInputStream();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return inStream;
    }

    public int downLoad(String url,String localName ,int lines) throws FileNotFoundException, IOException{
        FileOutputStream fos = null;
        InputStream inStream = null;
        int ret = 0;
        try{
            URL httpurl = new URL(url);
            URLConnection conn = httpurl.openConnection();
            inStream = conn.getInputStream();
            fos = new FileOutputStream(localName);
            byte[] b = new byte[102400];
            int j = 0;
            while(inStream.read(b) != -1 && lines > 0){
                for(int i = j; i < b.length; i++){
                    if(b[i] == 'n'){
                        fos.write(b, j, i - j + 1);
                        lines--;
                        if(lines <= 0){
                            break;
                        }
                        j = i + 1;
                        continue;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            ret = -1;
        }finally {
            fos.close();
            inStream.close();
            return ret;
        }
    }

    public static void main(String[] args){
        String httppath = "";
        int lines = 0;
        String localName = "";
        try{
            httppath = args[0];
            localName = args[1];
            lines = Integer.parseint(args[2]);
        }catch (Exception e){
            e.printStackTrace();
            return;
        }
        try{
            HttpUtil hu = new HttpUtil(httppath);
            hu.downLoad(hu.getHttpPath(),localName ,lines);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

这个工具实现了从HTTP服务器上下载指定行数的文件,并且不会因为编码的问题引起下载的文件内容乱码
三个工具已经搞定,下一次就是把这三个工具结合起来将HTTP、FTP的文件转移到HDFS上

hadoop工具
ftp工具

脚本宝典总结

以上是脚本宝典为你收集整理的java写的Http服务器下载工具全部内容,希望文章能够帮你解决java写的Http服务器下载工具所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。