跳过正文
  1. 文章/
  2. 代码块/

Java代码

2024

获取请求的ip地址
·403 字·1 分钟· loading · loading
代码块 Java代码
如果使用了nginx代理后台,则需要在nginx代理中将用户ip添加在请求头中
ResultVo
·199 字·1 分钟· loading · loading
代码块 Java代码
@Data public class ResultVO<T> implements Serializable { private Integer code; private String msg; private T data; public ResultVO(int code,String msg,T data){ this.code = code; this.msg = msg; this.data = data; } /** * 成功200、有数据 * @param msg * @param data * @return */ public static<T> ResultVO<T> success(String msg,T data){ return new ResultVO<>(ResultCode.SUCCESS,msg,data); } /** * 成功200、无数据 * @param msg * @return */ public static<T> ResultVO<T> success(String msg){ return success(msg,null); } /** * 失败500 * @param msg * @param <T> * @return */ public static<T> ResultVO<T> failed(String msg){ return new ResultVO<>(ResultCode.FAILED,msg,null); } /** * 自定义返回 * @param msg * @param code * @param data * @param <T> * @return */ public static<T> ResultVO<T> result(String msg,Integer code,T data){ return new ResultVO<T>(code,msg,data); } } public interface ResultCode { /** * 请求成功 */ int SUCCESS = 200; /** * 服务器异常 */ int FAILED = 500; /** * 未授权、未认证访问 */ int UNAUTHORIZED = 401; /** * 无权限 */ int NOACCESS = 403; }

2023

