资讯专栏INFORMATION COLUMN

java写的Http服务器下载工具

wangtdgoodluck / 3236人阅读

摘要:这个工具比较简单,用于配合另外一个工具进行文件传送废话少说,上代码这个工具实现了从服务器上下载指定行数的文件,并且不会因为编码的问题引起下载的文件内容乱码三个工具已经搞定,下一次就是把这三个工具结合起来将的文件转移到上工具工具

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

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] == "
"){
                        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工具

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64286.html

相关文章

  • 一、python与pycharm的安装

    摘要:是面向对象语言这意味着支持面向对象的风格或代码封装在对象的编程技术。在上执行命令,就可以进入到的交互模式,并显示出版本等信息。选择的版本,需要下载安装包,然后进行安装。 一、Python简介 Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节。类似于PHP和Perl语言。 Python 是交互式语言: 这意味着,您可以在一个Python提示符,直接互动执行写你的程...

    awokezhou 评论0 收藏0
  • 实现一个spring webservice服务端三:实现一个有复杂返回值的spring-ws服务

    摘要:,将类或枚举类型映射到模式类型,控制字段或属性的序列化。表示将自动绑定类中的每个非静态的非瞬态的由标注字段到。,对于数组或集合即包含多个元素的成员变量,生成一个包装该数组或集合的元素称为包装器。 在经过前面两篇文章的学习,我已经能够熟练创建一个正常运行的spring-ws的webservice服务,大多数接口,都是要有返回数据,所以这篇文章就是学习spring-ws怎么实现返回数据 实...

    xinhaip 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<