Java導(dǎo)出txt文件的方法
更新時間:2015年05月29日 11:34:49 作者:wo_soul
這篇文章主要介紹了Java導(dǎo)出txt文件的方法,實例分析了兩種java導(dǎo)出txt文本文件的使用技巧,需要的朋友可以參考下
本文實例講述了Java導(dǎo)出txt文件的方法。分享給大家供大家參考。具體如下:
例子一
/** * export導(dǎo)出文件 */ @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.printStackTrace(); } //導(dǎo)出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); //把內(nèi)容寫入文件 if(list.size()>0){ for (int i = 0; i < list.size(); i++) { write.append(list.get(i).getUrl()+","); write.append(list.get(i).getTitle()); 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(); } } }
例子二:
/** * 導(dǎo)出VIP兌換碼。 * @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); // 網(wǎng)校ID // 獲取網(wǎng)校的VIP套餐相應(yīng)的兌換碼 VipCodeExample example=new VipCodeExample(); example.createCriteria().andSchoolIdEqualTo(schoolId).andPackageIdEqualTo(packageId).andExchangeIdEqualTo(exchangeId); List<VipCode> vipCodes = vipExchangeManager.getVipCode(example); if(vipCodes.size()>0){ response.setContentType("text/plain");// 一下兩行關(guān)鍵的設(shè)置 response.addHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode(vipCodes.get(0).getName(),"UTF-8")+".txt"); // filename指定默認的名字 VipCode vipcode=new VipCode(); BufferedOutputStream buff = null; StringBuffer write = new StringBuffer(); String tab = " "; String enter = "\r\n"; ServletOutputStream outSTr = null; try { outSTr = response.getOutputStream();// 建立 buff = new BufferedOutputStream(outSTr); for (int i = 0; i < vipCodes.size(); i++) { vipcode = vipCodes.get(i); write.append(i+1); //序號 write.append(tab); write.append(vipcode.getExchangeCode()); write.append(tab); if("normal".equals(vipcode.getStatus())){ write.append("正常"); }else{ write.append("已兌換"); } 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(); } } } }
希望本文所述對大家的java程序設(shè)計有所幫助。
相關(guān)文章
Spring實現(xiàn)動態(tài)切換多數(shù)據(jù)源的解決方案
這篇文章主要給大家介紹了Spring實現(xiàn)動態(tài)切換多數(shù)據(jù)源的解決方案,文中給出了詳細的介紹和示例代碼,相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價值,有需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-01-01crawler4j抓取頁面使用jsoup解析html時的解決方法
crawler4j對response沒有指定編碼的頁面,解析成亂碼,很讓人煩惱,下面給出解決方法,需要的朋友可以參考下2014-04-04java并發(fā)編程專題(五)----詳解(JUC)ReentrantLock
這篇文章主要介紹了java(JUC)ReentrantLock的的相關(guān)資料,文中講解非常詳細,實例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07SpringMVC使用hibernate-validator進行參數(shù)校驗最佳實踐記錄
這篇文章主要介紹了SpringMVC使用hibernate-validator進行參數(shù)校驗最佳實踐,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05