ftp://ygdy8:ygdy8@y201.dygod.org:1234/[阳光电影www.ygdy8.com].神探驾到.BD.720p.国粤双语中字.mkv
这样的一个地址,如何用java下载这个电影呢?有牛人会弄过这个么?求赐教!
java ftp下载
答案:1 悬赏:70 手机版
解决时间 2021-02-14 12:42
- 提问者网友:辞取
- 2021-02-13 14:38
最佳答案
- 五星知识达人网友:几近狂妄
- 2021-02-13 15:35
这个和ftp没有太大关系,只是一个普通的下载,java连接ftp服务器传输文件是需要提供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 < 400) {
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;
}
}
这边用到了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 < 400) {
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;
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