HTTP文件的下載後臺JAVA代碼html
一、使用org.apache.http.impl.client.CloseableHttpClientjava
先上代碼:apache
public String downloadFile(String src_file, String dest_file) throws Throwable { String fileName = getFileName(src_file); try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpget = new HttpGet(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 = new FileOutputStream(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); windows
二、使用curl:
app
windows系統中使用須要下載CURL,下載地址:https://curl.haxx.se/download.html 選擇windows版;curl
使用命令行下載文件java代碼:ide
package com.test.download; import java.io.IOException; public class TestDownload { public static void main(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 void dowloadFile(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 -hui
三、Servlet文件下載:編碼
public void downloadNet(HttpServletResponse response) throws MalformedURLException { 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]; int length; 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) throws Exception { File f = new File(filePath); if (!f.exists()) { response.sendError(404, "File not found!"); return; } BufferedInputStream br = new BufferedInputStream(new FileInputStream(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(); }
四、添加兩個文件複製的代碼:url
private static void nioTransferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(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 void nioBufferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(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的實現:
public void downloadLittleFileToPath(String url, String target) { Instant now = Instant.now(); RestTemplate template = new RestTemplate(); ClientHttpRequestFactory clientFactory = new HttpComponentsClientHttpRequestFactory(); template.setRequestFactory(clientFactory); HttpHeaders header = new HttpHeaders(); List<MediaType> list = new ArrayList<MediaType>(); // 指定下載文件類型 list.add(MediaType.APPLICATION_OCTET_STREAM); header.setAccept(list); HttpEntity<byte[]> request = new HttpEntity<byte[]>(header); ResponseEntity<byte[]> 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 void downloadBigFileToPath(String url, String target) { Instant now = Instant.now(); try { RestTemplate template = new RestTemplate(); ClientHttpRequestFactory clientFactory = new HttpComponentsClientHttpRequestFactory(); template.setRequestFactory(clientFactory); //定義請求頭的接收類型 RequestCallback requestCallback = request -> request.getHeaders() .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL)); // getForObject會將全部返回直接放到內存中,使用流來替代這個操做 ResponseExtractor<Void> 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())); }