当前位置: 首页 > news >正文

怀化新站优化wordpress nginx安装目录

怀化新站优化,wordpress nginx安装目录,wordpress 404跳转首页,建站培训班本文介绍了JAVA通过HttpURLConnection 上传和下载文件的方法#xff0c;分享给大家#xff0c;具体如下#xff1a;HttpURLConnection文件上传HttpURLConnection采用模拟浏览器上传的数据格式#xff0c;上传给服务器上传代码如下#xff1a;package com.util;import java…本文介绍了JAVA通过HttpURLConnection 上传和下载文件的方法分享给大家具体如下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 actionUrl上传的路径* param uploadFilePaths需要上传的文件路径数组* return*/SuppressWarnings(finally)public static String uploadFile(String actionUrl, 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);// 连接类的父类抽象类URLConnection urlConnection url.openConnection();// http的连接类HttpURLConnection httpURLConnection (HttpURLConnection) urlConnection;// 设置是否从httpUrlConnection读入默认情况下是true;httpURLConnection.setDoInput(true);// 设置是否向httpUrlConnection输出httpURLConnection.setDoOutput(true);// Post 请求不能使用缓存httpURLConnection.setUseCaches(false);// 设定请求的方法默认是GEThttpURLConnection.setRequestMethod(POST);// 设置字符编码连接参数httpURLConnection.setRequestProperty(Connection, Keep-Alive);// 设置字符编码httpURLConnection.setRequestProperty(Charset, UTF-8);// 设置请求内容类型httpURLConnection.setRequestProperty(Content-Type, multipart/form-data;boundary boundary);// 设置DataOutputStreamds 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, 0, 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, 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 blocke.printStackTrace();} finally {if (ds ! null) {try {ds.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (reader ! null) {try {reader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (inputStreamReader ! null) {try {inputStreamReader.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (inputStream ! null) {try {inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return resultBuffer.toString();}}public static void main(String[] args) {// 上传文件测试String str uploadFile(http://127.0.0.1:8080/image/image.do,new String[] { /Users//H__D/Desktop//1.png,//Users/H__D/Desktop/2.png });System.out.println(str);}}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;// 设定请求的方法默认是GEThttpURLConnection.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {return file;}}public static void main(String[] args) {// 下载文件测试downloadFile(http://localhost:8080/images/1467523487190.png, /Users/H__D/Desktop);}}以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。
http://www.pierceye.com/news/522063/

相关文章:

  • 鄂州市建设局网站佰牛网站建设
  • 织梦网站上传及安装步骤农畜产品销售平台的网站建设
  • 网站续费如何做分录做交互设计的网站
  • 国家网站备案查询系统安丘网站建设多少钱
  • 长沙公司网站设计鹤壁建设网站推广公司电话
  • 电子商务网站建设与管理实务电子商务网站的构建
  • 做网站的集团用什么自己做网站
  • 买网站空间网站模块图片
  • 上海建设网站公在微信上怎么开店
  • 哪家网站雅虎全球购做的好做一婚恋网站多少钱
  • 苏州企业网站公司都有哪些php开源企业网站系统
  • wordpress收录很慢自己的网站如何优化
  • 个人介绍网站源码1v1网站建设
  • 大宇网络做网站怎么样app制作器下载软件
  • 四川建行网站做网站公司职务
  • 广州定制网站设计图标设计免费 logo
  • 十大网站有哪些网站建设 模板
  • 网站流量一直下降中国十大品牌网
  • 同学录网站开发的背景域名注册网站免费
  • 旅游电子商务网站建设规划书温州网站建设策划方案
  • 国家住房建设部网站域名查询官方网站
  • app开发 网站开发统称宁波seo推广咨询
  • 专门做书单的网站网络营销策划方案的设计
  • 网站建设推广合同自己建设网站需要花多少钱
  • 深圳网站建设电话哈尔滨建设网站官网
  • 上海网站建设网页制作培训做网站做论坛赚钱吗
  • 为网站做电影花絮哈尔滨互联网公司
  • 哈尔滨微网站建设公司做网站被骗该咋样做
  • 做翻译 英文网站dede网站版权信息
  • 梅江区住房和城乡建设局官方网站品牌设计帮