本文講解如何用java實(shí)現(xiàn)把數(shù)據(jù)庫(kù)的數(shù)據(jù)寫入到txt中 并實(shí)現(xiàn)類似下載軟件的樣子在網(wǎng)頁(yè)中彈出下載.

package datatest;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.ConnDB;
public class export extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//設(shè)置編碼
response.setCharacterEncoding("UTF-8");
//連接數(shù)據(jù)庫(kù)
ConnDB conn = new ConnDB();
ServletOutputStream outputstream = null;
BufferedOutputStream buffoutputstream = null;
String txt_name = "導(dǎo)出的txt文件名.txt";//導(dǎo)出的txt文件名
try {
response.reset();// 清空輸出流
response.setContentType("text/plain;charset=utf-8");
//設(shè)置txt文件名稱編碼,防止中文亂碼
response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(txt_name, "UTF-8"));
StringBuffer write = new StringBuffer();
outputstream=response.getOutputStream();
buffoutputstream = new BufferedOutputStream(outputstream);
//根據(jù)id查詢數(shù)據(jù)庫(kù)
int id=Integer.parseInt(request.getParameter("id"));
String sql = "select a.id,name,account,password ";
sql+="from test_rank a ";
sql+="left join test_join b on b.id=a.id where a.id="+id;
ResultSet rs = conn.doQuery(sql);
String content="";
try {
while(rs.next())
{
//把數(shù)據(jù)庫(kù)中讀取的數(shù)據(jù)寫入
content=rs.getString("name")+"\r\n";//在txt中換行為\t\n
write.append(content);
content=rs.getString("account")+"\r\n";
write.append(content);
break;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write.append(content);
//設(shè)置編碼 防止中文亂碼
String str = new String(write.toString().getBytes(),"gbk");
buffoutputstream.write(str.toString().getBytes("gbk"));
buffoutputstream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (outputstream != null)
try {
outputstream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (buffoutputstream != null)
try {
buffoutputstream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
新聞標(biāo)題:java實(shí)現(xiàn)數(shù)據(jù)庫(kù)的數(shù)據(jù)寫入到txt的方法-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article34/dpgipe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、App開發(fā)、面包屑導(dǎo)航、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)