This article describes the Java export TXT file method. Share to everyone for your reference. Specifically as follows:
Example One
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
/** * Export File/* * @RequestMapping (value= "/grab/export/csv", method={requestmethod.get}) public void Exportcsv ( HttpServletRequest request,httpservletresponse response) {String userId = Servletrequestutils.getstringparameter ( Request, "UserId", "Test"); Modelandview mav=new Modelandview (); Sqlvideolist sqlvideolist =new sqlvideolist (); list<videolistmodel> list = new arraylist<videolistmodel> (); try {list = Sqlvideolist.selectsuccessdate (userId);} catch (SQLException e) {//TODO auto-generated catch block E.print StackTrace (); }//Export TXT file 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 = "RN"; Servletoutputstream outstr = null; try {outstr = Response.getoutputstream ()//Establish buff = new Bufferedoutputstream (OUTSTR);//write content to file if (List.size () >0) { for (int i = 0; i < list.size (); i++) {Write.append (List.get (i) getUrl () + ","); Write.append (List.get (i). GetTitle ()); W Rite.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.pri Ntstacktrace (); } } } |
Example two:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
/** * Export VIP Exchange 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); NET ID//get the corresponding Exchange code vipcodeexample example=new vipcodeexample () for the VIP package of the schools; Example.createcriteria (). Andschoolidequalto (SchoolID). Andpackageidequalto (PackageID). AndExchangeIdEqual to ( Exchangeid); list<vipcode> Vipcodes = Vipexchangemanager.getvipcode (example); if (Vipcodes.size () >0) {Response.setcontenttype ("Text/plain"), or two rows of key settings 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 = "RN"; Servletoutputstream outstr = null; try {outstr = Response.getoutputstream ();//Build buff = new Bufferedoutputstream (OUTSTR); for (int i = 0; i < VIPCODES.S Ize (); 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 ("OK");} 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.pri Ntstacktrace (); } } } } |
I hope this article will help you with your Java programming.