java中的URLConnection和HttpURLConnection有什么区别(因为我自己搜到别人写的区别看下来都没有什么区别)...

本文探讨了URLConnection与HttpURLConnection在Java中的使用区别。指出HttpURLConnection是URLConnection的子类,专门用于处理HTTP请求,而URLConnection更通用,支持多种协议。文章还提到了HttpURLConnection提供了如getResponseCode()等额外的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  今天看了一下公司同事的代码,如下

1         URLConnection connection = openConnection(localURL);
2         HttpURLConnection httpURLConnection = (HttpURLConnection)connection;

  好奇,为何需要将 URLConnection 转 HttpURLConnection ?

  点进两个源码里面发现

1 一、HttpURLConnection 继承 URLConnection 
2 public abstract class URLConnection //URLConnection
3 abstract public class HttpURLConnection extends URLConnection //HttpURLConnection 

  将转换的代码注掉,发现编译报错,证明

1 二、URLConnection 较 HttpURLConnection 多一些方法,如图下的getResponseCode()

  再去仔细看 两个类分别得详解,个人理解:

1 三、URLConnection 可以走邮件、文件传输协议,而HttpURLConnection 就单指浏览器的HTTP协议

 ==================分割线====================

这是我搜到的博友写的,区别

 

 

 

转载于:https://www.cnblogs.com/sun7897/p/9993657.html

### 使用 `java.net.URLConnection` 设置 Content-Type 为 multipart/form-data 为了通过 `java.net.URLConnection` 发送 `multipart/form-data` 类型的数据,需要手动构建请求体并设置合适的头部信息。下面是一个完整的示例来展示这一过程: ```java import java.io.*; import java.net.*; public class MultipartFormDataExample { private static final String CRLF = "\r\n"; private static final String BOUNDARY_PREFIX = "--"; public void sendMultipartForm(String urlString, File file) throws IOException { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置连接属性 connection.setDoOutput(true); // 表明此URL连接打算入数据 connection.setRequestMethod("POST"); String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; // 自定义边界字符串 // 添加Content-Type头,并指定boundary connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); try (OutputStream outputStream = connection.getOutputStream()) { PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true); // 构建文件部的内容 StringBuilder sb = new StringBuilder(); sb.append(BOUNDARY_PREFIX).append(boundary).append(CRLF); sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"").append(CRLF); sb.append("Content-Type: application/octet-stream").append(CRLF); sb.append(CRLF); writer.println(sb.toString()); FileInputStream inputStream = null; byte[] buffer = new byte[4096]; int bytesRead; try { inputStream = new FileInputStream(file); while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); writer.append(CRLF).flush(); // 文件结束后的CRLF // 结束标志 writer.append(BOUNDARY_PREFIX).append(boundary).append(BOUNDARY_PREFIX).append(CRLF).flush(); InputStream responseStream = new BufferedInputStream(connection.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(responseStream)); String line; while((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); } finally { if(inputStream!=null){ inputStream.close(); } } } } } ``` 上述代码展示了如何创建一个多部件表单上传请求[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值