java导出txt文件的例子

代码很简单就一个servlet,如下:
package com.eastcom.servlet;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eastcom.jdbc.JdbcFactory;

public class ExportConstractTxt extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 response.setContentType("text/plain"); 
         String fileName = URLEncoder.encode("配置文件比对结果", "UTF-8");  
         response.setHeader("Content-Disposition","attachment; filename=" + fileName + ".txt");  
         String device_id=request.getParameter("device_id");
         String time_tamp=request.getParameter("time_tamp");
        // result =new String(result.getBytes("iso-8859-1"), "utf-8");
         String result=getDIFF(device_id,time_tamp);
         //result =new String(result.getBytes("iso-8859-1"), "gbk");
         System.out.println(result);
         BufferedOutputStream buff = null;     
         StringBuffer write = new StringBuffer();     
         String enter = "\r\n";     
         ServletOutputStream outSTr = null;     
         try {     
              outSTr = response.getOutputStream();  // 建立     
              buff = new BufferedOutputStream(outSTr);     
              write.append(result.replaceAll("\n", 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();     
            }     
        }     
	}
	private String getDIFF(String deviceID,String time){
		String result="";
		Connection Conn=null;
		ResultSet rSet=null;
		Statement stmt=null;
		try {
			Conn = JdbcFactory.getInstance().getConnection();
			stmt = Conn.createStatement();
			String sql ="select  diff "+
						"  from rm.RM_CONF_FILE_DIFF  "+
					"	 where device_id = '"+deviceID+"' and to_char(time_stamp, 'yyyy-mm-dd hh24:mi:mm') = '"+time+"' ";
			System.out.println("getDIFF sql >"+sql);
			rSet=stmt.executeQuery(sql);
			while(rSet.next()){
				result=rSet.getString("diff");
			}
		} catch (Exception e) {
				e.printStackTrace();
		}finally{
			if(stmt!=null){
				try {
					stmt.close();
				} catch (Exception e) {
				}
			}
			if(Conn!=null){
				try {
					Conn.close();
				} catch (Exception e) {
				}
			}
		}
		return result;
	}
}

你可能感兴趣的:(java,servlet)