99 lines
2.6 KiB
Java
99 lines
2.6 KiB
Java
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.net.UnknownHostException;
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
import java.util.zip.GZIPOutputStream;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import com.fangjia.datastore.driver.DSCollection;
|
|
import com.fangjia.datastore.driver.DataStore;
|
|
import com.mongodb.BasicDBObject;
|
|
import com.mongodb.DBObject;
|
|
|
|
public class SaveToWebpage {
|
|
|
|
static DSCollection dsCollection = null;
|
|
|
|
public static void DataSaveToWebpage(String city , String siteName , String pageName , String Category, String url, String content) {
|
|
if(dsCollection == null){
|
|
try {
|
|
dsCollection = new DataStore("192.168.0.51", 27001).getCollection("fangjia.webpage");
|
|
} catch (UnknownHostException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
if(content == null || city ==null || siteName == null || pageName == null || Category == null || url == null) {
|
|
return;
|
|
}
|
|
|
|
DBObject q = new BasicDBObject();
|
|
q.put("city", city);
|
|
q.put("site_name", siteName);
|
|
q.put("page_name", pageName);
|
|
q.put("category", Category);
|
|
q.put("url", url);
|
|
DBObject webpageEntity = dsCollection.findOne(q);
|
|
if(webpageEntity !=null) {
|
|
DBObject udata = new BasicDBObject();
|
|
try {
|
|
udata.put("content", zip(content));
|
|
} catch (IOException e) {
|
|
|
|
}
|
|
udata.put("status", 0);
|
|
udata.put("m_date", new Date());
|
|
dsCollection.update(new BasicDBObject("_id", webpageEntity.get("_id")), new BasicDBObject("$set", udata));
|
|
}else {
|
|
DBObject ret = new BasicDBObject();
|
|
ret.put("category", Category);
|
|
ret.put("city", city);
|
|
try {
|
|
ret.put("content", zip(content));
|
|
} catch (IOException e) {
|
|
|
|
}
|
|
ret.put("c_date", new Date());
|
|
ret.put("m_date", new Date());
|
|
ret.put("page_name", pageName);
|
|
ret.put("site_name", siteName);
|
|
ret.put("status", 0);
|
|
ret.put("url", url);
|
|
|
|
dsCollection.save(ret);
|
|
}
|
|
|
|
}
|
|
/**
|
|
* ѹËõ
|
|
*
|
|
* @param content
|
|
* @return
|
|
* @throws IOException
|
|
*/
|
|
public static byte[] zip(byte[] content) throws IOException {
|
|
ByteArrayOutputStream bao = new ByteArrayOutputStream();
|
|
GZIPOutputStream outputStream = new GZIPOutputStream(bao);
|
|
outputStream.write(content);
|
|
outputStream.finish();
|
|
return bao.toByteArray();
|
|
}
|
|
|
|
public static byte[] zip(String content) throws IOException {
|
|
if (StringUtils.isBlank(content)) {
|
|
return null;
|
|
}
|
|
|
|
ByteArrayOutputStream bao = new ByteArrayOutputStream();
|
|
GZIPOutputStream outputStream = new GZIPOutputStream(bao);
|
|
outputStream.write(content.getBytes("UTF-8"));
|
|
outputStream.finish();
|
|
return bao.toByteArray();
|
|
}
|
|
}
|