springMVC 导出.txt文件分析

访客 阅读:165 2021-03-31 21:43:08 评论:0

SpringMVC源码:

/* 
	 * 设备烧入基础数据导出功能 
	 *  
	 */ 
	@RequestMapping(value = "/productDetailBurn_export.html") 
	@ResponseBody 
	public void productDetailBurn_export(ProductDetailInfo productDetailInfo, HttpServletRequest request, 
			HttpServletResponse response) throws IOException { 
		DbContextHolder.setDbType("isap"); 
		ModelAndView modelAndView = new ModelAndView(); 
		ProductInfo pro = (ProductInfo) request.getSession().getAttribute("mainProduct"); 
		if (pro != null && pro.getProductId() != null) { 
			Integer pageNum = 1; 
			if (request.getParameter("pageNum") != null) { 
				pageNum = Integer.parseInt(request.getParameter("pageNum")); 
			} 
			productDetailInfo.setPageNum((pageNum - 1) * 10); 
			productDetailInfo.setPageSize(10);		 
			 
			List<String> strs = productDetailInfoService.selectProductDetailBurn(productDetailInfo);			 
			 
			//导出txt文件  
			response.setContentType("text/plain");   
			String fileName="videolist";  
			  try {  
			   fileName = URLEncoder.encode("videolist", "UTF-8");  
			  } catch (UnsupportedEncodingException e1) {  
			   // TODO Auto-generated catch block  
			   e1.printStackTrace();  
			  }   
			  response.setHeader("Content-Disposition","attachment; filename=" + fileName + ".txt");   
			  BufferedOutputStream buff = null;    
			  StringBuffer write = new StringBuffer();    
			  String enter = "\r\n";    
			  ServletOutputStream outSTr = null;  
			  try {    
				    outSTr = response.getOutputStream(); // 建立    
				    buff = new BufferedOutputStream(outSTr);  
				    //把内容写入文件  
				    if(strs.size()>0){  
				     for (int i = 0; i < strs.size(); i++) {  
				      write.append(strs.get(i)); 				    
				      write.append(enter);    
				     }  
				    }  
				    buff.write(write.toString().getBytes("UTF-8"));    
				    buff.flush();    
				    buff.close();    
				  } catch (Exception e) {    
				   e.printStackTrace();    
				  } finally {    
				   try {    
				    buff.close();    
				    outSTr.close();    
				   } catch (Exception e) {    
				    e.printStackTrace();    
				   }    
				  } 			   
//			response.setContentType("application/x-msdownload"); 
//			response.setHeader("Content-Disposition", "attachment;filename=equipmentDataExport.xls"); 
//			OutputStream ouputStream = response.getOutputStream(); 
//			POIUtil.exportExcel2FilePath("统一认证平台开发者数据信息", ouputStream, headInfoList, dataList); 
//			ouputStream.flush(); 
//			ouputStream.close(); 
		} else { 
			modelAndView.setViewName("redirect:/productinfo/productList.html"); 
		} 
	}


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号