网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么在JAVA中利用HttpURLConnection实现一个文件上传下载功能

怎么在JAVA中利用HttpURLConnection实现一个文件上传下载功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、网站建设、外贸网站建设、翔安网络推广、成都小程序开发、翔安网络营销、翔安企业策划、翔安品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供翔安建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

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);
   // 设定请求的方法,默认是GET
   httpURLConnection.setRequestMethod("POST");
   // 设置字符编码连接参数
   httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
   // 设置字符编码
   httpURLConnection.setRequestProperty("Charset", "UTF-8");
   // 设置请求内容类型
   httpURLConnection.setRequestProperty("Content-Type", "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, 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 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("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;
   // 设定请求的方法,默认是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");

 }

}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


本文题目:怎么在JAVA中利用HttpURLConnection实现一个文件上传下载功能
文章位置:http://bjjierui.cn/article/jhoeij.html

其他资讯