首頁 微信小程式 小程式開發 Java實作微信公眾號與掃碼支付的案例

Java實作微信公眾號與掃碼支付的案例

May 16, 2017 am 11:17 AM
java

微信支付已成為生活中必不可少的付款方式,這篇文章主要介紹了Java微信支付之公眾號支付、掃碼支付,有需要的小伙伴可以了解一下。

微信支付現在已經變得越來越流行了,隨之也出現了很多以可以快速接入微信支付為噱頭的產品,不過方便之餘也使得我們做東西慢慢依賴第三方,喪失了獨立思考的能力,這次打算分享下我之前開發過的微信支付。

一、H5公眾號支付

重點:正確取得openId以及統一下單介面,正確處理付款結果通知,正確設定支付授權目錄

H5的付款方式是使用較為廣泛的方式,這種支付方式主要用於微信內自訂選單的網頁,依賴手機上安裝的微信客戶端,高版本的微信才支援微信支付,下面按我的流程注意說明

#1  編寫用於支付的頁面,由於是測試用就寫的簡單了點

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>"> 
  
 <title>微信支付样例</title> 
  
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> 
 <!-- 
 <link rel="stylesheet" type="text/css" href="styles.css"> 
 --> 
 
 </head> 
 
 <body> 
 <form action="oauthServlet" method="POST"> 
    订单号:<input type="text" name="orderNo" /> 
  <input type="submit" value="H5支付"/> 
 </form> 
 </br></br> 
  <form action="scanCodePayServlet?flag=createCode" method="POST"> 
    订单号:<input type="text" name="orderNo" /> 
  <input type="submit" value="扫码支付"/> 
 </form> 
 </body> 
</html>
登入後複製

2  編寫一個servlet用於透過Oauth獲取code

package com.debug.weixin.servlet; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import com.debug.weixin.util.CommonUtil; 
import com.debug.weixin.util.ServerConfig; 
public class OauthServlet extends HttpServlet { 
 
 public void doGet(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
 
  this.doPost(request, response); 
 } 
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
 
   String orderNo=request.getParameter("orderNo"); 
   //调用微信Oauth2.0获取openid 
   String redirectURL=ServerConfig.SERVERDOMAIN+"/BasicWeixin/payServletForH5?orderNo="+orderNo; 
   String redirectURI=""; 
   try { 
    redirectURI=CommonUtil.initOpenId(redirectURL); 
   } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
   } 
   //System.out.println(redirectURI); 
   //RequestDispatcher dis= request.getRequestDispatcher(redirectURI); 
   //dis.forward(request, response); 
   response.sendRedirect(redirectURI); 
 } 
}
登入後複製

3 獲取到code後,透過REDIRECTURI獲取openId,調用統一下單接口

package com.debug.weixin.servlet; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.SortedMap; 
import java.util.TreeMap; 
 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import com.debug.weixin.pojo.WeixinOauth2Token; 
import com.debug.weixin.pojo.WeixinQRCode; 
import com.debug.weixin.util.AdvancedUtil; 
import com.debug.weixin.util.CommonUtil; 
import com.debug.weixin.util.ConfigUtil; 
import com.debug.weixin.util.PayCommonUtil; 
 
public class PayServletForH5 extends HttpServlet { 
 
  
 public void doGet(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
 
  this.doPost(request, response); 
 } 
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
   String orderNo=request.getParameter("orderNo"); 
   String code=request.getParameter("code"); 
   
   //获取AccessToken 
   
