当前位置:首页 > 开发 > 编程语言 > Java > 正文

java导出txt文件的例子

发表于: 2014-06-18   作者:知了ing   来源:转载   浏览:
摘要: 代码很简单就一个servlet,如下: package com.eastcom.servlet; import java.io.BufferedOutputStream; import java.io.IOException; import java.net.URLEncoder; import java.sql.Connection; import java.sql.Resu
代码很简单就一个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导出txt文件的例子

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号