java编写Http服务器下载工具
作者:hebedich 时间:2021-11-08 08:07:38
这个工具比较简单,用于配合另外一个工具进行文件传送,废话少说,上代码
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,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java编程泛型限定代码分享
2023-11-09 17:46:32
Spring Boot conditional注解用法详解
2022-03-19 02:32:25
![](https://img.aspxhome.com/file/2023/5/61795_0s.png)
JAVA中实现原生的 socket 通信机制原理
2021-08-08 03:06:01
![](https://img.aspxhome.com/file/2023/4/60484_0s.png)
Java 如何将网络资源url转化为File文件
2023-05-28 09:23:37
java中TESTful架构原理分析
2022-03-02 21:12:10
![](https://img.aspxhome.com/file/2023/9/61659_0s.jpg)
java修改JFrame默认字体方式
2022-11-16 14:02:50
Java Spring开发环境搭建及简单入门示例教程
2021-08-17 07:36:28
Java基础知识之CharArrayReader流的使用
2023-02-12 10:40:29
![](https://img.aspxhome.com/file/2023/0/61010_0s.png)
Spark SQL的自定义函数UDF使用
2022-07-31 04:19:47
利用Flutter制作经典贪吃蛇游戏
2023-07-21 07:04:13
![](https://img.aspxhome.com/file/2023/4/84724_0s.png)
java实现轻量型http代理服务器示例
2021-10-05 18:14:20
浅析Java多线程同步synchronized
2023-05-20 15:52:29
Java 中 synchronized的用法详解(四种用法)
2022-03-11 08:55:05
Android application捕获崩溃异常怎么办
2023-09-26 11:07:12
OpenCV和C++实现图像的翻转(镜像)、平移、旋转、仿射与透视变换
2023-07-14 23:47:22
![](https://img.aspxhome.com/file/2023/3/95523_0s.jpg)
Java循环队列原理与用法详解
2023-11-13 20:05:36
![](https://img.aspxhome.com/file/2023/4/61574_0s.png)
你所不知道的Spring自动注入详解
2021-09-04 19:30:08
![](https://img.aspxhome.com/file/2023/8/60828_0s.png)
关于MVC与SpringMVC的介绍、区别、执行流程
2023-11-28 02:25:56
springboot读取application.yaml文件数据的方法
2023-09-06 05:29:24
![](https://img.aspxhome.com/file/2023/6/63086_0s.jpg)
springmvc不进入Controller导致404的问题
2023-03-18 01:29:20
![](https://img.aspxhome.com/file/2023/2/62102_0s.png)