中文字符串转拼音
·236 字·1 分钟· loading · loading
代码块 Java代码
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency> @Slf4j public class PinYinUtil { /** * 将字符串中的中文转化为拼音,其他字符不变 * * @param inputString * @return */ public static String getPinYin(String inputString) { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.LOWERCASE); format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); format.setVCharType(HanyuPinyinVCharType.WITH_V); char[] input = inputString.trim().toCharArray(); String output = ""; try { for (int i = 0; i < input.length; i++) { if (java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) { String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format); output += temp[0]; } else output += java.lang.Character.toString(input[i]); } } catch (BadHanyuPinyinOutputFormatCombination e) { log.error("中文转拼音出错",e); } return output; } /** * 获取所有汉字串拼音首字母,英文字符不变 * @param chinese 汉字串 * @return 汉语拼音首字母 */ public static String getFirstSpell(String chinese) { StringBuffer pybf = new StringBuffer(); char[] arr = chinese.toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < arr.length; i++) { if (arr[i] > 128) { try { String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); if (temp != null) { pybf.append(temp[0].charAt(0)); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { pybf.append(arr[i]); } } return pybf.toString().replaceAll("\\W", "").trim(); } }
杨辉三角
·274 字·1 分钟· loading · loading
代码块 Java代码
public class ArrayTest1 { public static void main(String[] args) { // 创建二维数组并初始化,并且内层数组长度不确定 int[][] yangHui = new int[10][]; // 赋值 // 遍历外层数组 for (int i = 0; i < yangHui.length; i++) { // 初始化内层数组 yangHui[i] = new int[i + 1]; // 遍历内层数组 for (int j = 0; j < yangHui[i].length; j++) { // 如果是首位或末位元素,赋值为1 if (j == 0 || j == (yangHui[i].length - 1)) { yangHui[i][j] = 1; // 其他中间的元素,按照公式赋值 } else { yangHui[i][j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j]; } } } // 输出 // 遍历外层数组 for (int i = 0; i < yangHui.length; i++) { // 输出最左侧的行号 System.out.print("[" + i + "]\t"); // 遍历内层数组 for (int j = 0; j < yangHui[i].length; j++) { // 输出元素 System.out.print(yangHui[i][j] + "\t"); } // 输出完一行后换行 System.out.println(); // 输出最后一行的列数 // 判断是否输出到最后一行 if (i == yangHui.length - 1) { for (int j = 0; j < yangHui[yangHui.length - 1].length; j++) { System.out.print("\t" + "[" + j + "]"); } } } } }
序列化及反序列化
·86 字·1 分钟· loading · loading
代码块 Java代码
//序列化 public static void serialization(List list,String file) throws IOException { File file1 = new File(file); ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file)); for (Object o : list){ outputStream.writeObject(o); } outputStream.flush(); outputStream.close(); } //反序列化 public static List deserialization(String file) throws IOException, ClassNotFoundException { File file1 = new File(file); List list = new ArrayList(); ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file)); Object o; while (true){ try { o = inputStream.readObject(); }catch (Exception e){ break; } list.add(o); } inputStream.close(); return list; }
十进制整数转化为二进制
·115 字·1 分钟· loading · loading
代码块 Java代码
public static String tran(int a){ //con:商;rem:余数;last:最后承载结果的变量 int con; String rem = "",last = ""; //循环将余数储存在变量rem中 flag:while (true){ rem += (a % 2 + ""); a /= 2; if (a == 0){ break flag; } } //将获得的余数倒置,存储在变量last中 for (int i = rem.length() - 1;i >= 0;i--){ last += rem.charAt(i); } return last; }
六个不重复随机数数组
·74 字·1 分钟· loading · loading
代码块 Java代码
public class ArrayTest { public static void main(String[] args) { int[] arr = new int[6]; int i = 0; label: while (i < 6) { int num = (int) (Math.random() * 30 + 1); arr[i] = num; for (int j = 0; j < i; j++) { if (arr[j] == arr[i]) { continue label; } } i++; } for (int j = 0; j < 6; j++) { System.out.print(arr[j] + "\t"); } } }
获取异常堆栈信息
·53 字·1 分钟· loading · loading
代码块 Java代码
private String getStackTraceInfo(Exception e){ StackTraceElement[] stackTraceElements = e.getStackTrace(); String result = e.toString() + "\n"; for (int index = stackTraceElements.length - 1; index >= 0; --index) { result += "at [" + stackTraceElements[index].getClassName() + ","; result += stackTraceElements[index].getFileName() + ","; result += stackTraceElements[index].getMethodName() + ","; result += stackTraceElements[index].getLineNumber() + "]\n"; } return result; }
动态生成验证码
·262 字·1 分钟· loading · loading
代码块 Java代码
public class ImageServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置当前页面的文档类型,默认text/html resp.setContentType("image/gif"); //申请了一块画布 BufferedImage bufferedImage = new BufferedImage(50,20,BufferedImage.TYPE_INT_RGB); //得到一个画笔,设置背景颜色为白色 Graphics graphics = bufferedImage.getGraphics(); graphics.setColor(Color.white); graphics.fillRect(0, 0, 50, 20); graphics.setColor(Color.red); //生成验证码,随机颜色以及数字字母 String number = ""; for (int i = 0; i < 4; i++) { graphics.setColor(getRandomColor()); String code = getRandomChar(); number+=code; graphics.drawString(code, 10 + (i * 10), 12); } //生成五条随机的干扰线 for (int i = 0; i < 5; i++) { graphics.setColor(getRandomColor()); graphics.drawLine(new Random().nextInt(50), new Random().nextInt(20), new Random().nextInt(50), new Random().nextInt(20)); } //响应给浏览器 ImageIO.write(bufferedImage, "jpg", resp.getOutputStream()); } //生成随机颜色 public Color getRandomColor(){ return new Color(new Random().nextInt(255), new Random().nextInt(255), new Random().nextInt(255)); } //所有的数字字母,存入list集合 public String getRandomChar(){ ArrayList list = new ArrayList(); for (int i = 0; i < 10; i++) { list.add(i); } for (int i = 65; i < 65+26; i++) { list.add((char)i); } for (int i = 97; i < 97+26; i++) { list.add((char)i); } return list.get(new Random().nextInt(list.size()))+""; } }
递归删除文件夹内所有文件
·68 字·1 分钟· loading · loading
代码块 Java代码
//编写Java程序:删除指定的文件或文件夹,如果文件夹中有文件或子文件夹也一并删除 public static void deleteAll(File file){ if (file.isDirectory()){ File[] files = file.listFiles(); if (files != null){ for (File fi : files){ deleteAll(fi); } } } file.delete(); }