怎么在JAVA中利用HttpURLConnection实现一个文件上传下载功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。HttpURLConnection文
怎么在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;public class HttpConnectionUtil { @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); fStream.close(); } ds.writeBytes(twoHyphens + boundary + twoHyphens + end); 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); }}
--结束END--
本文标题: 怎么在JAVA中利用HttpURLConnection实现一个文件上传下载功能
本文链接: https://www.lsjlt.com/news/222959.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0