基于http 和 https的文件下载
来源:互联网 发布:网络红歌2016 编辑:程序博客网 时间:2024/06/07 15:01
1.基于http下载
//url为文件在服务器上的详细路径 URL url = new URL("brook.com\Plugin_ngCUTE.xlsm"); 本地路径 File dist = new File("D:\\Plugin_ngCUTE.xlsm"); FileUtils.copyFile(url.openStream(), dist);public class FileUtils{ public static final void copyFile(final InputStream in, final File dst) throws IOException { final OutputStream out = new FileOutputStream(dst); copyFile(in, out); } public static void copyFile(final InputStream in, final OutputStream out) throws IOException { final byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }}
2.https
public static void downloadFile(String url, String path, String fileName) throws Exception { log.debug("path:" + path); log.debug("url:" + url); HttpClient client = null; try { client = getHttpClient(); HttpGet httpGet = getHttpGet(url, null, null); HttpResponse response = client.execute(httpGet); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { byte[] result = EntityUtils.toByteArray(response.getEntity()); BufferedOutputStream bw = null; try { File f = new File(path + "/" + fileName); if (!f.getParentFile().exists()) f.getParentFile().mkdirs(); bw = new BufferedOutputStream(new FileOutputStream(f)); bw.write(result); } catch (IOException e) { log.error("Error saving file,path=" + path + ",url=" + url, e); throw e; } finally { if (bw != null) bw.close(); } } else { StringBuffer errorMsg = new StringBuffer(); errorMsg.append("httpStatus:"); errorMsg.append(response.getStatusLine().getStatusCode()); errorMsg.append(response.getStatusLine().getReasonPhrase()); errorMsg.append(", Header: "); Header[] headers = response.getAllHeaders(); for (Header header : headers) { errorMsg.append(header.getName()); errorMsg.append(":"); errorMsg.append(header.getValue()); } log.error("HttpResonse Error:" + errorMsg); throw new HttpResponseException("HttpResonse Error:" + errorMsg); } } catch (ClientProtocolException e) { log.error("Download the files saved to the local,http Connection exception,path=" + path + ",url=" + url, e); throw e; } catch (IOException e) { log.error("Download the files saved to the local,File operations abnormal,path=" + path + ",url=" + url, e); throw e; } finally { client.getConnectionManager().shutdown(); } }public static HttpClient getHttpClient() throws Exception{ HttpClient httpclient = null; SSLContext sslContext; try { sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers ============="); return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { System.out.println("checkClientTrusted ============="); } public void checkServerTrusted(X509Certificate[] certs, String authType) { System.out.println("checkServerTrusted ============="); } } }, new SecureRandom()); SSLSocketFactory sf = new SSLSocketFactory(sslContext); Scheme httpsScheme = new Scheme(getProperties().getProperty(TYPE), new Integer(getProperties().getProperty(PORT)) , sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); // apache HttpClient version >4.2 should use // BasicClientConnectionManager ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry); httpclient = new DefaultHttpClient(cm); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } return httpclient; }
0 0
- 基于http 和 https的文件下载
- SpringMVC下http和https路径下的文件下载
- 文件下载支持http和https
- https下载文件http header 设置
- 解决Http或Https中下载中文文件乱码和无法下载问题
- wsdl文件生成webservice的http和https的问题
- wget用法wget是一个从网络上自动下载文件的自由工具。它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.
- 用java实现基于http协议的网络文件下载
- 用java实现基于http协议的网络文件下载
- Java实现基于http协议的网络文件下载
- 基于http协议的网络文件下载原理
- Android实现基于http协议的文件下载
- Java实现基于http协议的网络文件下载
- 基于Twisted的通过HTTP协议下载文件
- 基于HTTP的单线程文件下载功能实现
- 基于HTTP的多线程文件下载功能实现
- 【Netty基础】基于HTTP的文件下载Server实例
- Tomcat配置https与http自动跳转和tomcat单双向SSL配置及IE访问HTTPS下载文件失败
- Unity3d面试6 DontDestroyOnLoad();PlayerPrefs;协同程序;游戏框架;Unity中播放视频;LOD技术
- 【Core Java Volume 6】集合算法--二分查找法
- IOS - UIBezierPath精讲
- CUDA(1)
- 使用OpenSearch为应用提供搜索功能
- 基于http 和 https的文件下载
- nmg_common_lighting.glinc
- 开发模式及框架梳理
- 新浪微博分享以及回调可能会遇到的一些问题
- 平面坐标转大地坐标(经纬度)
- Sqlite3数据库用法总结:
- mysql 开启root用户远程连接
- 2016 是时候结束校招了!
- string format 的用法