分割一段网址(url)

<atomic> - C++ Reference

#define _CRT_SECURE_NO_WARNINGS 
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string url("https://legacy.cplusplus.com/reference/atomic/");
    size_t pos1 = url.find(':');
    string url1 = url.substr(0, pos1 - 0);
    cout << url1 << endl;
    size_t pos2 = url.find('/',pos1+3);

    string url2 = url.substr(pos1 + 3,pos2-(pos1+3));
    cout << url2 << endl;
    string url3 = url.substr(pos2+1);
    cout << url3 << endl;
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码怎么写? 好的,以下是一个简单的Java文件上传的示例代码: ``` import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.UUID; public class FileUploader { public static void upload(String urlString, String filePath) throws IOException { // 生成随机边界 String boundary = UUID.randomUUID().toString(); // 创建URL对象 URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); // 设置请求属性 connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); // 打开连接,开始上传文件 connection.setDoOutput(true); connection.connect(); // 获取文件名 File file = new File(filePath); String fileName = file.getName(); // 构建请求体 StringBuilder builder = new StringBuilder(); builder.append("--").append(boundary).append("\r\n"); builder.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(fileName).append("\"\r\n"); builder.append("Content-Type: application/octet-stream\r\n\r\n"); byte[] headerBytes = builder.toString().getBytes(); // 把请求头写入输出流 connection.getOutputStream().write(headerBytes); // 读取文件,把文件内容写入输出流 FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { connection.getOutputStream().write(buffer, 0, bytesRead); } inputStream.close(); // 结束请求体 builder = new StringBuilder(); builder.append("\r\n--").append(boundary).append("--\r\n"); byte[] footerBytes = builder.toString().getBytes(); connection.getOutputStream().write(footerBytes); // 获取响应 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { System.out.println("上传成功!"); } else { System.out.println("上传失败!"); } connection.disconnect(); } public static void main(String[] args) throws IOException { String urlString = "http://www.example.com/upload"; String filePath = "C:/example.txt"; upload(urlString, filePath); } } ``` 这个示例代码可以将本地的一个文件上传到指定的URL地址。如果文件上传成功,就会在控制台输出"上传成功!",否则输出"上传失败!"。注意,这个代码中的boundary是一个随机生成的字符串,用来分割不同的请求参数。在写请求体时,要按照multipart/form-data格式的规定来构造RequestBody。如果上传的文件很大,可以考虑使用分段上传,以减少内存占用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值