/** * 下載數據文件壓縮包 * https://www.cnblogs.com/esther-qing/p/6222643.html * https://www.cnblogs.com/zhao1949/p/6731601.html * @throws IOException * @throws Exception */ @Action(value = "datefile") public void filedate() throws IOException, Exception { HttpServletRequest request = ServletActionContext.getRequest(); request.setCharacterEncoding("utf-8"); String key =req.getParameter("key"); ByteArrayOutputStream os =new ByteArrayOutputStream(); String sbb="123"; InputStream imgInputStream = null; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); if (key!=null&&!"".equals(key)) { Analyse analyse=analyseService.find(key); if (analyse!=null) { String sql="SELECT * from measure_object where measure_id='8a22de5859d992290159da04b6ef16d0'"; String datname=jdbc.GetObject(sql,"name"); String measure_object_id=jdbc.GetObject(sql,"id"); String device_id=jdbc.GetObject(sql,"device_id"); String pic=jdbc.GetObject(sql,"c_id"); if (datname==null||"".equals(datname)) { datname= UUID.randomUUID().toString().replaceAll("-",""); } sbb=datname; ZipOutputStream zos = null; zos = new ZipOutputStream(os); zos.setEncoding("UTF-8"); zos.putNextEntry(new ZipEntry(datname+ ".dat"));//建立文件 StringBuilder sb = new StringBuilder(); sb.append("測線名稱").append(",").append("測點增量").append(","); //查找設備頻率 //第一行 String sql2="select * from frequency where device_id='"+device_id+"' order by sort asc "; List<Map<String, Object>> listFile = jdbc.select(sql2, "unit,value");// 制定輸出 for (Map<String, Object> map :listFile) { sb.append("\"").append((String)map.get("value")+(String)map.get("unit")).append("\","); } // sb.delete(sb.length() - 1, sb.length()); sb.append("建立時間"); sb.append(System.lineSeparator()); zos.write(sb.toString().getBytes()); //寫入數據 String sql3="select * from measure_cell where measure_object_id='"+measure_object_id+"' order by col asc,row asc"; // String sql3="select * from measure_cell where measure_object_id='8a22de5859d992290159da04b6ef16d1' GROUP BY col asc"; List<Map<String, Object>> list2= jdbc.select(sql3, "id,col,row,value,measure_object_id");// 制定輸出 int a=1; for (Map<String, Object> map : list2) {//獲得全部行數據,按照col的值 從1開始寫 sb.setLength(0); sb.append(datname).append(","); sb.append(a * 10).append(","); a++; String col=(String) map.get("col");//行 String row=(String) map.get("row");//列 String value=(String) map.get("value"); String object_id=(String) map.get("measure_object_id"); if (col!=null&&!"".equals(col)&&object_id!=null&&!"".equals(object_id)) { String sql44="select * from measure_cell where measure_object_id='"+object_id+"' and col='"+col+"' order by row asc"; List<Map<String, Object>> list4= jdbc.select(sql44, "id,col,row,value,measure_object_id");// 制定輸出 for (Map<String, Object> map2 : list4) {//獲得所屬行的列數據 String value3=(String) map2.get("value"); if (value3!=null&&!"".equals(value3)) { sb.append(value3).append(","); } } if (analyse.getCreatetime()!=null&&!"".equals(analyse.getCreatetime())) { sb.append(sdf.format(analyse.getCreatetime())); }else{ sb.append(sdf.format(new Date())); } sb.append(System.lineSeparator()); zos.write(sb.toString().getBytes()); } } ///寫入圖片 String image =QiniuFile.down(pic); if (image!=null&&!"".equals(image)) { //new一個URL對象 zos.putNextEntry(new ZipEntry(pic + ".png")); URL url = new URL(image); //打開連接 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //設置請求方式爲"GET" conn.setRequestMethod("GET"); //超時響應時間爲5秒 conn.setConnectTimeout(5 * 1000); //經過輸入流獲取圖片數據 InputStream inStream = conn.getInputStream(); //獲得圖片的二進制數據,以二進制封裝獲得數據,具備通用性 // byte[] data = readInputStream(inStream); //outStream.toByteArray() //建立一個Buffer字符串 byte[] buffer = new byte[1024]; //每次讀取的字符串長度,若是爲-1,表明所有讀取完畢 int len = 0; //使用一個輸入流從buffer裏把數據讀取出來 while( (len=inStream.read(buffer))>0){ // while( (len=inStream.read(buffer))>-1){ //用輸出流往buffer裏寫入數據,中間參數表明從哪一個位置開始讀,len表明讀取的長度 //outStream.write(buffer, 0, len); zos.write(buffer, 0, len); } //關閉輸入流 inStream.close(); //把outStream裏的數據寫入內存 // return outStream.toByteArray(); // zos.flush(); // zos.closeEntry(); } zos.flush(); zos.closeEntry(); } } os.flush(); ByteArrayResource res=new ByteArrayResource(os.toByteArray()); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-disposition", "attachment;filename=123.zip" ); OutputStream ouputStream = response.getOutputStream(); ouputStream.write(os.toByteArray()); ouputStream.flush(); os.close(); ouputStream.close(); }
主要是將文件流裝入到os中,而後經過ouputstream輸出文件html