首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

項目實戰筆記:傳單下發業務全鏈路——HTTP 調用、數據庫操作、JSON/HTML 解析與文件分發

     編輯:Java 綜合教學

項目實戰筆記:傳單下發業務全鏈路——HTTP 調用、數據庫操作、JSON/HTML 解析與文件分發

記錄對接老系統(Struts+Spring+JdbcTemplate)傳單下發接口的全流程:新系統(SpringMVC+MyBatis)通過 HTTP POST 調用,老系統服務層處理業務邏輯,涉及數據庫批量增刪改、JSON 名片數據替換、Jsoup 靜態 HTML 重寫、文件復制上傳雲存儲。

1. HTTP 遠程調用工具類

public String postTelentService(String url, String params) throws IOException {
    URL u = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true); conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setUseCaches(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.connect();
    try (DataOutputStream out = new DataOutputStream(conn.getOutputStream())) {
        out.writeBytes(params);
        out.flush();
    }
    try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) sb.append(line).append("\n");
        return new String(sb.toString().getBytes("GBK"), "UTF-8");
    }
}

2. Controller 調用

String url = "https://" + Config.getMkpUrl() + "/marketa/remotePassFlyers.do";
String params = "userId=" + userId + "&maId=" + maId + "&flyerId=" + flyerId
              + "&userIds=" + userIds + "&assUserIds=" + assUserIds;
String result = postTelentService(url, params);

3. 老系統 Struts Action 入口

public String remotePassFlyers() {
    Integer userId = Integer.parseInt(request.getParameter("userId"));
    Integer maId = Integer.parseInt(request.getParameter("maId"));
    Integer flyerId = Integer.parseInt(request.getParameter("flyerId"));
    String userIds = request.getParameter("userIds");
    String assUserIds = request.getParameter("assUserIds");
    marketaService.passFlyerToUsers(userId, maId, flyerId, userIds, assUserIds);
    return SUCCESS;
}

4. Service 核心邏輯 passFlyerToUsers

  • 查詢目標用戶信息(左連接 flyer 表),分為 已有傳單 與 無傳單 兩組。
  • 已有組:批量更新 d_flyer 主表與 d_flyer_detail 子表(動態 SQL 多表 UPDATE)。
  • 無組:批量 INSERT 複製模板傳單到 d_flyer,同步 INSERT 子表詳情。
  • 重新查詢所有下發傳單,調用 refreshNameCard 替換名片 JSON,調用 uploadShareFlyerHtmlFile 生成靜態 HTML 並上傳。

5. 關鍵輔助方法

方法功能
getInvitedUserInfos左連接查詢用戶與傳單關聯,返回 user_id + flyer_id
getSharedFlyerDetaiInfo多表連接獲取傳單詳情(名片字段:email/mobile/qq/weixin/realname/job)
castArrayToString數組/集合轉逗號分隔字符串,用於 SQL IN 子句
getPassFlyerSql(type)根據類型返回 5 種動態 SQL(更新主表/子表、插入主表/子表、批量複製)
refreshNameCardGson 解析 JSON content,定位 wd_u_name=bio 節點,替換 realname/qq/tell/email/phone/weixin/job/company
replaceCardInfo按字段名從用戶信息 Map 取值覆蓋,缺失保留原值
updateDetailContent更新 d_flyer_detail.content 字段
uploadShareFlyerHtmlFile復制模板 HTML → Jsoup 解析替換 data-key 屬性元素文本 → 寫回文件 → 上傳雲存儲
reloadCardHtmlJsoup 選擇 [data-key=realname/qq/tell/...] 設置文本,輸出 HTML

6. 工具類

  • JsonUtil:Gson 實現 jsonToMap/objToJson。
  • FileUtil:緩衝流 100KB 分塊復制文件。

7. JSON Content 結構示例

{
  "widgets": [
    {"wd_u_name":"title", "value":"...", "color":"#000", "fontSize":"25px", ...},
    {"wd_u_name":"feedback", "btnLabel":"搶購", "items":[{"name":"name",...}, ...]},
    {"wd_u_name":"bio", "company":"...", "job":"...", "realname":"...", "qq":"", "tell":"...", ...}
  ],
  "meta": {"color":"#fedac2", "bgImg":"/flyer/images/flyerimg/flyer_bg_6.jpg", ...}
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved