- 浏览: 187273 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (83)
- J2EE/Core Java (24)
- J2EE/Portal (2)
- J2EE/UI (4)
- J2EE/ATG (1)
- J2EE/Report (1)
- J2EE/Web Service/Rest API (2)
- Design Pattern (2)
- Arithmetic (4)
- Linux (12)
- Ruby&Rails (17)
- Database (5)
- J2EE/Payment (1)
- J2EE/JVM (1)
- Encryption/Decryption (3)
- J2EE/Multi Threading (4)
- SQL (1)
- https://community.teamviewer.com/t5/Knowledge-Base/Where-can-I-download-older-TeamViewer-versions-nbsp/ta-p/7729 (0)
最新评论
public class HttpDownloadUtility { private static final Logger logger = LoggerFactory.getLogger(HttpDownloadUtility.class); private static final int BUFFER_SIZE = 4096; private static final int END_OF_FILE = -1; public static boolean downloadFile(String fileURL, File saveDir) { return downloadFile(fileURL, saveDir, null); } public static boolean downloadFile(String fileURL, File saveDir, String fileName) { HttpURLConnection httpConn = null; try { URL url = new URL(fileURL); httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); // always check HTTP response code first if (responseCode == HttpURLConnection.HTTP_OK) { String disposition = httpConn.getHeaderField("Content-Disposition"); String contentType = httpConn.getContentType(); int contentLength = httpConn.getContentLength(); if (StringUtils.isEmpty(fileName)) fileName = parseFileName(fileURL, disposition); logger.info("Content-Type = " + contentType); logger.info("Content-Disposition = " + disposition); logger.info("Content-Length = " + contentLength); logger.info("fileName = " + fileName); if (!saveDir.exists()) { logger.info("Creating File Destination Folder " + saveDir.getAbsolutePath()); saveDir.mkdirs(); } File savedFile = saveFile(saveDir, httpConn, fileName); logger.info("File downloaded to " + savedFile.getAbsolutePath()); return true; } else { logger.warn("No file to download. Server replied HTTP code: " + responseCode); return false; } } catch (Exception e) { logger.error("downloading failed from URL " + fileURL, e); return false; } finally { if (null != httpConn) httpConn.disconnect(); } } private static String parseFileName(String fileURL, String disposition) { String fileName = ""; if (disposition != null) { // extracts file name from header field int index = disposition.indexOf("filename="); if (index > 0) { fileName = disposition.substring(index + 10, disposition.length() - 1); } } else { // extracts file name from URL fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length()); } return fileName; } private static File saveFile(File saveDir, HttpURLConnection httpConn, String fileName) throws IOException, FileNotFoundException { // opens input stream from the HTTP connection InputStream inputStream = httpConn.getInputStream(); File saveFile = new File(saveDir, fileName); // opens an output stream to save into file FileOutputStream outputStream = new FileOutputStream(saveFile); int bytesRead = END_OF_FILE; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = inputStream.read(buffer)) != END_OF_FILE) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); return saveFile; } }
发表评论
-
Start tomcat with port 80 without Linux root user-Use iptables mapping
2016-05-25 17:39 830引用In linux system. only root us ... -
Format XML in JAVA
2016-01-11 12:23 601public static String format ... -
Ehcache RMI Replicated Cluster(RMI集群)
2013-04-25 23:39 1067引用本文是ehcache RMI集群的例子,导入附件中的jav ... -
Integrete unitils for database(dao) testing
2013-02-01 18:39 1691引用Database testing Unit tests f ... -
JAXB入门
2012-10-16 11:59 785引用jaxb是一个读写xml的工具,还可以提供验证,不需要额外 ... -
Freemarker使用入门
2012-10-16 11:54 1017引用freemarker是一种模板标记工具,可以做页面静态化, ... -
perforce java api使用
2012-10-16 11:43 1253引用perforce是种版本管理软件,提供啦完整的java a ... -
XPath 入门
2012-10-16 11:29 879引用xpath可以快速定位获取XML文件中指定属性和值,jdk ... -
Java File Diff-diffutils
2012-09-27 17:35 74441. Maven Dependency <depende ... -
XSD 入门使用
2012-09-18 23:20 775<?xml version="1.0" ... -
nexus-2.1.1安装及使用入门
2012-08-13 22:52 14441. 安装 地址http://www.sonatype.org ... -
File Demo
2012-06-25 22:55 1307package org.springside.examples ... -
Java 访问sharepoint webservice(NTLM & SSL)
2012-06-12 09:47 3771引用遇到需要使用java访问微软的sharepoint的web ... -
Selenium Web Driver入门
2012-05-27 23:17 58351 What is Selenium? 引用Selenium ... -
HttpClient4.1.2 & HtmlUnit2.9 处理文件下载
2012-01-09 18:18 1024TestCode import java.io.Fi ... -
HttpClient4.1.2 & HtmlUnit2.9 NTLM 验证 和 Httpclient4.1.2 https/SSL
2012-01-09 18:13 15941. HttpClient4.1.2 & HtmlUn ... -
HttpClient4登陆ITeye
2012-01-08 23:33 1895import java.io.IOException; im ... -
Spring2集成测试
2011-08-25 22:21 761Spring2测试类继承层次 集成测试例子 public ... -
Learning EasyMock3.0 By Official Example
2011-08-24 16:48 1375Maven Installation+ add followi ... -
Maven+jetty+jrebel+m2eclipse+eclipse搭建struts2开发环境
2011-08-11 11:18 4119引用Maven:项目构建工具,通过pom.xml可以自动维护j ...
相关推荐
HttpUrlConnection下载文件
HttpURLConnection文件下载\httpURLConnection文件下载
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
实例Demo程序来示范使用HttpURLConnection实现多线程下载。 使用多线程下载文件可以更快完成文件的下载,因为客户端启动多条线程进行下载就意味着服务器也需要为该客户端提供响应的服务。假设服务器同时最多服务100...
使用HttpUrlConnection实现上传文件服务器端代码,引用commons-fileupload-1.2.1.jar、commons-io-1.4.jar
Netroid框架 HttpURLConnection类 文件下载demo 主要目的是测量文件下载的速度,注意网络环境的一致性
本示例使用HttpUrlConnection实现上传文件,可以将手机上的文件上传到服务器 必须与my_upload工程配合运行,先启动my_upload工程
Java自带的HttpURLConnection访问接口文件上传, 含普通参数值传递和文件传递
HttpURLConnection实现三方接口HTTP调用调用,带有FILE文件参数
本文通过实例代码给大家介绍了Android基于HttpUrlConnection类的文件下载功能,非常不错,具有参考借鉴价值,需要的的朋友参考下吧
NULL 博文链接:https://dreamoftch.iteye.com/blog/1860289
HttpURLConnection的demo,里面有网络请求get post 上传文件,下载文件,介绍HttpURLConnection这个类整体流程是怎样使用的,方便初学者学习
* 使用URLConnection下载文件或图片并保存到本地。 * * @author 老紫竹(laozizhu.com) */ public class URLConnectionDownloader { public static void main(String[] args) throws Exception { ...
通过HttpURLConnection把本地目录上的资源打包远程到另一台服务器上,有通过过滤器启动Timer,有手工上传的。
主要介绍了java HttpURLConnection 发送文件和字符串信息的相关资料,需要的朋友可以参考下
HttpURLConnection调用三方HTTP接口,带有FILE文件参数
主要介绍了Android程序开发通过HttpURLConnection上传文件到服务器的相关资料,需要的朋友可以参考下
Android中HTTP请求中文乱码解决办法 编码参数 List<NameValuePair> formparams = new ArrayList(); // 请求参数 for (NameValuePair p : params) { formparams.add(p); } ... // 创建POST请求 ...
Java利用HttpURLConnection发送post请求上传文件