   WeixinOauth2Token token=AdvancedUtil.getOauth2AccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code); 
   
   String openId=token.getOpenId(); 
   
   //调用微信统一支付接口 
   SortedMap<Object, Object> parameters = new TreeMap<Object, Object>(); 
  parameters.put("appid", ConfigUtil.APPID); 
 
  parameters.put("mch_id", ConfigUtil.MCH_ID); 
  parameters.put("device_info", "1000"); 
  parameters.put("body", "我的测试订单"); 
  parameters.put("nonce_str", PayCommonUtil.CreateNoncestr()); 
   
    
  parameters.put("out_trade_no", orderNo); 
  //parameters.put("total_fee", String.valueOf(total)); 
  parameters.put("total_fee", "1"); 
  parameters.put("spbill_create_ip", request.getRemoteAddr()); 
  parameters.put("notify_url", ConfigUtil.NOTIFY_URL); 
  parameters.put("trade_type", "JSAPI"); 
  parameters.put("openid", openId); 
 
  String sign = PayCommonUtil.createSign("UTF-8", parameters); 
  parameters.put("sign", sign); 
 
  String requestXML = PayCommonUtil.getRequestXml(parameters); 
 
  String result = CommonUtil.httpsRequestForStr(ConfigUtil.UNIFIED_ORDER_URL,"POST", requestXML); 
  System.out.println("----------------------------------"); 
  System.out.println(result); 
  System.out.println("----------------------------------"); 
   
  request.setAttribute("orderNo", orderNo); 
  request.setAttribute("totalPrice", "0.01"); 
  String payJSON=""; 
  try { 
   payJSON=CommonUtil.getH5PayStr(result,request); 
    
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  //System.out.println(payJSON); 
  request.setAttribute("unifiedOrder",payJSON); 
   
  RequestDispatcher dis= request.getRequestDispatcher("h5Pay.jsp"); 
  dis.forward(request, response); 
 } 
}
登入後複製

調用微信統一下單接口,需要注意簽名演算法,只有簽章計算正確才能順利支付

public static String getH5PayStr(String result,HttpServletRequest request) throws Exception{ 
   
   Map<String, String> map = XMLUtil.doXMLParse(result); 
    
    
    SortedMap<Object,Object> params = new TreeMap<Object,Object>(); 
   params.put("appId", ConfigUtil.APPID); 
   params.put("timeStamp", Long.toString(new Date().getTime())); 
   params.put("nonceStr", PayCommonUtil.CreateNoncestr()); 
   params.put("package", "prepay_id="+map.get("prepay_id")); 
   params.put("signType", ConfigUtil.SIGN_TYPE); 
   String paySign = PayCommonUtil.createSign("UTF-8", params); 
   
   params.put("paySign", paySign);  //paySign的生成规则和Sign的生成规则一致 
   
   String json = JSONObject.fromObject(params).toString(); 
   
   return json; 
 }
