Commit 4f36c623 authored by 张永's avatar 张永
parents 6b24f32a 8190468a
...@@ -95,9 +95,14 @@ ...@@ -95,9 +95,14 @@
<version>4.5.2</version> <version>4.5.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-httpclient</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>commons-httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>3.1</version> <version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.sun.mail</groupId> <groupId>com.sun.mail</groupId>
......
...@@ -426,24 +426,39 @@ public class FileUtil { ...@@ -426,24 +426,39 @@ public class FileUtil {
OutputStreamWriter osw = null; OutputStreamWriter osw = null;
Writer writer = null; Writer writer = null;
try { try {
if (!file.getParentFile().exists()) { File absFile = file.getAbsoluteFile();
file.getParentFile().mkdirs(); if (!absFile.getParentFile().exists()) {
absFile.getParentFile().mkdirs();
} }
fos = new FileOutputStream(file); fos = new FileOutputStream(absFile);
osw = new OutputStreamWriter(fos, "UTF-8"); osw = new OutputStreamWriter(fos, "UTF-8");
writer = new BufferedWriter(osw); writer = new BufferedWriter(osw);
for (String string : strings) { for (String string : strings) {
writer.append(string).append(System.getProperty("line.separator")); writer.append(string).append(System.getProperty("line.separator"));
} }
} catch (IOException e) { } catch (IOException e) {
throw new FileUtilException(e); throw new FileUtilException("", e);
} finally { } finally {
try { try {
writer.close(); if(writer != null) {
osw.close(); writer.close();
fos.close(); }
} catch (IOException e) { } catch (IOException e) {
throw new FileUtilException(e); e.printStackTrace();
}
try {
if(osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
} }
} }
} }
......
...@@ -70,7 +70,7 @@ public class HttpUtil { ...@@ -70,7 +70,7 @@ public class HttpUtil {
return sslContextDefault; return sslContextDefault;
} }
private static HttpURLConnection createConnection(String requestUrl, Proxy proxy) throws IOException{ public static HttpURLConnection createConnection(String requestUrl, Proxy proxy) throws IOException{
URL HTTP_URL = new URL(requestUrl); URL HTTP_URL = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) (proxy == null ? HTTP_URL.openConnection() : HTTP_URL.openConnection(proxy)); HttpURLConnection connection = (HttpURLConnection) (proxy == null ? HTTP_URL.openConnection() : HTTP_URL.openConnection(proxy));
if (requestUrl.startsWith("https")) { if (requestUrl.startsWith("https")) {
......
package com.egolm.common.jdbc;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import javax.sql.DataSource;
public class DBPort {
private DataSource dataSource;
public DBPort(DataSource dataSource) {
this.dataSource = dataSource;
}
public void doImport(File exportFile, String tableName) throws IOException, SQLException, ClassNotFoundException {
FileInputStream fis = null;
try {
fis = new FileInputStream(exportFile);
FileChannel channel = fis.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
int i = 1;
while(i > 0) {
i = channel.read(byteBuffer);
}
this.doImport(byteBuffer.array(), tableName);
} finally {
if(fis != null) {
fis.close();
}
}
}
public void doExport(File exportFile, String selectSql, Object[] args) throws IOException, SQLException {
if(!exportFile.getParentFile().exists()) {
exportFile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(exportFile);
this.doExport(fos, selectSql, args);
}
public void doExport(OutputStream os, String sql, Object[] args) throws IOException, SQLException {
try {
os.write(this.doExport(sql, args));
os.flush();
} finally {
if(os != null) {
os.close();
}
}
}
public byte[] doExport(String selectSql, Object[] args) throws IOException, SQLException {
Connection connection = dataSource.getConnection();
PreparedStatement ps = connection.prepareStatement(selectSql);
if(args != null) {
for (int i = 0; i < args.length; i++) {
Object argObj = args[i];
if (argObj instanceof java.util.Date) {
ps.setObject(i + 1, new Timestamp(((Date) argObj).getTime()));
} else if (argObj.getClass().isEnum()) {
ps.setObject(i + 1, args[i].toString());
} else {
ps.setObject(i + 1, args[i]);
}
}
}
ResultSet resultSet = ps.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
ArrayList<Map<String, Map<String, Object>>> datas = new ArrayList<Map<String, Map<String, Object>>>();
while (resultSet.next()) {
Map<String, Map<String, Object>> rowData = new LinkedHashMap<String, Map<String, Object>>();
for (int i = 1; i <= columnCount; i++) {
String colType = metaData.getColumnTypeName(i);
String columnLabel = metaData.getColumnLabel(i);
if (columnLabel == null || columnLabel.trim().length() == 0) {
columnLabel = metaData.getColumnName(i);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("type", colType);
map.put("value", resultSet.getObject(i));
rowData.put(columnLabel, map);
}
datas.add(rowData);
}
ByteArrayOutputStream bos = null;
GZIPOutputStream gzip = null;
ObjectOutputStream out = null;
try {
bos = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(bos);
out = new ObjectOutputStream(gzip);
out.writeObject(datas);
out.flush();
gzip.flush();
gzip.finish();
bos.flush();
return bos.toByteArray();
} finally {
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void doImport(byte[] data, String tableName) throws IOException, ClassNotFoundException, SQLException {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(bis);
ObjectInputStream out = new ObjectInputStream(gzip);
List<?> datas = (List<?>)out.readObject();
StringBuffer sql = new StringBuffer("insert into ").append(tableName).append("(");
Object first = datas.get(0);
Map<?, ?> firstMap = (Map<?, ?>) first;
int columnIndex = 0;
for(Object keyObject : firstMap.keySet()) {
String columnName = (String)keyObject;
sql.append(columnName);
if(columnIndex < firstMap.size() - 1) {
sql.append(", ");
} else {
sql.append(") values (");
for(int k = 0; k < firstMap.size(); k++) {
sql.append("?");
if(k < firstMap.size() - 1) {
sql.append(", ");
} else {
sql.append(")");
}
}
}
columnIndex += 1;
}
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
PreparedStatement ps = connection.prepareStatement(sql.toString());
for(int i = 0; i < datas.size(); i++) {
Object obj = datas.get(i);
Map<?, ?> map = (Map<?, ?>) obj;
int index = 0;
for(Object keyObject : map.keySet()) {
index += 1;
Object colObj = map.get(keyObject);
Map<?, ?> columnMap = (Map<?, ?>) colObj;
Object columnValue = columnMap.get("value");
ps.setObject(index, columnValue);
}
ps.addBatch();
}
ps.executeUpdate();
connection.commit();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment