java ftp byte下载_java ftp下载

展开全部

这个和ftp没有太大关系,只是一个普通的下载,java连接ftp服务器32313133353236313431303231363533e58685e5aeb931333337383837传输文件是需要提供ip 端口号,用户名密码 路径的,这个只是一个静态资源,用这个就可以,支持断点续传

这边用到了apache commons-httpclient-3.1包import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.RandomAccessFile;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")

public class DownloadTool {

public static void main(String[] args) throws ClientProtocolException, IOException {

String url = "

[阳光电影

].神探驾到.BD.720p.国粤双语中字.mkv";

String downFile = "d:\\aaa.mkv";//本地存放路径

Long netFileLenght = getNetFileSize(url);

Long localFileLenght = getLocalFileSize(downFile);

if (localFileLenght >= netFileLenght) {

System.out.println("已下载完成");

return;

}

System.out.println("netFileLenght : " + netFileLenght + " localFileLenght : " + localFileLenght);

final HttpClient httpClient = new DefaultHttpClient();

httpClient.getParams().setIntParameter("http.socket.timeout", 5000);

final HttpGet httpGet = new HttpGet(url);

httpGet.addHeader("Range", "bytes=" + localFileLenght + "-");

final HttpResponse response = httpClient.execute(httpGet);

final int code = response.getStatusLine().getStatusCode();

final HttpEntity entity = response.getEntity();

System.out.println(code);

if (entity != null && code 

File file = new File(downFile);

RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

randomAccessFile.seek(localFileLenght);

InputStream inputStream = entity.getContent();

int b = 0;

final byte buffer[] = new byte[1024];

while ((b = inputStream.read(buffer)) != -1) {

randomAccessFile.write(buffer, 0, b);

}

randomAccessFile.close();

inputStream.close();

httpClient.getConnectionManager().shutdown();

System.out.println("下载完成");

}

}

public static Long getLocalFileSize(String fileName) {

File file = new File(fileName);

return file.length();

}

public static Long getNetFileSize(String url) {

Long count = -1L;

final HttpClient httpClient = new DefaultHttpClient();

httpClient.getParams().setIntParameter("http.socket.timeout", 5000);

final HttpGet httpGet = new HttpGet(url);

HttpResponse response = null;

try {

response = httpClient.execute(httpGet);

final int code = response.getStatusLine().getStatusCode();

final HttpEntity entity = response.getEntity();

if (entity != null && code == 200) {

count = entity.getContentLength();

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

httpClient.getConnectionManager().shutdown();

}

return count;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值