登入後複製

 4 編寫最終的支付介面調起微信H5支付

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
 <base href="<%=basePath%>"> 
  
 <title>微信H5支付</title> 
  
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> 
  <script type="text/javascript"> 
  
 function jsApiCall(){ 
  WeixinJSBridge.invoke( 
   &#39;getBrandWCPayRequest&#39;,<%=(String)request.getAttribute("unifiedOrder")%>, function(res){ 
    WeixinJSBridge.log(res.err_msg); 
    //alert(res.err_code+res.err_desc+res.err_msg); 
    if(res.err_msg == "get_brand_wcpay_request:ok" ) { 
     alert("恭喜你,支付成功!"); 
    }else{ 
     alert(res.err_code+res.err_desc+res.err_msg);     
    } 
   } 
  ); 
 } 
 
 function callpay(){ 
  if (typeof WeixinJSBridge == "undefined"){ 
   if( document.addEventListener ){ 
    document.addEventListener(&#39;WeixinJSBridgeReady&#39;, jsApiCall, false); 
   }else if (document.attachEvent){ 
    document.attachEvent(&#39;WeixinJSBridgeReady&#39;, jsApiCall); 
    document.attachEvent(&#39;onWeixinJSBridgeReady&#39;, jsApiCall); 
   } 
  }else{ 
   jsApiCall(); 
  } 
 } 
 </script> 
 </head> 
 
 <body> 
  <input type="button" value="支付" onclick="callpay()"/> 
 </body> 
</html>
登入後複製

5 處理微信支付結果通知

package com.debug.weixin.servlet; 
 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintWriter; 
import java.util.Map; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.jdom.JDOMException; 
 
import com.debug.weixin.util.PayCommonUtil; 
import com.debug.weixin.util.XMLUtil; 
 
public class PayHandlerServlet extends HttpServlet { 
 
  
 public void doGet(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
   this.doPost(request, response); 
 } 
 
  
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
 
  InputStream inStream = request.getInputStream(); 
  ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); 
  byte[] buffer = new byte[1024]; 
  int len = 0; 
  while ((len = inStream.read(buffer)) != -1) { 
   outSteam.write(buffer, 0, len); 
  } 
   
  outSteam.close(); 
  inStream.close(); 
  String result = new String(outSteam.toByteArray(),"utf-8");//获取微信调用我们notify_url的返回信息 
  Map<Object, Object> map=null; 
  try { 
   map = XMLUtil.doXMLParse(result); 
  } catch (JDOMException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  for(Object keyValue : map.keySet()){ 
   System.out.println(keyValue+"="+map.get(keyValue)); 
  } 
  if (map.get("result_code").toString().equalsIgnoreCase("SUCCESS")) { 
    
   //对订单进行业务操作 
   System.out.println("-------------OK"); 
   response.getWriter().write(PayCommonUtil.setXML("SUCCESS", "")); //告诉微信服务器,我收到信息了,不要在调用回调action了 
    
  } 
 } 
}
登入後複製

對於上面的程式碼,有很多都是參考blog.csdn.net/u011160656/article/details/41759195,因此這部分的程式碼就不貼出來了,需要的話看這篇部落格就知道了。

二  微信掃碼支付(模式一)

#重點:必須呼叫長連結轉短連結介面、正確設定掃碼支付回呼URL

1 根據訂單號碼產生微信支付二維碼

下面是幾個產生二維碼的方法:

package com.debug.weixin.util; 
import com.google.zxing.common.BitMatrix; 
 
 import javax.imageio.ImageIO; 
 import java.io.File; 
 import java.io.OutputStream; 
 import java.io.IOException; 
 import java.awt.image.BufferedImage; 
 
 
 public final class MatrixToImageWriter { 
 
 private static final int BLACK = 0xFF000000; 
 private static final int WHITE = 0xFFFFFFFF; 
 
 private MatrixToImageWriter() {} 
 
  
 public static BufferedImage toBufferedImage(BitMatrix matrix) { 
  int width = matrix.getWidth(); 
  int height = matrix.getHeight(); 
  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
  for (int x = 0; x < width; x++) { 
  for (int y = 0; y < height; y++) { 
   image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); 
  } 
  } 
  return image; 
 } 
  
 public static void writeToFile(BitMatrix matrix, String format, File file) 
  throws IOException { 
  BufferedImage image = toBufferedImage(matrix); 
  if (!ImageIO.write(image, format, file)) { 
  throw new IOException("Could not write an image of format " + format + " to " + file); 
  } 
 } 
 
  
 public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) 
  throws IOException { 
  BufferedImage image = toBufferedImage(matrix); 
  if (!ImageIO.write(image, format, stream)) { 
  throw new IOException("Could not write an image of format " + format); 
  } 
 } 
 
 }
登入後複製

 這個算是工具類,還有一個就是把二維碼顯示在介面上的方法,CreateQRCode主要用到程式碼區塊:

 public static void createCodeStream(String text,HttpServletResponse response) throws Exception{ 
  
  // response.setContentType("image/jpeg"); 
  ServletOutputStream sos = response.getOutputStream(); 
 
  int width = 500; 
  int height = 500; 
  //二维码的图片格式 
  String format = "jpg"; 
  MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); 
  Map hints = new HashMap(); 
  //内容所使用编码 
  hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
  BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE, width, height, hints); 
  
  
  //生成二维码 
  
  MatrixToImageWriter.writeToStream(bitMatrix, format,sos); 
  
  sos.close(); 
  
  
 }
登入後複製

2 長連結轉短連結產生二維碼,編寫掃碼支付回呼方法並呼叫統一下單介面

 package com.debug.weixin.servlet; 
 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintWriter; 
import java.util.Date; 
import java.util.Map; 
import java.util.SortedMap; 
import java.util.TreeMap; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.jdom.JDOMException; 
 
import com.debug.weixin.util.CommonUtil; 
import com.debug.weixin.util.ConfigUtil; 
import com.debug.weixin.util.CreateQRCode; 
import com.debug.weixin.util.PayCommonUtil; 
import com.debug.weixin.util.XMLUtil; 
import com.mongodb.DBObject; 
 
