问题
项目中通过ESB去执行上传下载的时候发现乱码的问题。
之后上传功能通过解析java成功上传的报文格式。解决问题
代码
- JAVA版本上传
@Autowired
private RestTemplate restTemplate;
@Test
public void postUploadEsb() {
HttpHeaders httpHeaders = new HttpHeaders();
MediaType mediaType = new MediaType("application","octet-stream", Charset.forName("UTF-8"));
httpHeaders.setContentType(mediaType);
FileSystemResource resource = new FileSystemResource(new File("B://新建文本文档.zip"));
HttpEntity fileEntity = new HttpEntity<>(resource,httpHeaders);
MultiValueMap paraMap = new LinkedMultiValueMap<>();
paraMap.add("file", fileEntity);
String esbRequestData ="{\n" +
" \"Service\":{\n" +
" \"Header\":{\n" +
" \"Request\":{\n" +
" \"Bcode\":\"\",\n" +
" \"ReqSysCode\":\"S76\",\n" +
" \"ReqTime\":\"20190805153144327\",\n" +
" \"ReqType\":\"0\",\n" +
" \"Security\":{\n" +
" \"AppKey\":\"81f2b3c9-8392-424c-9a54-4a7532518a99\",\n" +
" \"SessionId\":\"4D00489427767A348A745DA1801CCC36\"\n" +
" },\n" +
" \"SerialNumber\":\"S76af256cfa479b4faf8f98e9735221ff39\",\n" +
" \"ServiceCode\":\"S01_COMM_C_00327\",\n" +
" \"Ucode\":\"\"\n" +
" }\n" +
" },\n" +
" \"Body\":{\n" +
" \"Request\":{\n" +
" \"usercode\":\"000000008410\",\n" +
" \"tablename\":\"printsave\",\n" +
" \"icodename\":\"docid\",\n" +
" \"filepathname\":\"docname\",\n" +
" \"compresspathname\":\"\",\n" +
" \"relatelist\":[\n" +
" {\n" +
" \"key\":\"sheettbl\",\n" +
" \"value\":\"js_ydyy_busitips\"\n" +
" },\n" +
" {\n" +
" \"key\":\"doctype\",\n" +
" \"value\":\"0\"\n" +
" },\n" +
" {\n" +
" \"key\":\"sheetid\",\n" +
" \"value\":\"GL0000000000000006\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
paraMap.add("payload",esbRequestData);
this.postFile("http://localhost:8091/esb",paraMap);
}
public JSONObject postFile(String url, MultiValueMap paraMap){
try{
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
formConverter.setCharset(StandardCharsets.UTF_8);
formConverter.setMultipartCharset(StandardCharsets.UTF_8);
restTemplate.getMessageConverters().set(1,formConverter);
restTemplate.getMessageConverters().set(2,new StringHttpMessageConverter(Charset.forName("UTF-8")));
HttpEntity> httpEntity = new HttpEntity>(paraMap,headers);
ResponseEntity repoResponseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
String json = repoResponseEntity.getBody();
JSONObject result = JSONObject.fromObject(json);
System.out.println(result.toString());
return result;
}catch (HttpStatusCodeException e) {
throw e;
}
}
通过拦截器得到报文。发现问题
filename =?UTF-8?B?5paw5bu65paH5pys5paH5qGjLnppcA==?=
?UTF-8?B? ?=这个是邮件头的编码格式 B表示是Base64编码 UTF-8表示字符集的编码
5paw5bu65paH5pys5paH5qGjLnppcA== 这个是普通的base64编码报文中格式为二进制流的方式
最终vue下载的方法
let fd = new FormData();
console.log(file);
let Base64 = require("js-base64").Base64;
// var b = new Base64();
var newName = Base64.encode(file.name);
let reader = new FileReader()
if (typeof FileReader === 'undefined') {
alert('您的浏览器不支持FileReader接口')
return
}
let _this = this;
reader.readAsBinaryString(file)
reader.onload = function () {
console.log(reader.result);
let newFile = new File([reader.result], "=?UTF-8?B?" + newName + "?=", {
type: "application/octet-stream;charset=UTF-8"
}); +
fd.append("file", newFile); //传文件
let val = {
usercode: localStorage.getItem("userCode") || "",
tablename: "printsave", //附件保存表名
icodename: "docid", //附件保存表字段-内码
filepathname: "docname", //附件保存表字段-附件地址名
compresspathname: "", //附件保存表字段-压缩附件地址名
relatelist: [
{
key: "sheettbl",
value: "js_ydyy_busitips"
},
{ key: "doctype", value: "0" },
{
key: "sheetid",
value: _this.upload_busitipsicode //内码
}
]
};
let ReqTime = _this.getReqTime();
let uuid = _this.getUUID();
let SessionId = sessionStorage.getItem("sessionID") || "";
let data_val = {
Service: {
Header: {
Request: {
Bcode: "",
ReqSysCode: "S76",
ReqTime: ReqTime,
ReqType: "0",
Security: {
AppKey: "81f2b3c9-8392-424c-9a54-4a7532518a99",
SessionId: SessionId
},
SerialNumber: "S76" + uuid,
ServiceCode: "S01_COMM_C_00327",
Ucode: ""
}
},
Body: {
Request: val
}
}
};
fd.append("payload", JSON.stringify(data_val));
console.log("-------");
console.log(fd);
let config = {
headers: {
"Content-Type": "charset=UTF-8;multipart/form-data;"
}
};
let requestURL = requestUrl("");
_this.$axios
.post(requestURL, fd, config)
.then(res => {
let header = res.data.Service.Header.Response;
let body = res.data.Service.Body.Response;
if (header && header.ReturnCode == "000000000") {
} else {
_this.$message.error(header.ReturnMessage);
}
})
.catch(err => {});
}
主要通过formData上传数据
用FileReader来解析数据,并且改变附件报文头以及名称。
let reader = new FileReader()
reader.readAsBinaryString(file)
reader.onload = function () {
console.log(reader.result);
let newFile = new File([reader.result], "=?UTF-8?B?" + newName + "?=", {
type: "application/octet-stream;charset=UTF-8"
}); +
fd.append("file", newFile); //传文件
附件下载
bigdownload(val) {
let docname = val.filepath.split("|")[0];
let docname2 = val.filepath.split("|")[1];
let _data = {
usercode: localStorage.getItem("userCode") || "",
docname: docname
};
this.listLoading = true;
let requestURL = requestUrl("");
let ReqSysCode = "S76";
let ReqTime = this.getReqTime();
let uuid = this.getUUID();
let SessionId = sessionStorage.getItem("sessionID") || "";
let userCode = sessionStorage.getItem("userCode") || "";
this.$axios({
url:requestUrl("/esbdownload"),
method: "POST",
responseType: "blob",
data: JSON.stringify({
Service: {
Header: {
Request: {
Bcode: "",
ReqSysCode: ReqSysCode,
ReqTime: ReqTime,
ReqType: "0",
Security: {
AppKey: "81f2b3c9-8392-424c-9a54-4a7532518a99",
SessionId: SessionId
},
SerialNumber: ReqSysCode + uuid,
ServiceCode: "S01_COMM_R_00119",
Ucode: userCode
}
},
Body: {
Request: _data
}
}
})
})
.then(response => {
console.log(response);
this.download(response.data, docname2);
})
.catch(error => {});
},
// 下载文件
download(data, name) {
this.listLoading = false;
if (!data) {
return;
}
let content = data;
let blob = new Blob([content], {
type:
"application/octet-stream;charset=UTF-8"
});
let url = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", name);
document.body.appendChild(link);
link.click();
document.body.removeChild(link); // 下载完成移除元素
window.URL.revokeObjectURL(url); // 释放掉blob对象
}
}
注意点
- responseType: "blob"这个一定要添加,
- 用blob来装载二进制流
let blob = new Blob([content], {
type:
"application/octet-stream;charset=UTF-8"
});