文件下载模板

文件下载模板

public void downloadFile(HttpServletResponse response){
  InputStream inputStream  = null;
  OutputStream outputStream  = null;
      try{
        // 读到流中
        inputStream  = ;
        //自定义文件名称
        String fileName = ;
        String fileName = new String(filename.getBytes("GBK"),"ISO8859-1");
       // 设置输出的格式
        response.reset();
        response.addHeader("Content-Disposition", String.format("attachment;filename=\"%s\"",fileName));
        response.setContentType("application/octet-stream");
        outputStream = new BufferOutputStream(response.getOutputStream);
        // 循环取出流中的数据
        byte[] chuf = new byte[1024];
        int len;
        while ((len = inStream.read(chuf)) > 0){
          outputStream.write(chuf, 0, len);
          }
      
          outputStream.flush();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
               try{
                    if (inputStream  != null)
                           inputStream  .close();
               } catch (IOException e) {
                    e.printStackTrace();
              }
                 
                try{
                    if (outputStream != null)
                           outputStream .close();
               } catch (IOException e) {
                    e.printStackTrace();
              }
         }
 
     }                                                    

 

上一篇:关键词搜图、截图小助手——有了它妈妈再也不用担心我数据集啦


下一篇:第9天 用css画一个五边形和一个六边形