/** * Export the VIP redemption code. * @ Throws UnsupportedEncodingException */ @ RequestMapping (value = "/{exchangeId}/{packageId}/export", method = RequestMethod. GET) Public void writeToTxt (@ PathVariable String exchangeId, @ PathVariable String packageId, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { String schoolId = this. getSchoolId (request); // network school ID // Obtain the redemption code for the VIP package of the online school. VipCodeExample example = new VipCodeExample (); Example. createCriteria (). andSchoolIdEqualTo (schoolId). andPackageIdEqualTo (packageId). andExchangeIdEqual To (exchangeId ); List <VipCode> vipCodes = vipExchangeManager. getVipCode (example ); If (vipCodes. size ()> 0 ){ Response. setContentType ("text/plain"); // The key settings of the two lines Response. addHeader ("Content-Disposition ", "Attachment; filename =" + java.net. URLEncoder. encode (vipCodes. get (0). getName (), "UTF-8") + ". txt "); // Filename specifies the default name VipCode vipcode = new VipCode (); BufferedOutputStream buff = null; StringBuffer write = new StringBuffer (); String tab = ""; String enter = "\ r \ n "; ServletOutputStream outSTr = null; Try { OutSTr = response. getOutputStream (); // create Buff = new BufferedOutputStream (outSTr ); For (int I = 0; I <vipCodes. size (); I ++ ){ Vipcode = vipCodes. get (I ); Write. append (I + 1); // serial number Write. append (tab ); Write. append (vipcode. getExchangeCode ()); Write. append (tab ); If ("normal". equals (vipcode. getStatus ())){ Write. append ("normal "); } Else { Write. append ("Redeemed "); } 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 (); } } } } |