文件上传与下载

https://www.cnblogs.com/lixianguo/p/12518970.html

使用springboot实现文件上传与下载可以前往上面链接浏览。

当我们在公司中实际开发时,一般都是前后端分离的。

public void downloadFile(HttpServletResponse response,HttpServletRequest request){
    try{
       response.setContentType("application/x-msdownload");
       Boolean isFireFox = request.getHeader("user-agent").toLowerCase().indexOf("firefox") > 0;
       //response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename,"utf8"));
       response.addHeader("Content-Disposition", "attachment;filename="+( isFireFox ? new String(fileName.getBytes("GB2312"),"ISO-8859-1") : URLEncoder.encode(fileName, "UTF-8")));
       OutputStream output=response.getOutputStream();
       //将output写进workbook.
       wb.write(output);
       output.flush();
       }catch(Exception e){
         logger.error(e.toString());
         logger.error("导出excel出错:%s"+ fileName, e.getMessage());
       }           
}

我这里的wb.write(output),是将excel的工作表wookbook写进到输出流里面,同理我们只需要将我们的数据写入到输出流里面,就能实现我们所要下载的文件,这里我们只需要进行到这个地步,只需要到downloadFile()方法,其他的都是前端来做的,当然你也可以使用postman去测试一下,用postman测试的时候,发送请求时,会弹出一个窗口让你选择下载的路径,你会惊讶于postman的智能之处.大大地简化了我们的开发

 

 

 

posted @ 2020-08-21 15:39  拿着放大镜看世界  阅读(186)  评论(0编辑  收藏  举报