`
zhang_zling
  • 浏览: 96973 次
  • 性别: Icon_minigender_2
  • 来自: 济南
社区版块
存档分类
最新评论

DataSync 数据同步

 
阅读更多
package com.nova.common;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class DataSync
*/
@WebServlet("/dataSync")
public class DataSync extends HttpServlet {
private static final long serialVersionUID = 1L;

private Connection conn;
private String filePath;

private String orgProvinceId;
private String orgCityId;
private String orgDistrictId;
private String orgTownId;
private String orgVillageId;

/**
* @see HttpServlet#HttpServlet()
*/
public DataSync() {
super();
// TODO Auto-generated constructor stub
}

/**
* @param path
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

PrintWriter out=response.getWriter();
    
out.print("1323443554qqqqqqqqqqq");
// orgProvinceId
orgProvinceId = request.getParameter("orgProvinceId");
// orgCityId
orgCityId = request.getParameter("orgCityId");
// orgDistrictId
orgDistrictId = request.getParameter("orgDistrictId");
// orgTownId
orgTownId = request.getParameter("orgTownId");
// orgVillageId
orgVillageId = request.getParameter("orgVillageId");
String fileName = orgProvinceId + orgCityId + orgDistrictId + orgTownId
+ orgVillageId+".sql";
filePath=request.getRealPath("/") + "/" + fileName;
filePath = new String(filePath.getBytes("ISO-8859-1"), "utf-8");
download(filePath, request, response);
}

private void getDBData() {
try {

Properties props = new Properties();
props.load(DataSync.class
.getResourceAsStream("/configs/database/oracle.properties"));
String driver = props.getProperty("db_driver");
String url = props.getProperty("db_url");
String username = props.getProperty("db_user_name");
String password = props.getProperty("db_password");
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
String sql = "{call out_file(?,?,?,?,?,?)}";
CallableStatement cst = conn.prepareCall(sql);
// 输入参数的调用
cst.setString(1, orgProvinceId);
cst.setString(2, orgCityId);
cst.setString(3, orgDistrictId);
cst.setString(4, orgTownId);
cst.setString(5, orgVillageId);
// 输出参数的调用
cst.registerOutParameter(6, java.sql.Types.CLOB);
// 执行存储过程
cst.execute();
// 得到存储过程的输出参数值
Clob syncContent =cst.getClob(6);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

public HttpServletResponse download(String path,
HttpServletRequest request, HttpServletResponse response) {
Reader reader = null;
OutputStreamWriter writerClient = null;
try {
// path是指欲下载的文件的路径。
File file = new File(request.getRealPath("/") + "/" + path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
            response.reset();
// 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("utf-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
Clob syncContent = getDBData();
reader = syncContent.getCharacterStream();
char[] buffer = new char[1024];
int length = 0;
writerClient = new OutputStreamWriter(response.getOutputStream());
while ((length = reader.read(buffer)) != -1) {
System.out.println(buffer);
writerClient.write(buffer, 0, length);
}
writerClient.flush();

} catch (IOException ex) {
ex.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
writerClient.close();
reader.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return response;
}
// <a href="fileDownLoadServlet?filename=通讯录.xls">哈哈,测试文件下载</a>
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics