- public void inputStreamUpload() {
- //创建HttpClient对象
- CloseableHttpClient client = HttpClients.createDefault();
- //构建POST请求 请求地址请更换为自己的。
- //1)
- HttpPost post = new HttpPost("http://localhost:8003/uploadAndDownload/uploadFileAction");
- InputStream inputStream = null;
- try {
- //文件路径请换成自己的
- //2)
- inputStream = new FileInputStream("G:\\LearnVideo\\text01.txt");
- MultipartEntityBuilder builder = MultipartEntityBuilder.create();
- builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
- //第一个参数为 相当于 Form表单提交的file框的name值 第二个参数就是我们要发送的InputStream对象了
- //第三个参数是文件名
- //3)
- builder.addBinaryBody("uploadFile", inputStream, ContentType.create("multipart/form-data"), "text01.txt");
- //4)构建请求参数 普通表单项
- StringBody stringBody = new StringBody("12",ContentType.MULTIPART_FORM_DATA);
- builder.addPart("id",stringBody);
- HttpEntity entity = builder.build();
- post.setEntity(entity);
- //发送请求
- HttpResponse response = client.execute(post);
- entity = response.getEntity();
- if (entity != null) {
- inputStream = entity.getContent();
- //转换为字节输入流
- BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, Consts.UTF_8));
- String body = null;
- while ((body = br.readLine()) != null) {
- System.out.println(body);
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(inputStream != null){
- try {
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
网站首页 > 技术文章 正文
猜你喜欢
- 2024-08-03 Fury:一个基于JIT动态编译的高性能多语言原生序列化框架
- 2024-08-03 史上最全!CISCO网络故障排错命令大汇总
- 2024-08-03 一篇文章帮你搞懂什么是网络通信(网络 通信)
- 2024-08-03 谈谈dubbo异常处理(dubbo 异常)
- 2024-08-03 SpringBoot | RestTemplate异常处理器ErrorHandler使用详解
- 2024-08-03 聊聊HttpClient的RedirectStrategy
- 2024-08-03 「设计模式(四) - 责任链模式」(什么是责任链模式)
- 2024-08-03 SpringBoot系列之JDBC数据访问(springboot 访问数据库)
- 2024-08-03 聊聊HttpClient的HttpRoutePlanner
- 2024-08-03 工作日报 2021.10.20 OkHttp3错误异常:unexpected end of stream
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)