java http 下载_HTTP文件下载JAVA后台的实现

本文介绍了使用JAVA进行HTTP文件下载的多种方法,包括使用org.apache.http.impl.client.CloseableHttpClient、curl命令行、Servlet文件下载以及RestTemplate下载。通过示例代码详细展示了如何实现文件的下载和保存,并提供了设置HTTP头部信息的示例。
摘要由CSDN通过智能技术生成

HTTP文件的下载后台JAVA代码

1、使用org.apache.http.impl.client.CloseableHttpClient

先上代码:

public String downloadFile(String src_file, String dest_file) throwsThrowable {

String fileName=getFileName(src_file);try (CloseableHttpClient httpclient =HttpClients.createDefault()) {

HttpGet httpget= newHttpGet(src_file);

httpget.setConfig(RequestConfig.custom()// .setConnectionRequestTimeout(downloadTimeout) // .setConnectTimeout(downloadTimeout) // .setSocketTimeout(downloadTimeout) //.build());try (CloseableHttpResponse response =httpclient.execute(httpget)) {

org.apache.http.HttpEntity entity=response.getEntity();

File desc= new File(dest_file+File.separator+fileName);

File folder=desc.getParentFile();

folder.mkdirs();try (InputStream is = entity.getContent(); // OutputStream os = newFileOutputStream(desc)) {

StreamUtils.copy(is, os);

}

}catch(Throwable e){throw new Throwable("文件下载失败......", e);

}

}return dest_file+File.separator+fileName;

}

另外:添加header代码如下:httpget.addHeader("X-Auth-Token",token);

2、使用curl:

windows系统中使用需要下载CURL,下载地址:https://curl.haxx.se/download.html 选择windows版;

bb5c912f0a45514ace2911f15cf69055.png

使用命令行下载文件java代码:

packagecom.test.download;importjava.io.IOException;public classTestDownload {public static voidmain(String[] args) {

String curlPath= "D:\\curl\\I386\\CURL.EXE";

String destPath= "D:\\2.jpg";

String fileUrl= "http://i0.hdslb.com/bfs/archive/5a08e413f479508ab78bb562ac81f40ad28a4245.jpg";

dowloadFile(curlPath,destPath,fileUrl);

}private static voiddowloadFile(String curlPath ,String filePath, String url) {long start =System.currentTimeMillis();

String token= "12345678901234567890";

System.out.println("执行命令==="+curlPath + " -o "+ filePath +" \"" + url +"\""

+ " -H \"X-Auth-Token:"+token+"\" -X GET");try{

Runtime.getRuntime().exec(curlPath+ " -o "+ filePath +" \"" + url +"\""

+ " -H \"X-Auth-Token:"+token+"\" -X GET");

}catch(IOException e) {

e.printStackTrace();

}

System.out.println("下载成功,耗时(ms):"+(System.currentTimeMillis()-start));

}

}

具体的CURL命令行使用可以看帮助:curl -h

3、Servlet文件下载:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public void downloadNet(HttpServletResponse response) throwsMalformedURLException {int bytesum = 0;int byteread = 0;

URL url= new URL("http://img.baidu.com/logo.gif");try{

URLConnection conn=url.openConnection();

InputStream inStream=conn.getInputStream();

FileOutputStream fs= new FileOutputStream("c:/abc.gif");byte[] buffer = new byte[1204];intlength;while ((byteread = inStream.read(buffer)) != -1) {

bytesum+=byteread;

System.out.println(bytesum);

fs.write(buffer,0, byteread);

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throwsException {

File f= newFile(filePath);if (!f.exists()) {

response.sendError(404, "File not found!");return;

}

BufferedInputStream br= new BufferedInputStream(newFileInputStream(f));byte[] buf = new byte[1024];int len = 0;

response.reset();//非常重要

if (isOnLine) { //在线打开方式

URL u = new URL("file:///" +filePath);

response.setContentType(u.openConnection().getContentType());

response.setHeader("Content-Disposition", "inline; filename=" +f.getName());//文件名应该编码成UTF-8

} else { //纯下载方式

response.setContentType("application/x-msdownload");

response.setHeader("Content-Disposition", "attachment; filename=" +f.getName());

}

OutputStream out=response.getOutputStream();while ((len = br.read(buf)) > 0)

out.write(buf,0, len);

br.close();

out.close();

}

download

4、添加两个文件复制的代码:

private static voidnioTransferCopy(File source, File target) {

FileChannel in= null;

FileChannel out= null;

FileInputStream inStream= null;

FileOutputStream outStream= null;try{

inStream= newFileInputStream(source);

outStream= newFileOutputStream(target);

in=inStream.getChannel();

out=outStream.getChannel();

in.transferTo(0, in.size(), out);

}catch(IOException e) {

e.printStackTrace();

}finally{

close(inStream);

close(in);

close(outStream);

close(out);

}

}private static voidnioBufferCopy(File source, File target) {

FileChannel in= null;

FileChannel out= null;

FileInputStream inStream= null;

FileOutputStream outStream= null;try{

inStream= newFileInputStream(source);

outStream= newFileOutputStream(target);

in=inStream.getChannel();

out=outStream.getChannel();

ByteBuffer buffer= ByteBuffer.allocate(4096);while (in.read(buffer) != -1) {

buffer.flip();

out.write(buffer);

buffer.clear();

}

}catch(IOException e) {

e.printStackTrace();

}finally{

close(inStream);

close(in);

close(outStream);

close(out);

}

}

添加一个文件下载的RestTemplate的实现:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public voiddownloadLittleFileToPath(String url, String target) {

Instant now=Instant.now();

RestTemplate template= newRestTemplate();

ClientHttpRequestFactory clientFactory= newHttpComponentsClientHttpRequestFactory();

template.setRequestFactory(clientFactory);

HttpHeaders header= newHttpHeaders();

List list = new ArrayList();//指定下载文件类型

list.add(MediaType.APPLICATION_OCTET_STREAM);

header.setAccept(list);

HttpEntity request = new HttpEntity(header);

ResponseEntity rsp = template.exchange(url, HttpMethod.GET, request, byte[].class);

logger.info("[下载文件] [状态码] code:{}", rsp.getStatusCode());try{

Files.write(Paths.get(target), Objects.requireNonNull(rsp.getBody(), "未获取到下载文件"));

}catch(IOException e) {

logger.error("[下载文件] 写入失败:", e);

}

logger.info("[下载文件] 完成,耗时:{}", ChronoUnit.MILLIS.between(now, Instant.now()));

}public voiddownloadBigFileToPath(String url, String target) {

Instant now=Instant.now();try{

RestTemplate template= newRestTemplate();

ClientHttpRequestFactory clientFactory= newHttpComponentsClientHttpRequestFactory();

template.setRequestFactory(clientFactory);//定义请求头的接收类型

RequestCallback requestCallback = request ->request.getHeaders()

.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));//getForObject会将所有返回直接放到内存中,使用流来替代这个操作

ResponseExtractor responseExtractor = response ->{//Here I write the response to a file but do what you like

Files.copy(response.getBody(), Paths.get(target));return null;

};

template.execute(url, HttpMethod.GET, requestCallback, responseExtractor);

}catch(Throwable e) {

logger.error("[下载文件] 写入失败:", e);

}

logger.info("[下载文件] 完成,耗时:{}", ChronoUnit.MILLIS.between(now, Instant.now()));

}

RestTemplate下载文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值