public class ScanCodePayServlet extends HttpServlet { 
 
  
 public void doGet(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
  this.doPost(request, response); 
   
 } 
 
  
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException { 
   
  String flag=request.getParameter("flag"); 
  if("createCode".equals(flag)){ 
   createPayCode(request,response); 
  }else{ 
   try { 
    wxScanCodeHandler(request,response); 
   } catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
   } 
  } 
   
   
 } 
  
 public void createPayCode(HttpServletRequest request,HttpServletResponse response){ 
   
  String orderNo=request.getParameter("orderNo"); 
   
  SortedMap<Object,Object> paras = new TreeMap<Object,Object>(); 
  paras.put("appid", ConfigUtil.APPID); 
  paras.put("mch_id", ConfigUtil.MCH_ID); 
  paras.put("time_stamp", Long.toString(new Date().getTime())); 
  paras.put("nonce_str", PayCommonUtil.CreateNoncestr()); 
  paras.put("product_id", orderNo);//商品号要唯一 
  String sign = PayCommonUtil.createSign("UTF-8", paras); 
  paras.put("sign", sign); 
   
  String url = "weixin://wxpay/bizpayurl?sign=SIGN&appid=APPID&mch_id=MCHID&product_id=PRODUCTID&time_stamp=TIMESTAMP&nonce_str=NOCESTR"; 
  String nativeUrl = url.replace("SIGN", sign).replace("APPID", ConfigUtil.APPID).replace("MCHID", ConfigUtil.MCH_ID).replace("PRODUCTID", (String)paras.get("product_id")).replace("TIMESTAMP", (String)paras.get("time_stamp")).replace("NOCESTR", (String)paras.get("nonce_str")); 
   
 
 
   SortedMap<Object,Object> parameters = new TreeMap<Object,Object>(); 
   parameters.put("appid", ConfigUtil.APPID); 
   parameters.put("mch_id", ConfigUtil.MCH_ID); 
   parameters.put("nonce_str", PayCommonUtil.CreateNoncestr()); 
   parameters.put("long_url", CommonUtil.urlEncodeUTF8(nativeUrl)); 
   String sign2 = PayCommonUtil.createSign("UTF-8", parameters); 
   parameters.put("sign", sign2); 
   String requestXML = PayCommonUtil.getRequestXml(parameters); 
   String result =CommonUtil.httpsRequestForStr(ConfigUtil.SHORT_URL, "POST", requestXML); 
   
   Map<String, String> map=null; 
  try { 
   map = XMLUtil.doXMLParse(result); 
  } catch (JDOMException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (IOException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
   String returnCode = map.get("return_code"); 
   String resultCode = map.get("result_code"); 
   
   if(returnCode.equalsIgnoreCase("SUCCESS")&&resultCode.equalsIgnoreCase("SUCCESS")){ 
    
    String shortUrl = map.get("short_url"); 
    //TODO 拿到shortUrl,写代码生成二维码 
    System.out.println("shortUrl="+shortUrl); 
    try { 
    CreateQRCode.createCodeStream(shortUrl,response); 
    } catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
  } 
 } 
  
  
 public void wxScanCodeHandler(HttpServletRequest request,HttpServletResponse response) throws Exception { 
  InputStream inStream = request.getInputStream(); 
  ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); 
  byte[] buffer = new byte[1024]; 
  int len = 0; 
  while ((len = inStream.read(buffer)) != -1) { 
   outSteam.write(buffer, 0, len); 
  } 
   
  outSteam.close(); 
  inStream.close(); 
  String result = new String(outSteam.toByteArray(),"utf-8");//获取微信调用我们notify_url的返回信息 
  Map<Object, Object> map=null; 
  try { 
   map = XMLUtil.doXMLParse(result); 
  } catch (JDOMException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  for(Object keyValue : map.keySet()){ 
   System.out.println(keyValue+"="+map.get(keyValue)); 
  } 
  String orderNo=map.get("product_id").toString(); 
   
  //接收到请求参数后调用统一下单接口 
  SortedMap<Object, Object> parameters = new TreeMap<Object, Object>(); 
  parameters.put("appid", ConfigUtil.APPID); 
 
  parameters.put("mch_id", ConfigUtil.MCH_ID); 
  parameters.put("device_info", "1000"); 
  parameters.put("body", "测试扫码支付订单"); 
  parameters.put("nonce_str", PayCommonUtil.CreateNoncestr()); 
   
    
  parameters.put("out_trade_no", map.get("product_id")); 
  //parameters.put("total_fee", String.valueOf(totalPrice)); 
  parameters.put("total_fee", "1"); 
  parameters.put("spbill_create_ip", request.getRemoteAddr()); 
  parameters.put("notify_url", ConfigUtil.NOTIFY_URL); 
  parameters.put("trade_type", "NATIVE"); 
  parameters.put("openid", map.get("openid")); 
 
  String sign = PayCommonUtil.createSign("UTF-8", parameters); 
  
  parameters.put("sign", sign); 
 
  String requestXML = PayCommonUtil.getRequestXml(parameters); 
 
  String result2 = CommonUtil.httpsRequestForStr(ConfigUtil.UNIFIED_ORDER_URL,"POST", requestXML); 
   
  System.out.println("-----------------------------统一下单结果---------------------------"); 
  System.out.println(result2); 
  Map<String, String> mm=null; 
  try { 
   mm=getH5PayMap(result2,request); 
  } catch (Exception e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
  //String prepayId=getPrepayId(result2,request); 
  //String returnNoneStr=getReturnNoneStr(result2,request); 
  String prepayId=mm.get("prepay_id"); 
  String returnNoneStr=mm.get("nonce_str");; 
  SortedMap<Object, Object> lastSign = new TreeMap<Object, Object>(); 
  lastSign.put("return_code", "SUCCESS"); 
  lastSign.put("appid", ConfigUtil.APPID); 
  lastSign.put("mch_id", ConfigUtil.MCH_ID); 
  lastSign.put("nonce_str", returnNoneStr); 
  lastSign.put("prepay_id", prepayId); 
  lastSign.put("result_code", "SUCCESS"); 
  lastSign.put("key", ConfigUtil.API_KEY); 
   
   
  String lastSignpara = PayCommonUtil.createSign("UTF-8", lastSign); 
   
   
  StringBuffer buf=new StringBuffer(); 
  buf.append("<xml>"); 
  buf.append("<return_code>SUCCESS</return_code>"); 
  buf.append("<appid>"+ConfigUtil.APPID+"</appid>"); 
  buf.append("<mch_id>"+ConfigUtil.MCH_ID+"</mch_id>"); 
  buf.append("<nonce_str>"+returnNoneStr+"</nonce_str>"); 
  buf.append("<prepay_id>"+prepayId+"</prepay_id>"); 
  buf.append("<result_code>SUCCESS</result_code>"); 
  buf.append("<sign>"+lastSignpara+"</sign>"); 
  buf.append("</xml>"); 
   
  response.getWriter().print(buf.toString()); 
 } 
  
 public Map<String, String> getH5PayMap(String result,HttpServletRequest request) throws Exception{ 
   
   Map<String, String> map = XMLUtil.doXMLParse(result); 
   return map; 
 } 
 
}
登入後複製

最終看下公眾號支付與掃碼支付的微信配置:

【相關推薦】

#1. 特別推薦「php程式設計師工具箱」V0.1版本下載

#2. 微信小程式完整原始碼下載

3. 微信小程式demo:仿網易雲音樂

以上是Java實作微信公眾號與掃碼支付的案例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1653
14
CakePHP 教程
1413
52
Laravel 教程
1306
25
PHP教程
1251
29
C# 教程
1224
24
突破或從Java 8流返回? 突破或從Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP與Python:了解差異 PHP與Python:了解差異 Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP與Python:核心功能 PHP與Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

Java程序查找膠囊的體積 Java程序查找膠囊的體積 Feb 07, 2025 am 11:37 AM

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP:許多網站的基礎 PHP:許多網站的基礎 Apr 13, 2025 am 12:07 AM

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

See all articles