从数据库中获取文件转成的byte[] 文件名 文件类型。我要怎么拨给浏览器下载

图片说明

如图的话

图片说明

浏览器接收到了一堆东西 但是没有开启下载

一个简单而且符合springmvc方式的做法:
https://my.oschina.net/datevan/blog/654593

   @RequestMapping("download")  
    public ResponseEntity<byte[]> download() throws IOException {  
        HttpHeaders headers = new HttpHeaders();  
       // headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  
        //设置文件名
        headers.setContentDispositionFormData("attachment", "a.xls");  
        String file = TestController.class.getResource("/config/").getPath()+"a.xls";

        //把文件转成字节数组
        File byteFile = new File(file);
        int size = (int) byteFile.length();
        FileInputStream inputStream = new FileInputStream(byteFile);
        byte[] bytes = new byte[size];

        int offset=0;
        int readed;
        while(offset<size && (readed = inputStream.read(bytes, offset,inputStream.available() )) != -1){
            offset+=readed;
        }
        inputStream.close();

        //返回
        return new ResponseEntity<byte[]>(bytes,headers, HttpStatus.OK);  
    }

/**
* 文件下载
* @param response
* @param fileName 文件名称
* @param filePath 文件服务器地址
*/
@RequestMapping("/download")
public void downloadFile(HttpServletResponse response, @RequestParam("fileName")String fileName, @RequestParam("filePath") String filePath){
OutputStream os = null;
try{
os = response.getOutputStream();
fileName = URLEncoder.encode(fileName, "UTF-8");
byte[] bytes = FastdfsClientUtil.downloadFileInner(filePath);
os.write(bytes);
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
os.flush();
}catch(Exception e){
LOGGER.error("下载文件失败", e);
}finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}//之前详细研究过浏览器下载文件的需求 包括不同浏览器的支持,文件下载,文件预览

顺带提示一下 你的入参传入了两个HttpServletResponse