上一周,由于公司业务需求,需要根据表中的url生成图片,翻阅了一天资料之后,发现只有一篇博文基本可以参考:http://blog.csdn.net/cping1982/article/details/5353049/,其他博文,大部分都是和该篇博文大差不差。该篇博文中,很显然,只有第三种合适。所以我也模仿写了一个根据url截图的,写完测试后,10条之内的截图还可以,但我数据库中数据量过大,根据该方法生成截图效率很低,经不起大数据的考验。所以就又写了一
个根据swingworker生成截图的,该方法在速度上有明显的提升,上代码:
1、TestSwingWorker是入口类
方法
package com;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.demo.EagleBrowser;
import com.mysql.jdbc.PreparedStatement;
public class TestSwingWorker extends JFrame implements ActionListener {
private static final long serialVersionUID = -6147740041804837545L;
public static void main(String[] args) {
try {
ConstantsUtils.init();
TestSwingWorker main = new TestSwingWorker();
main.setVisible(true); // 设置窗体可见
} catch (Exception e) {
e.printStackTrace();
}
}
Container p = getContentPane();
public static JButton updateButton;
public static JTextField draftIdTextField;
public static JLabel infoLable;
public static JLabel infoLable1;
public static JLabel infoLable2;
public static int totalCount;
public TestSwingWorker() {
super();
this.setTitle("更新截图");
this.setBounds(200, 200, 350, 260); // setSize(int width,int
// hight);setBounds(x,y,width,hight);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p.setLayout(null);
JLabel label1 = new JLabel("稿件ID");
label1.setBounds(80, 10, 80, 30);
p.add(label1);
draftIdTextField = new JTextField();
draftIdTextField.setBounds(130, 10, 120, 30);
p.add(draftIdTextField);
updateButton = new JButton("更新");
updateButton.setBounds(80, 48, 180, 30);
updateButton.addActionListener(this); // 添加鼠标响应事件
p.add(updateButton);
infoLable = new JLabel();
p.add(infoLable);
infoLable.setBounds(100, 100, 300, 30);
infoLable1 = new JLabel();
p.add(infoLable1);
infoLable1.setBounds(100, 125, 300, 30);
}
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (jb == updateButton) {
String id = draftIdTextField.getText();
if(null == id || "".equals(id)){
infoLable.setText("稿件ID不能为空!");
}else{
try {
updateShortCutImgURL(id);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
/**
* 更新大数据表中的截图url
*
* @param draftPublishedList
*/
private void updateShortCutImgURL(String draftId) throws Exception {
Connection conn = DBUtil.getConnection();
String sql = "select id,url from tb_draft_published_list where draft_id = "
+ Integer.parseInt(draftId)
+ " and short_cut_Img is null and url is not null";
PreparedStatement stmt = (PreparedStatement) conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(sql);
//计算该稿件在大数据表中的个数
String countSql = "select count(*) as rowCount from tb_draft_published_list where draft_id = "
+ Integer.parseInt(draftId)
+ &