java post流_Java后端HttpClient Post提交文件流 及服务端接收文件流
客户端将文件转换为流发送:
依赖的包:
org.apache.httpcomponents
httpclient
4.4
org.apache.httpcomponents
httpmime
4.4
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public static void main(String[] args) throws IOException {
DataInputStream in = null;
OutputStream out = null;
HttpURLConnection conn = null;
JSONObject resposeTxt = null;
InputStream ins = null;
ByteArrayOutputStream outStream = null;
try {
// URL url = new URL("http://192.168.3.11:8081/mes-boot-doc/test/fileupload?fileName=shafei.xls");
URL url = new URL("http://localhost:8081/mes-boot-doc/test/fileupload?fileName=shafei.xls");
conn = (HttpURLConnection) url.openConnection();
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/html");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Charsert", "UTF-8");
conn.connect();
conn.setConnectTimeout(10000);
out = conn.getOutputStream();
File file = new File("C:/Users/Dell/Desktop/print/shafei.xls");
in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] buffer = new byte[1024];
while ((bytes = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
}
out.flush();
// 返回流
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
ins = conn.getInputStream();
outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int count = -1;
while ((count = ins.read(data, 0, 1024)) != -1) {
outStream.write(data, 0, count);
}
data = null;
resposeTxt = JSONObject.parseObject(new String(outStream
.toByteArray(), "UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (ins != null) {
ins.close();
}
if (outStream != null) {
outStream.close();
}
if (conn != null) {
conn.disconnect();
}
}
}
服务端接收文件流生成文件:
@PostMapping("/fileupload")
public String uploadFile(HttpServletRequest request,HttpServletResponse response) throws Exception{
String fileName = request.getParameter("fileName");
log.info("filename:"+fileName);
// String fileName ="shafei.xls";
// String fileFullPath = "C:/Users/Dell/Desktop/print/test/" + fileName;
String fileFullPath = "/root/uploadfile/apache-tomcat-8.5.42/" + fileName;
InputStream input = null;
FileOutputStream fos = null;
try {
input = request.getInputStream();
File file = new File("/root/uploadfile/apache-tomcat-8.5.42/");
if(!file.exists()){
file.mkdirs();
}
fos = new FileOutputStream(fileFullPath);
int size = 0;
byte[] buffer = new byte[1024];
while ((size = input.read(buffer,0,1024)) != -1) {
fos.write(buffer, 0, size);
}
//响应信息 json字符串格式
Map responseMap = new HashMap();
responseMap.put("flag", true);
//生成响应的json字符串
String jsonResponse = JSONObject.toJSONString(responseMap);
sendResponse(jsonResponse,response);
} catch (IOException e) {
//响应信息 json字符串格式
Map responseMap = new HashMap();
responseMap.put("flag", false);
responseMap.put("errorMsg", e.getMessage());
String jsonResponse = JSONObject.toJSONString(responseMap);
sendResponse(jsonResponse,response);
} finally{
if(input != null){
input.close();
}
if(fos != null){
fos.close();
}
}
return null;
}
/**
* 返回响应
*
* @throws Exception
*/
private void sendResponse(String responseString,HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=UTF-8");
PrintWriter pw = null;
try {
pw = response.getWriter();
pw.write(responseString);
pw.flush();
} finally {
IOUtils.closeQuietly(pw);
}
}
java post流_Java后端HttpClient Post提交文件流 及服务端接收文件流相关推荐
- 表单及数据提交、表单的作用、服务端接收提交的数据、php处理数据流程、文件域及文件域中数据处理、php展示数据(响应)
表单及数据提交: 表单的作用: 用于收集相关信息:html中有专门提交数据的标签,可以很容易的收集用户输入的信息,这个标签有两个重要的属性:action表单提交的地址和method以什么方式提交表单, ...
- java swing对话框_Java开发笔记(一百三十五)Swing的文件对话框
除了常规的提示对话框,还有一种对话框也很常见,它叫做文件对话框.文件对话框又分为两小类:打开文件的对话框.保存文件的对话框,但在Swing中它们都用类型JFileChooser来表达.下面是JFile ...
- java echarts 生成图片_java后端生成echarts图片
一.所需工具 1.phantomjs 2.EChartsConvert 二.Maven依赖 org.freemarker freemarker 2.3.28 org.apache.httpcompon ...
- java中二叉树_java后端学习路线
"学习真的是一个循序渐进的过程,也是一件需要坚持的事情.对于这篇文章,不同阶段的人可能有不同的理解,所以我把我整理的比较全面的java学习路线分享给大家,以供参考.下面的这个学习路线主要针对 ...
- java遍历字符串_Java后端开发算法基础面试题分享,你离大厂也许就差这份面试题
一.算法基础 1. 重建二叉树 题目: 输入一棵二叉树前序遍历和中序遍历的结果,请重建该二叉树. 注意: 二叉树中每个节点的值都互不相同: 输入的前序遍历和中序遍历一定合法: 演示: 给定: 前序遍历 ...
- java copyfile失败_java – FileUtils.copyFile()在目标是网络路径时不创建文件(在Windows上)...
我正在使用apache common的FileUtils.copyFile()将本地磁盘上的文件复制到网络共享位置.共享文件夹已存在,运行该应用程序的用户具有该权限. FileUtils.copyFi ...
- java 图片请求_java中使用scoket模拟http post请求发送图片或文件
最近遇到个问题,两个系统共用用户的头像,用户的头像在一个系统中保存,这就涉及到将图片通过scoket发送给另一个系统存储的问题,最初的思路是将图片读成byte[]数组,然后发送,但又发现,发送图片的同 ...
- java for 最后_Java的for循环中调用了查询服务,最后只打印了循环的最后一条数据(循环次数)次...
问题描述 1.首先我查询了出来一个对象集合(包含1和2俩个对象) 2.利用上面集合中的某数据循环查询下一个表中的数据.(也就是for循环中调用了查询方法,返回一个对象) 3.在循环体中我需要用到第一次 ...
- java在线聊天项目0.4版本 制作服务端接收连接,客户端连接功能 新增客户端窗口打开时光标指向下边文本域功能,使用WindowListener监听WindowAdapter...
建一个服务端类ChatServer,用于设置端口接收连接 package com.swift;import java.io.IOException; import java.net.ServerSoc ...
最新文章
- weblogic 12c 一键静默建域、集群、受管,计算机以及添加受管到集群
- react textarea 空格为什么不换行_React 怎么实现预防XSS 攻击的
- CORDIC算法——圆周系统之旋转模式
- K.O. -------- Eclipse中Maven的报错处理
- 将RGB值转换为灰度值的简单算法(转)
- jenkins active exited(待编辑)
- excel oss 上传_java实现上传文件到oss(阿里云)功能示例
- Erlang --- gen_server
- 神奇技术:科学家借助AI从受害人脑中还原犯罪者样貌
- 出现java.lang.NoSuchMethodError错误的原因
- win10下安装SQL2000
- GandCrab勒索病毒
- 十个接私活赚外快的网站,你有技术就有钱
- 信息提取 Information Extraction
- SVL simulator 2021.3 Radar目标检测结果异常的解决方案
- unity游戏内拍照保存
- 利用python搭建“5433小游戏集成平台”
- cad哪个版本最好用?思路提供
- 八荣八耻(超强暴笑豪华版)
- mac系统python配置