起因
因公司要重做一个项目,服务由原先.Net WCF改为Java平台下Web Api(Spring Boot).公司的项目一直都是.Net Framework 3.5,这次客户端改为.Net Framework 4.0,为什么不用再高的版本,是因为.Net Framework 4.0可以在Windows Xp上运行,在高的版本Windows Xp就不支持了.所以造成公司大部分项目都是3.5的.还有一部分已经改为Java重做了.
Spring Boot 数据压缩
因为原先WCF服务是做了数据压缩的功能,在换为Spring Boot数据压缩还是要保留的,因为这个项目大量图片及pdf文件.Spring Boot还是开箱即用.直接在配置文件中配置一下就可以使用压缩功能,压缩方式是GZIP方式.
@RequestMapping(value = "/getJson")
public ArrayList<UserInfo> getJson() {
ArrayList<UserInfo> list = new ArrayList<UserInfo>();
for (int i = 0; i < 1000; i++) {
UserInfo info = new UserInfo();
info.setName("tom" + (i + 1));
info.setAge(38);
list.add(info);
}
return list;
}
server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,image/bmp,image/jpeg,image/png,image/tiff,application/pdf
server.compression.min-response-size=2048
压缩之后,getJosn接口返回的数据只有2KB多点,如果没有压缩差不多是26KB多点,压缩后是未压缩的十分之一.
接收文件
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFile(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
if (file.isEmpty()) {
return "failed";
}
File srcFile = new File("e:/blog/" + file.getOriginalFilename());
file.transferTo(srcFile);
return "ok";
}
使用c# 进行Post提交文件.因为新项目客户端是用WinForm来做.这里使用HttpClient.
using (HttpClient client = new HttpClient())
{
var content = new MultipartFormDataContent();
string path = Path.Combine(Environment.CurrentDirectory, "vs2019-cpu.png");
content.Add(new ByteArrayContent(File.ReadAllBytes(path)), "file", "vs2019-cpu.png");
var requestUri = "http://127.0.0.1:8080/uploadFile";
var result = client.PostAsync(requestUri, content).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(result);
}