String downloadDir) { File file = null; try { // 统一资源 URL u
默认环境下是true; httpURLConnection.setDoInput(true); // 配置是否向httpUrlConnection输出 httpURLConnection.setDoOutput(true); // Post 请求不能利用缓存 httpURLConnection.setUseCaches(false); // 设定请求的要领, * 但不足轻便; * * 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,即java.net.URL、java.net.URLConnection,这些API很好用、很常用, Keep-Alive); // 配置字符编码 httpURLConnection.setRequestProperty(Charset, /Users/H__D/Desktop); } } ,默认是GET httpURLConnection.setRequestMethod(POST); // 配置字符编码 httpURLConnection.setRequestProperty(Charset。
即java.net.URL、java.net.URLConnection。
默认是GET httpURLConnection.setRequestMethod(POST); // 配置字符编码毗连参数 httpURLConnection.setRequestProperty(Connection, 0, UTF-8); // 配置请求内容范例 httpURLConnection.setRequestProperty(Content-Type,抽象类 URLConnection urlConnection = url.openConnection(); // http的毗连类 HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; // 设定请求的要领, length); } ds.writeBytes(end); /* close streams */ fStream.close(); } ds.writeBytes(twoHyphens + boundary + twoHyphens + end); /* close streams */ ds.flush(); if (httpURLConnection.getResponseCode() = 300) { throw new Exception( HTTP Request is not success, UTF-8); // 打开到此 URL 引用的资源的通信链接(假如尚未成立这样的毗连),这些API很好用、很常用, 0,抽象类 URLConnection urlConnection = url.openConnection(); // http的毗连类 HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; // 配置是否从httpUrlConnection读入。
new String[] { /Users//H__D/Desktop//1.png, * 但不足轻便; * * 1.通过统一资源定位器(java.net.URL)获取毗连器(java.net.URLConnection) 2.配置请求的参数 3.发送请求 * 4.以输入流的形式获取返回内容 5.封锁输入流 * * @author H__D * */ public class HttpConnectionUtil { /** * 多文件上传的要领 * * @param actionUrl:上传的路径 * @param uploadFilePaths:需要上传的文件路径。
String[] uploadFilePaths) { String end = \r\n; String twoHyphens = --; String boundary = *****; DataOutputStream ds = null; InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader reader = null; StringBuffer resultBuffer = new StringBuffer(); String tempLine = null; try { // 统一资源 URL url = new URL(actionUrl); // 毗连类的父类, multipart/form-data;boundary= + boundary); // 配置DataOutputStream ds = new DataOutputStream(httpURLConnection.getOutputStream()); for (int i = 0; i uploadFilePaths.length; i++) { String uploadFile = uploadFilePaths[i]; String filename = uploadFile.substring(uploadFile.lastIndexOf(//) + 1); ds.writeBytes(twoHyphens + boundary + end); ds.writeBytes(Content-Disposition: form-data; + name=\file + i + \;filename=\ + filename + \ + end); ds.writeBytes(end); FileInputStream fStream = new FileInputStream(uploadFile); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int length = -1; while ((length = fStream.read(buffer)) != -1) { ds.write(buffer, 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(:8080/images/1467523487190.png, 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, Response code is + httpURLConnection.getResponseCode()); } if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { inputStream = httpURLConnection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); reader = new BufferedReader(inputStreamReader); tempLine = null; resultBuffer = new StringBuffer(); while ((tempLine = reader.readLine()) != null) { resultBuffer.append(tempLine); resultBuffer.append(\n); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (ds != null) { try { ds.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (reader != null) { try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return resultBuffer.toString(); } } public static void main(String[] args) { // 上传文件测试 String str = uploadFile(:8080/image/image.do,数组 * @return */ @SuppressWarnings(finally) public static String uploadFile(String actionUrl,上传给处事器 上传代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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请求,//Users/H__D/Desktop/2.png }); System.out.println(str); } } HttpURLConnection文件下载 下载代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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请求。
String downloadDir) { File file = null; try { // 统一资源 URL url = new URL(urlPath); // 毗连类的父类, HttpURLConnection文件上传 HttpURLConnection回收模仿欣赏器上传的数据名目,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/java/12648.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Fitness fitness){ /*double X1=m
时间:2021-01-21
-
所以这里也是需要注意的
时间:2021-01-21
-
hadoop上传文件成果实例代
时间:2021-01-15
-
hadoop负责按key值将map的输
时间:2021-01-15
-
记得勾选springconfig.xml 因为
时间:2021-01-14
-
如果当前没有事务
时间:2021-01-14
-
SpringCloud整合Nacos实现流程
时间:2021-01-07
-
Intellijidea建javaWeb以及Ser
时间:2021-01-07
热门文章
-
Java内部类的实现原理与可能的内存泄漏说
时间:2020-12-29
-
记得勾选springconfig.xml 因为我们之前下载
时间:2021-01-14
-
SpringCloud整合Nacos实现流程详解
时间:2021-01-07
-
JAVA多线程和并发基础面试问答(翻译)
时间:2020-12-25
-
Spring Boot 使用Druid详解
时间:2020-12-28
-
多方位解析,2020Java开发就业前景怎么样
时间:2020-12-25
-
最新IDEA永久激活教程(支持最新2019.2版本
时间:2020-12-25
-
Fitness fitness){ /*double X1=min+0.382*(max-min);*
时间:2021-01-21
-
详解SpringMVC在IDEA中的第一个程序
时间:2021-01-06
-
Java基础:集合框架
时间:2020-12-28
