Java導(dǎo)出txt文件的方法
本文實(shí)例講述了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指定默認(rèn)的名字
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); //序號(hào)
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();
}
}
}
}
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Spring實(shí)現(xiàn)動(dòng)態(tài)切換多數(shù)據(jù)源的解決方案
這篇文章主要給大家介紹了Spring實(shí)現(xiàn)動(dòng)態(tài)切換多數(shù)據(jù)源的解決方案,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-01-01
聊聊RabbitMQ發(fā)布確認(rèn)高級(jí)問題
這篇文章主要介紹了RabbitMQ發(fā)布確認(rèn)高級(jí)問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
crawler4j抓取頁(yè)面使用jsoup解析html時(shí)的解決方法
crawler4j對(duì)response沒有指定編碼的頁(yè)面,解析成亂碼,很讓人煩惱,下面給出解決方法,需要的朋友可以參考下2014-04-04
java并發(fā)編程專題(五)----詳解(JUC)ReentrantLock
這篇文章主要介紹了java(JUC)ReentrantLock的的相關(guān)資料,文中講解非常詳細(xì),實(shí)例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐記錄
這篇文章主要介紹了SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
maven一鍵刪除倉(cāng)庫(kù)無(wú)用文件的實(shí)現(xiàn)
大家都知道我們?cè)谑褂肕aven的時(shí)候,會(huì)下載一堆無(wú)用非jar文件,本文主要介紹了maven一鍵刪除倉(cāng)庫(kù)無(wú)用文件的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11

