Java tutorial
package org.apache.commons.g; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.UIManager; import org.apache.commons.g.a.Config; import org.apache.commons.g.a.Config.PathType; import org.apache.commons.g.a.HttpUtil; import org.apache.commons.g.a.MyDialog; import org.apache.commons.g.a.Util; import org.apache.commons.g.a.XssfUtil; import org.apache.http.Header; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.util.TextUtils; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class QueryStorePicUI extends JFrame { private static final long serialVersionUID = 6357257504691733221L; private JFrame mContext = null; private HttpUtil mHttpUtil = null; public QueryStorePicUI(JFrame frame) { mContext = this; mHttpUtil = new HttpUtil(true); Font commonFont = new Font("", Font.PLAIN, 12); Font textFont = new Font("", Font.PLAIN, 16); Font titleFont = new Font("", Font.ITALIC, 12); UIManager.getDefaults().put("TextField.inactiveForeground", Color.darkGray); UIManager.getDefaults().put("Label.font", commonFont); UIManager.getDefaults().put("Menu.font", commonFont); UIManager.getDefaults().put("MenuBar.font", commonFont); UIManager.getDefaults().put("MenuItem.font", commonFont); UIManager.getDefaults().put("TitledBorder.font", titleFont); UIManager.getDefaults().put("TextField.font", textFont); UIManager.getDefaults().put("Button.font", textFont); //UIManager.getDefaults().put("TextArea.font", textFont); this.setLayout(null); ImageIcon mImageIcon = new ImageIcon("res/gitzo.jpeg"); this.setIconImage(mImageIcon.getImage()); // ********************** *********************************** final JTextArea textarea_username = new JTextArea("??"); textarea_username.setBounds(10, 25, 50, 20); textarea_username.setEditable(false); textarea_username.setBackground(getBackground()); textarea_username.setFont(textFont); final JTextField textfield_username = new JTextField(); textfield_username.setBounds(70, 20, 300, 35); textfield_username.setText(Config.Username); final JTextArea textarea_passwd = new JTextArea("?"); textarea_passwd.setBounds(10, 65, 50, 20); textarea_passwd.setEditable(false); textarea_passwd.setBackground(getBackground()); textarea_passwd.setFont(textFont); final JPasswordField textfield_passwd = new JPasswordField(); textfield_passwd.setBounds(70, 60, 300, 35); textfield_passwd.setText(Config.Password); JPanel select = new JPanel(null); select.setBounds(10, 20, 380, 105); select.setBorder(BorderFactory.createTitledBorder("OpenAM")); select.add(textarea_username); select.add(textfield_username); select.add(textarea_passwd); select.add(textfield_passwd); this.add(select); // ****************** ********************************* JButton button_login = new JButton(""); button_login.setHorizontalTextPosition(SwingConstants.CENTER); button_login.setVerticalTextPosition(SwingConstants.BOTTOM); button_login.setBounds(400, 30, 85, 40); this.add(button_login); final JButton button_logout = new JButton(""); button_logout.setHorizontalTextPosition(SwingConstants.CENTER); button_logout.setVerticalTextPosition(SwingConstants.BOTTOM); button_logout.setBounds(500, 30, 85, 40); button_logout.setEnabled(Config.JSESSIONID != null); this.add(button_logout); final JButton button_run = new JButton(""); button_run.setHorizontalTextPosition(SwingConstants.CENTER); button_run.setVerticalTextPosition(SwingConstants.BOTTOM); button_run.setBounds(400, 80, 185, 40); button_run.setEnabled(Config.JSESSIONID != null); this.add(button_run); // ********************** ***************************** mTextArea_ShowLog = new JTextArea(); mTextArea_ShowLog.setLineWrap(true); mTextArea_ShowLog.setEditable(false); mTextArea_ShowLog.setAutoscrolls(true); JScrollPane log = new JScrollPane(mTextArea_ShowLog); log.setBounds(10, 20, 560, 315); bar = log.getVerticalScrollBar(); JPanel showLog = new JPanel(null); showLog.setBounds(10, 130, 580, 340); showLog.setBorder(BorderFactory.createTitledBorder(":")); showLog.add(log); this.add(showLog); button_login.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { @Override public void run() { try { String username = textfield_username.getText(); @SuppressWarnings("deprecation") String password = textfield_passwd.getText(); // ??? XssfUtil.savePath(PathType.Username, username); XssfUtil.savePath(PathType.Password, password); boolean result = login(username, password); button_logout.setEnabled(result); button_run.setEnabled(result); } catch (Exception e) { Util.printError(e); } } }).start(); } }); button_logout.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { @Override public void run() { try { boolean result = logout(); button_logout.setEnabled(!result); button_run.setEnabled(!result); } catch (Exception e) { Util.printError(e); } } }).start(); } }); button_run.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Thread(new Runnable() { @Override public void run() { try { queryStorePic(Config.FilePath, Config.OutPath); } catch (Exception e) { Util.printError(e); } } }).start(); } }); this.setTitle(""); this.setSize(600, 500); setLocationRelativeTo(getOwner()); this.setResizable(false); this.setVisible(true); } /** */ private static JTextArea mTextArea_ShowLog = null; private static JScrollBar bar = null; public static void appendLog(String text) { mTextArea_ShowLog.append(text + "\n"); bar.setValue(bar.getMaximum()); System.out.println(); System.out.println(text); } private boolean logout() throws Exception { System.out.println(); appendLog("OpenAM..."); final MyDialog dialog = new MyDialog(mContext, "OpenAM..."); final HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/x-www-form-urlencoded"); headers.put("Host", "homesis.homecredit.cn"); headers.put("Origin", "https://homesis.homecredit.cn"); headers.put("Referer", "https://homesis.homecredit.cn/hsis/index.html"); headers.put("Cookie", Config.JSESSIONID); HttpGet get = mHttpUtil.doGet("https://homesis.homecredit.cn/hsis/j_spring_security_logout", headers); Header[] cookies = mHttpUtil.executeForCookies(get); String JSESSIONID = null; for (int i = 0; i < cookies.length; i++) { String cookie = cookies[i].getValue(); if (cookie.contains("JSESSIONID")) { System.out.println(cookie); JSESSIONID = cookie.substring(cookie.indexOf(";")); break; } } if (JSESSIONID == null) { dialog.dispose(0); appendLog("?"); return false; } appendLog("?"); Config.JSESSIONID = null; dialog.dispose(1); return true; } private boolean login(String username, String passwd) throws Exception { System.out.println(); appendLog("OpenAM..."); final MyDialog dialog = new MyDialog(mContext, "OpenAM..."); final HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/x-www-form-urlencoded"); headers.put("Host", "homesis.homecredit.cn"); headers.put("Origin", "https://homesis.homecredit.cn"); headers.put("Referer", "https://homesis.homecredit.cn/opensso/UI/Login"); headers.put("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"); // HttpGet get = new HttpGet("https://homesis.homecredit.cn/opensso/UI/Login"); Header[] cookies = mHttpUtil.executeForCookies(get); String AMAuthCookie = null; String JSESSIONID = null; for (int i = 0; i < cookies.length; i++) { String cookie = cookies[i].getValue(); if (cookie.contains("AMAuthCookie") && AMAuthCookie == null) { AMAuthCookie = cookie.substring(0, cookie.indexOf(";")); System.out.println(AMAuthCookie); } else if (cookie.contains("JSESSIONID")) { JSESSIONID = cookie.substring(0, cookie.indexOf(";")); System.out.println(JSESSIONID); } } if (AMAuthCookie == null || JSESSIONID == null) { dialog.dispose(0); appendLog("OpenAM?"); return false; } System.out.println(); appendLog("..."); dialog.setWorkText("..."); String data = "IDToken1=" + username + "&IDToken2=" + passwd + "&IDButton=&goto=aHR0cHM6Ly9ob21lc2lzLmhvbWVjcmVkaXQuY24vaHNpcy9pbmRleC5odG1s&gotoOnFail=&SunQueryParamsString=&encoded=true&gx_charset=UTF-8"; //System.out.println(data); headers.put("Cookie", "amlbcookie=01; " + JSESSIONID + "; " + AMAuthCookie); HttpPost post = mHttpUtil.doPost("https://homesis.homecredit.cn/opensso/UI/Login", headers, data); cookies = mHttpUtil.executeForHeaders(post); boolean loginSuccess = true; String iPlanetDirectoryPro = null; for (int i = 0; i < cookies.length; i++) { Header cookie = cookies[i]; String name = cookie.getName(); String value = cookie.getValue(); //System.out.println(name+"-"+value); if ("X-AuthErrorCode".equals(name)) { if ("-1".equals(value)) loginSuccess = false; System.out.println(cookie); } else if ("Set-Cookie".equals(name) && value.contains("iPlanetDirectoryPro") && iPlanetDirectoryPro == null) { int index = value.indexOf("iPlanetDirectoryPro"); iPlanetDirectoryPro = value.substring(index, value.indexOf(";", index)); System.out.println(iPlanetDirectoryPro); } } if (!loginSuccess) { dialog.dispose(0); appendLog("?(???)"); return false; } if (iPlanetDirectoryPro == null) { System.out.println(); appendLog("..."); dialog.setWorkText("..."); data = "IDButton=Continue+to+application&goto=aHR0cHM6Ly9ob21lc2lzLmhvbWVjcmVkaXQuY24vaHNpcy9pbmRleC5odG1s&gotoOnFail=&SunQueryParamsString=&encoded=true&gx_charset=UTF-8"; post = mHttpUtil.doPost("https://homesis.homecredit.cn/opensso/UI/Login", headers, data); cookies = mHttpUtil.executeForCookies(post); for (int i = 0; i < cookies.length; i++) { String cookie = cookies[i].getValue(); if (cookie.contains("iPlanetDirectoryPro")) { System.out.println(cookie); iPlanetDirectoryPro = cookie.substring(0, cookie.indexOf(";")); break; } } } if (iPlanetDirectoryPro == null) { dialog.dispose(0); appendLog("?(???)"); return false; } System.out.println(); appendLog("..."); dialog.setWorkText("..."); headers.put("Cookie", "amlbcookie=01; " + JSESSIONID + "; " + iPlanetDirectoryPro); get = mHttpUtil.doGet("https://homesis.homecredit.cn/hsis/index.html", headers); cookies = mHttpUtil.executeForCookies(get); JSESSIONID = null; for (int i = 0; i < cookies.length; i++) { String cookie = cookies[i].getValue(); if (cookie.contains("JSESSIONID")) { JSESSIONID = cookie.substring(0, cookie.indexOf(";")); System.out.println(JSESSIONID); break; } } if (JSESSIONID == null) { dialog.dispose(0); appendLog("?"); return false; } Config.JSESSIONID = JSESSIONID; appendLog("?"); dialog.dispose(1); return true; } /** * ?? * @param path * @throws Exception */ public void queryStorePic(String filePath, String outPath) throws Exception { final MyDialog dialog = new MyDialog(mContext, " ..."); final List<String> noPics = new ArrayList<String>(); final List<String> noStores = new ArrayList<String>(); InputStream in = new FileInputStream(filePath); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(in); final XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); int rows = xssfSheet.getLastRowNum(); if (rows > 100) { rows = 100; } System.out.println(); System.out.println(" " + rows + "..."); dialog.setWorkText(" " + rows + "..."); HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json"); headers.put("Cookie", Config.JSESSIONID); headers.put("Referer", "https://homesis.homecredit.cn/hsis/index.html"); final XSSFCell cell0 = xssfSheet.getRow(0).createCell(0); cell0.setCellValue(""); System.out.println("?? | | ? | ?? | ??"); for (int i = 1; i <= rows; i++) { final XSSFRow xssfRow = xssfSheet.getRow(i); if (xssfRow == null) continue; // ?? String visitName = XssfUtil.getCellValue(xssfRow.getCell(0)); if (TextUtils.isEmpty(visitName)) continue; // ? final String storeCode = XssfUtil.getCellValue(xssfRow.getCell(3)); final String data = "{\"code\":\"" + storeCode + "\",\"name\":null,\"licenseNumber\":null,\"salesDistrictId\":null,\"offset\":0,\"limit\":15}"; // ?? final String storeName = XssfUtil.getCellValue(xssfRow.getCell(10)); final XSSFCell cell = xssfRow.createCell(0); String space2 = " "; // ??2 if (storeName.length() < 20) space2 += Util.getFormateSpace(20 - storeName.length()); ; String space = " "; // ??2 if (visitName.length() == 3) space = space.substring(2); // ????? if (i < 10) { System.out.print( " " + i + " " + visitName + space + storeCode + " " + storeName + space2); } else { System.out.print( " " + i + " " + visitName + space + storeCode + " " + storeName + space2); } dialog.setWorkText(" " + storeCode + "-" + storeName); Thread.sleep(1234); String store = null; boolean run = true; while (run) { try { // ? HttpPost seachStore = mHttpUtil .doPost("https://homesis.homecredit.cn/hsis/api/salesroom/search", headers, data); store = mHttpUtil.executeForBody(seachStore); //System.out.println(store); run = false; } catch (Exception e) { System.out.print("/"); } } if (store.contains("HTTP/1.1 500 Internal Server Error")) { System.out.println("?(?)"); break; } JSONObject json = null; try { json = JSONObject.parseObject(store); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println(store); continue; } if (json == null) { System.out.println("?"); continue; } JSONArray array = json.getJSONArray("data"); if (array.size() == 0) { noStores.add(storeCode); cell.setCellValue("?"); System.out.println("?"); continue; } JSONObject obj = (JSONObject) array.get(0); // ID int id = obj.getIntValue("id"); //System.out.println(id); String storePic = null; run = true; while (run) { try { // HttpGet seachPic = mHttpUtil .doGet("https://homesis.homecredit.cn/hsis/api/document/salesroom/all/" + id, headers); storePic = mHttpUtil.executeForBody(seachPic); // System.out.println(storePic); run = false; } catch (Exception e) { System.out.print("/"); } } json = JSONObject.parseObject(storePic); array = json.getJSONArray("documents"); if (array.size() == 0) { noPics.add(storeCode); cell.setCellValue(""); System.out.println(""); continue; } boolean print = true; for (int j = 0; j < array.size(); j++) { obj = (JSONObject) array.get(j); // ID int picid = obj.getIntValue("id"); String fileName = obj.getString("fileName"); if (fileName.contains("POSRepID")) continue; // fileName = fileName.substring(0, fileName.indexOf("-"));//+fileName.substring(fileName.indexOf(".")); run = true; while (run) { try { // HttpGet down = mHttpUtil.doGet( "https://homesis.homecredit.cn/hsis/api/document/download/" + picid, headers); String result = mHttpUtil.downPic(down, Util .mkdirsIfNeed(outPath + File.separator + visitName, storeCode + "-" + fileName)); if (print) { print = false; System.out.println(fileName.substring(0, fileName.indexOf("-"))); } run = false; } catch (Exception e) { System.out.println(); e.printStackTrace(); System.out.println(); System.out.println("?/"); System.out.println(); } } } } xssfWorkbook.write(new FileOutputStream(filePath.replace(".xlsx", "_pic.xlsx"))); xssfWorkbook.close(); xssfWorkbook = null; in.close(); in = null; if (noStores.size() > 0) { System.out.println(); System.out.println(); System.out.println("??"); System.out.println( ""); for (int k = 0; k < noStores.size(); k++) { System.out.println(noStores.get(k)); } } if (noPics.size() > 0) { System.out.println(); System.out.println(); System.out.println("?"); System.out.println( ""); for (int k = 0; k < noPics.size(); k++) { System.out.println(noPics.get(k)); } } dialog.dispose(2); } }