50,530
社区成员
发帖
与我相关
我的任务
分享
@RequestMapping(value = "/download")
public void download(@RequestBody FileUpload fileUpload, HttpServletResponse response) {
String fileName = fileUpload.getRealFileName();
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名(这个信息头会告诉浏览器这个文件的名字和类型)
response.setContentType("application/octet-stream");// 二进制流,不知道下载文件的类型"application/octet-stream;charset=utf-8"
String fileFullName = fileUpload.getFilePath() + fileUpload.getRandomFileName();
File file = new File(fileFullName);
try {
logger.error(response.getContentType());
OutputStream outputStream = null;// 文件输出流
outputStream = response.getOutputStream();
FileDownLoad.fileReadInDownLoading(file, outputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
}
}
public static void fileReadInDownLoading(File file, OutputStream outputStream) {
if (file == null || !file.exists()) {
logger.error((file != null ? file.getPath() : "") + "文件不存在");
throw new BaseException(CrmExceptionCode.文件不存在);
}
byte[] buffer = new byte[1024];
FileInputStream fis = null;// 文件输入流
BufferedInputStream bis = null;// 带缓冲区的输入流
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while (i != -1) {
outputStream.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
try {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
export function downloadMarketFile(fileUpload) {
return request({
url: '/fileUpload/download',
responseType: 'blob', // !!!!必须
method: 'post',
data: fileUpload
})
}
一直以为后台代码或者环境的问题,把所有可能性都找了一遍,结果是前端设置的问题。我前端用的vue