4. In Rt $\triangle A B C$, $C D$ is the altitude on the hypotenuse $A B$, the three sides $a, b, c$ of $\triangle A B C$ are all positive integers, $B D$ $=27$. Then $\cos B=$ $\qquad$
4. $\frac{3}{5}$.

Let $(a, c)=d, a=a_{0} d, c=c_{0} d,\left(a_{0}, c_{0}\right)=1$.
By the projection theorem, we know $B C^{2}=B D \cdot B A$
$$
\Rightarrow a^{2}=27 c \Rightarrow d a_{0}=27 c_{0}
$$
$\Rightarrow a_{0}^{2} \mid 27 \Rightarrow a_{0}=1$ or 3:
If $a_{0}=1$, then
$$
\begin{array}{l}
b=\sqrt{c^{2}-a^{2}}=d \sqrt{c_{0}^{2}-a_{0}^{2}} \\
=d \sqrt{c_{0}^{2}-1}
\end{array}
$$

is not a positive integer, which is a contradiction.
If $a_{0}=3$, then $d=3 c_{0}$, and
$$
b=\sqrt{c^{2}-a^{2}}=d \sqrt{c_{0}^{2}-9} \text {. }
$$

Let $c_{0}^{2}-9=e^{2}$. Then
$$
\begin{array}{l}
\left(c_{0}-e\right)\left(c_{0}+e\right)=9 \\
\Rightarrow\left(c_{0}, e\right)=(5,4) .
\end{array}
$$

Therefore, $\cos B=\frac{a}{c}=\frac{a_{0}}{c_{0}}=\frac{3}{5}$.