zoukankan      html  css  js  c++  java
  • 将字符串写出文件,然后根据路径查找下载文件

            String time = QDateTime.dateToString(new Date(), "yyyy-MM-dd HH:mi:ss");
            time = time.replaceAll("-", "");
            time =time.replace(":", "");
            time =time.replace(" ", "");
            time = time.substring(2);
            String file_name = time + ".txt";
            String uploadDir = request.getRealPath("/resources") + "\printTicket\";
            File dirPath = new File(uploadDir);
            OutputStream fos =null;
            try {            
                  if (!dirPath.exists()){
                      dirPath.mkdirs();
                  }
                
                  fos = new FileOutputStream(uploadDir+file_name);            
                fos.write(logTxt.getBytes());
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally{
                if(fos != null){
                    fos.close();
                }
            }
            
    //下载
            InputStream inStream = null;
            OutputStream out = null;
            try{        
    //            response.setContentType("application/vnd.ms-excel");
                response.addHeader("Content-Disposition", "attachment; filename="+ file_name + "");    
                inStream = new FileInputStream(uploadDir+file_name);
                out = response.getOutputStream();
                byte[] buf = new byte[4096];
                int readLength;
                while (((readLength = inStream.read(buf)) != -1)){
                    out.write(buf, 0, readLength);
                }
    
             }catch (Exception e) {
                 log.error("读取文件内容出错");    
             }finally{
                 if(out != null){
                     out.close();
                 }
                 if(inStream!=null){
                     inStream.close();
                 }
             }
  • 相关阅读:
    CCF_ 201403-2_窗口
    CCF_201503-2_数字排序
    [loj3346]交换城市
    [cf516E]Drazil and His Happy Friends
    [cf505E]Mr. Kitayuta vs. Bamboos
    [loj3343]超现实树
    [loj3331]选课
    [loj3342]制作菜品
    [loj3156]回家路线
    [loj3339]美食家
  • 原文地址:https://www.cnblogs.com/jinzhiming/p/4923234.html
Copyright © 2011-2022 走看看