最近在做关于文件上传的
接口,但是不管怎么处理,文件的名称总是乱码,我们使用的技术站为
spring cloud
前端原代码:
class="java" name="code">
uploadData () {
let formData = new FormData()
let fileNames = []
for (let i = 0; i < this.fileList.length; i++) {
formData.append('files', this.fileList[i]);
fileNames.push(this.fileList[i].name)
}
let json = {
'description': this.sampleDescription,
'status': this.qualifyValue
}
formData.append('json', JSON.stringify(json));
// 上传文件
this.$http.post(`/modules/api/v1/sampleInformation/uploadSampleReport/${this.$route.params.sampleId}`, formData).then(res => {
if (res.status === 200) {
this.$router.replace({
name: 'my-sample'
})
} else {
this.loading = false
}
})
更改后的代码:
uploadData () {
let formData = new FormData()
let fileNames = []
for (let i = 0; i < this.fileList.length; i++) {
formData.append('files', this.fileList[i]);
fileNames.push(this.fileList[i].name)
}
let json = {
'description': this.sampleDescription,
'status': this.qualifyValue
}
formData.append('json', JSON.stringify(json));
// 上传文件
this.$http.post(`/zuul/modules/api/v1/sampleInformation/uploadSampleReport/${this.$route.params.sampleId}`, formData).then(res => {
if (res.status === 200) {
this.$router.replace({
name: 'my-sample'
})
} else {
this.loading = false
}
})
后台处理代码:
List<Part> list = (List<Part>) request.getParts();
for (Part part : list) {
if (!part.getName().equalsIgnoreCase("files")) {
continue;
}
//Content-Disposition: form-data; name="files"; filename="test.pdf"
String fileName = part.getHeader("Content-Disposition").split(";")[2];
LOG.info("上传检测报告获取到的文件名称:{}", fileName);
fileName = new String(fileName.getBytes(Charsets.ISO_8859_1), "UTF-8");
}