1 controller
@RequestMapping(value = "/{project}/{moduel}/export/product/{productId}", method = RequestMethod.GET) public void exportProductData(HttpServletRequest request, HttpServletResponse response, @PathVariable("project") String project, @PathVariable("moduel") String moduel, @PathVariable("productId") String productId) throws IOException { String baseUrl = String.format("/%s/%s/%s", project, moduel, "export/product"); LOG.info(String.format("正在接收%s请求", baseUrl)); Map<String, Object> reqMap = new HashMap<String, Object>(); reqMap.put("productId", productId); RtResponse rtResponse = reqUrlDefService.getResponse(baseUrl, request, reqMap); Map<String,Object> data = (Map<String, Object>) rtResponse.getData(); Map<String,Map<String,Object>> bytes = (Map<String,Map<String,Object>>) data.get("files"); List<File> files = new ArrayList<File>(); for(Entry<String, Map<String, Object>> map : bytes.entrySet()){ Map<String,Object> byteMap = map.getValue(); String fileName = (String) byteMap.get("fileName"); String contentType = (String) byteMap.get("contentType"); byte[] byt = (byte[]) byteMap.get("fileByte"); File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); fos.write(byt); files.add(file); fos.close(); } String filename = URLEncoder.encode("product-struct.zip", "UTF-8"); byte[] buf = new byte[1024]; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filename)); out.setMethod(ZipOutputStream.DEFLATED); for (int i = 0; i < files.size(); i++) { FileInputStream in = new FileInputStream(files.get(i)); out.putNextEntry(new ZipEntry(files.get(i).getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); int bytesRead = 0; byte[] buffer = new byte[8092]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = new FileInputStream(filename); while ((bytesRead = is.read(buffer, 0, 8092)) != -1) { bos.write(buffer, 0, bytesRead); } response.reset(); response.setHeader("Content-disposition", "attachment;filename=" + filename); response.setContentType("application/x-zip-compressed"); OutputStream os = response.getOutputStream(); os.write(bos.toByteArray()); os.flush(); os.close(); }
2 service
@Override public RtResponse exportProductInfo(RtRequest request) throws Exception { RtResponse response = new RtResponse(); Integer code = ResultCode.FAILURE; String dataJson = ""; Map<String,Map<String,Object>> bytes = new HashMap<String,Map<String,Object>>(); //产品图片 if(AppUtils.isNotBlank(adjuncts)){ for(ProductAdjunct pa : adjuncts){ String fileId = pa.getAdjunctPath(); GridFS gridFS = new GridFS(mongotemplate.getDb(), "file_collections"); DBObject query = new BasicDBObject("_id", fileId); GridFSDBFile gridFSDBFile = gridFS.findOne(query); logger.info("附件接口 文件流内容...{}", gridFSDBFile.getInputStream()); Map<String, Object> maps = new HashMap<String, Object>(); maps.put("fileName", pa.getAdjunctClass()+"-+"+gridFSDBFile.getFilename()); maps.put("contentType", gridFSDBFile.getContentType()); maps.put("fileByte", FileUtils.input2byte(gridFSDBFile.getInputStream())); maps.put("type", pa.getAdjunctClass()); bytes.put(pa.getAdjunctClass(),maps); } } dataJson = JsonUtil.javaObjToJson(productDataObject); Map<String, Object> maps = new HashMap<String, Object>(); maps.put("fileName", "ProductData.txt"); maps.put("contentType", "application/txt"); maps.put("fileByte", dataJson.getBytes("UTF-8")); maps.put("type", "product"); bytes.put("product",maps); data.put("files",bytes); code = ResultCode.SUCCESS; response.setCode(code); response.setData(data); return response; }