最近在项目中用到了一个传输文件的需求,需要把尽可能的 把文件和判断的其他字段放在一个Map里面去。
之前了解的文件一般都是以单独下载的形式。
我的思路是把文件存到Map的一个key中来实现。之前试过了多种存的方式。最后采用了把byte[]数组的数据先转为string,然后再到客户端拼接起来。
测试的时候在本地访问:http://localhost/testMapStream2
下面是代码示例,在本地调用本地的方式进行测试的。
使用的是SpringBoot,把这两个类放进工程里面运行即可。
服务端代码:
/**
* @ClassName: TestController
* @Author WDF
* @Description 测试
* @Date 2021/1/15 11:01
* @Version 1.0
**/
@RestController
public class TestController {
private static Logger logger = LoggerFactory.getLogger(TestController.class);
/**
* @Author WDF
* @Description 模拟服务端
* @Date 2021/4/16 9:54
* @Param []
* @return java.util.Map
**/
@RequestMapping("/testMapStream")
@ResponseBody
public Map testMap() throws IOException {
//String path = "E:\\a.xlsx";
String path = "E:\\test.txt";
File file = new File(path);
byte[] bytes = FileUtils.readFileToByteArray(file);
StringBuilder str = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
str.append(bytes[i] + ",");
}
Map map = new HashMap<String,Object>(1);
map.put("data", str);
//System.out.println(map);
return map;
}
/**
* @Author WDF
* @Description 模拟客户端
* @Date 2021/4/16 9:54
* @Param []
* @return java.lang.String
**/
@RequestMapping("/testMapStream2")
@ResponseBody
public String testMapStream2(){
String path = "E:\\test2.txt";
String json = PostUtil.doPostTestOne("http://localhost/testMapStream");
HashMap hashMap = JSON.parseObject(json, HashMap.class);
String str = String.valueOf(hashMap.get("data"));
String[] strArra = str.split(",");
byte[] bytes = new byte[strArra.length];
for (int i = 0; i < strArra.length; i++) {
bytes[i] = Byte.parseByte(strArra[i]);
}
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(path);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(path);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return "ok";
}
}
客户端http请求的工具类:
public class PostUtil {
public static String doPostTestOne(String url) {
String msg = null;
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Post请求
HttpPost httpPost = new HttpPost(url);
// 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + responseEntity.getContentLength());
msg = EntityUtils.toString(responseEntity);
System.out.println("响应内容为:" + msg);
return msg;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return msg;
}
}