Code snippet for 1:VM template page
<Divclass= "Col-sm-1"> <Buttontype= "button"class= "btn btn-warning btn-sm"ID= "Exporttext"><Iclass= "Glyphicon glyphicon-file"/>Exporting a text file</Button> </Div>
Code snippet for 2:javascript script file
/* * * export text file * /$ ("#exportText"). On (' click ',function() { window.open (contextpath+ '/exporttext.json ', ' _blank '); });
Code snippet for the 3:java controller
/*** Export File File * For UCC configuration, convert valid number to JSON string and export text file * *@return * @throwsException*/@RequestMapping ("/exporttext.json") Public voidExporttext (httpservletresponse response) {//Get Valid dataList List = "I am godtrue I am the most handsome";//Pseudo code
//convert a collection to a stringString jsonstring =json.tojsonstring (list); Exporttextutil.writetotxt (response,jsonstring,"Switch Control-JSON_FOR_UCC"); }
4: Tool class for exporting text files--the core code of this example, of course, this is just one way, there are other options
ImportJava.io.BufferedOutputStream;ImportJava.text.MessageFormat;ImportJava.util.Calendar;ImportJavax.servlet.ServletOutputStream;ImportJavax.servlet.http.HttpServletResponse;ImportCom.jd.fce.ape.web.common.util.FileUtil;Importorg.apache.commons.lang.StringUtils;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;/*** Tool class for exporting file files*/ Public classExporttextutil {/*** Declaration Logger*/ Private Static FinalLogger Logger = Loggerfactory.getlogger (exporttextutil.class); /*** Export Text file *@paramResponse *@paramjsonstring *@paramFileName*/ Public Staticvoidwritetotxt (httpservletresponse response,string jsonstring,string fileName) {//set the character set of the responseResponse.setcharacterencoding ("Utf-8"); //set the type of response contentResponse.setcontenttype ("Text/plain"); //set the file name and formatResponse.AddHeader ("Content-disposition", "Attachment; Filename= "+ Fileutil.genattachmentfilename (filename+" _ "," Json_for_ucc_ ") + Messageformat.format ("{0,date,yyyy-mm-dd HH:mm:ss}",Newobject[]{calendar.getinstance (). GetTime ()})+ ". txt");//different file formats can be downloaded by using the suffixBufferedoutputstream buff =NULL; Servletoutputstream outstr=NULL; Try{outstr=Response.getoutputstream (); Buff=NewBufferedoutputstream (OUTSTR); Buff.write (Delnull (jsonstring). GetBytes ("UTF-8")); Buff.flush (); Buff.close (); } Catch(Exception e) {logger.error ("Error exporting file file, e:{}", E); } finally {Try{buff.close (); Outstr.close (); } Catch(Exception e) {logger.error ("Error closing Stream object e:{}", e); } } } /*** Returns an empty string if the string object is null, otherwise returns a string that removes the space before and after the string *@paramSTR *@return */ Public Staticstring Delnull (String str) {string Returnstr=""; if(Stringutils.isnotblank (str)) {RETURNSTR=Str.trim (); } returnReturnstr; }}
5: Tool class to resolve garbled export file name
Public Abstract classFileutil {/*** The name of the exported attachment is generated. The export file should be garbled in Chinese * <p> * Response.AddHeader ("content-disposition", "attachment; Filename= "+ cnname); * * @paramCnname *@paramDefaultName *@return */ Public Staticstring Genattachmentfilename (String cnname, String defaultname) {Try {//filename = urlencoder.encode (filename, "UTF-8");Cnname =NewString (Cnname.getbytes ("gb2312"), "Iso8859-1"); /*if (Filename.length () >) {fileName = new String (filename.getbytes ("gb2312"), "ISO8 859-1 "); } */ } Catch(Exception e) {cnname=DefaultName; } returnCnname; }}
6: See below
http://qingfeng825.iteye.com/blog/461504
Java Export TXT file