资讯专栏INFORMATION COLUMN

java写的Http服务器下载工具

wangtdgoodluck / 3462人阅读

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

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

</>复制代码

  1. import java.net.URL;
  2. import java.net.URLConnection;
  3. import java.io.File;
  4. import java.io.InputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import org.apache.commons.io.FileUtils;
  9. public class HttpUtil{
  10. private String httppath = "";
  11. public void setHttpPath(String httppath){
  12. this.httppath = httppath;
  13. }
  14. public String getHttpPath(){
  15. return this.httppath;
  16. }
  17. public HttpUtil(String httppath){
  18. this.httppath = httppath;
  19. }
  20. public InputStream getStream(String url){
  21. InputStream inStream = null;
  22. try{
  23. URL httpurl = new URL(url);
  24. URLConnection conn = httpurl.openConnection();
  25. inStream = conn.getInputStream();
  26. }catch (Exception e){
  27. e.printStackTrace();
  28. return null;
  29. }
  30. return inStream;
  31. }
  32. public int downLoad(String url,String localName ,int lines) throws FileNotFoundException, IOException{
  33. FileOutputStream fos = null;
  34. InputStream inStream = null;
  35. int ret = 0;
  36. try{
  37. URL httpurl = new URL(url);
  38. URLConnection conn = httpurl.openConnection();
  39. inStream = conn.getInputStream();
  40. fos = new FileOutputStream(localName);
  41. byte[] b = new byte[102400];
  42. int j = 0;
  43. while(inStream.read(b) != -1 && lines > 0){
  44. for(int i = j; i < b.length; i++){
  45. if(b[i] == "
  46. "){
  47. fos.write(b, j, i - j + 1);
  48. lines--;
  49. if(lines <= 0){
  50. break;
  51. }
  52. j = i + 1;
  53. continue;
  54. }
  55. }
  56. }
  57. }catch (Exception e){
  58. e.printStackTrace();
  59. ret = -1;
  60. }finally {
  61. fos.close();
  62. inStream.close();
  63. return ret;
  64. }
  65. }
  66. public static void main(String[] args){
  67. String httppath = "";
  68. int lines = 0;
  69. String localName = "";
  70. try{
  71. httppath = args[0];
  72. localName = args[1];
  73. lines = Integer.parseInt(args[2]);
  74. }catch (Exception e){
  75. e.printStackTrace();
  76. return;
  77. }
  78. try{
  79. HttpUtil hu = new HttpUtil(httppath);
  80. hu.downLoad(hu.getHttpPath(),localName ,lines);
  81. }catch (Exception e){
  82. e.printStackTrace();
  83. }
  84. }
  85. }

这个工具实现了从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元查看
<