栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java post流

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 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;

}

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);

}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023534.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号