【JAVA】通过HttpURLConnection 上传和下载文件(二)
2016-07-03 15:25
696 查看
HttpURLConnection文件上传
HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器上传代码如下:
package com.util; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Iterator; import java.util.Map; /** * Java原生的API可用于发送HTTP请求,即java.net.URL、java.net.URLConnection,这些API很好用、很常用, * 但不够简便; * * 1.通过统一资源定位器(java.net.URL)获取连接器(java.net.URLConnection) 2.设置请求的参数 3.发送请求 * 4.以输入流的形式获取返回内容 5.关闭输入流 * * @author H__D * */ public class HttpConnectionUtil { /** * * @param urlPath * 下载路径 * @param downloadDir * 下载存放目录 * @return 返回下载文件 */ public static File downloadFile(String urlPath, String downloadDir) { File file = null; try { // 统一资源 URL url = new URL(urlPath); // 连接类的父类,抽象类 URLConnection urlConnection = url.openConnection(); // http的连接类 HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; // 设定请求的方法,默认是GET httpURLConnection.setRequestMethod("POST"); // 设置字符编码 httpURLConnection.setRequestProperty("Charset", "UTF-8"); // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。 httpURLConnection.connect(); // 文件大小 int fileLength = httpURLConnection.getContentLength(); // 文件名 String filePathUrl = httpURLConnection.getURL().getFile(); String fileFullName = filePathUrl.substring(filePathUrl.lastIndexOf(File.separatorChar) + 1); System.out.println("file length---->" + fileLength); URLConnection con = url.openConnection(); BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream()); String path = downloadDir + File.separatorChar + fileFullName; file = new File(path); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } OutputStream out = new FileOutputStream(file); int size = 0; int len = 0; byte[] buf = new byte[1024]; while ((size = bin.read(buf)) != -1) { len += size; out.write(buf, 0, size); // 打印下载百分比 // System.out.println("下载了-------> " + len * 100 / fileLength + // "%\n"); } bin.close(); out.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { return file; } } public static void main(String[] args) { // 下载文件测试 downloadFile("http://localhost:8080/images/1467523487190.png", "/Users/H__D/Desktop"); } }
downloadFile
相关文章推荐
- TCP /IP 协议-简介
- TCP的三次握手和四次挥手
- [线性规划与网络流24题] 网络流常见模型
- TCP /IP 协议-(传输层)UDP协议
- python学习之路-9 socket网络编程
- http://www.cnblogs.com/saqhybj/p/4723345.html
- http://www.cnblogs.com/jiligalaer/p/4097429.html
- TCP /IP 协议- (网络层)ARP、 RARP、ICMP、IGMP协议
- CCNA+NP学习笔记—交换网络篇
- CCNA+NP学习笔记—交换网络篇
- android网络框架Retrofit使用记录
- 在线答题系统(适合党政军和院校使用的网络竞答系统)
- JavaScript笔记整理 —— XMLHttpRequest对象
- UNIX网络编程——概述
- 产生随机数可以提高网络安全的新方法(New method of producing random numbers could improve cybersecurity)
- socket与http的区别
- Java网络编程(五)socket的半包,粘包与分包的问题
- Java网络编程(四) Reactor和Proactor模式
- Java网络编程(三) java 流io和块io
- Java网络编程(二) Java I/O分析(jdk1.4之前)