vue接收springboot后端返回的文件流,大小没有错,为啥excel与word打不开,打开是乱码,txt可以正常打开?

    public String downloadFile (HttpServletRequest request,HttpServletResponse response,com.sunyard.cusprotect.entities.File downfile)  {

try {

// 不使用客户端传来的文件名,使用数据库查出来的,避免路径纂改 漏洞

com.sunyard.cusprotect.entities.File fileInfo = universalService.getFileById(downfile.getId());

String meansFictitiousName = fileInfo.getName();

String meansName = fileInfo.getReal_name();

File file = new File(downloadFilePath + File.separator + meansFictitiousName);

if (file.exists()) {

// String fileName = file.getName().toString();

// firefox浏览器

if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {

meansName = new String(meansName.getBytes("UTF-8"), "ISO8859-1");

} // IE浏览器

else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {

meansName = URLEncoder.encode(meansName, "UTF-8");

}// 谷歌

else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {

// meansName = new String(meansName.getBytes("UTF-8"), "ISO8859-1");

}

//首先设置响应的内容格式是force-download,那么你一旦点击下载按钮就会自动下载文件了

//response.setContentType("application/force-download");

//通过设置头信息给文件命名,也即是,在前端,文件流被接受完还原成原文件的时候会以你传递的文件名来命名

response.addHeader("Content-Disposition", "attachment;filename=" + meansName);

response.setHeader("Cache-Control", "cache");

response.setCharacterEncoding("UTF-8");

//进行读写操作

byte[] buffer = new byte[1024];

FileInputStream fis = null;

BufferedInputStream bis = null;

try {

fis = new FileInputStream(file);

bis = new BufferedInputStream(fis);

OutputStream os = response.getOutputStream();

//从源文件中读

int i = bis.read(buffer);

while (i != -1) {

//写到response的输出流中

os.write(buffer, 0, i);

i = bis.read(buffer);

}

os.close();

return "下载成功";

} catch (IOException e) {

e.printStackTrace();

}finally {

if(bis != null){

try {

bis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(fis != null){

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return "下载失败";

}

        download(row) {

// 获取列表数据

this.$http.post('file/download', row).then(res =>{

fileDownload(res.data,res.headers,row.real_name)

}).catch((err) => {

console.log(err)

this.$message.info('下载错误')

})

},

const CONTENT_TYPE = 'content-type'

const CONTENT_DISPOSITION = 'content-disposition'

export default function fileDownload(data, header, fileName) {

const blob = new Blob([data], { type: header[CONTENT_TYPE] || 'application/octet-stream' })

if (window.navigator.msSaveBlob) {

// ie兼容

window.navigator.msSaveBlob(blob)

} else {

const blobURL = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob)

const template = document.createElement('a')

template.style.display = 'none'

template.href = blobURL

const download = fileName || decodeURIComponent(header[CONTENT_DISPOSITION].split('filename=').pop())

template.setAttribute('download', download)

if (typeof download === 'undefined') {

template.setAttribute('target', '_blank')

}

document.body.append(template)

template.click()

setTimeout(_ => {

document.body.removeChild(template)

if (window.URL && window.URL.revokeObjectURL) {

window.URL.revokeObjectURL(blobURL)

} else {

window.webkitURL.revokeObjectURL(blobURL)

}

}, 200)

}

}


回答:

后端改成get自己在浏览器试下文件对不对,不对那就是读写时有问题,对的话那就是前端处理有问题,可以用saveAs这个库


回答:

后端试着把返回去掉(String换成void)

以上是 vue接收springboot后端返回的文件流,大小没有错,为啥excel与word打不开,打开是乱码,txt可以正常打开? 的全部内容, 来源链接: utcz.com/p/944460.html

回到顶部