跳过正文

代码块

vue2 实现Dict 自动加载
·427 字·1 分钟· loading · loading
代码块 前端代码
url/sys-dict.js # 接口请求,根据字典的 type 请求
获取请求的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; }
中文字符串转拼音
·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; }
图片文件上传预览
·282 字·1 分钟· loading · loading
代码块 前端代码
//在input file内容改变的时候触发事件 $("#filed").change(function(){ //获取input file的files文件数组; //$('#filed')获取的是jQuery对象,.get(0)转为原生对象; //这边默认只能选一个,但是存放形式仍然是数组,所以取第一个元素使用[0]; var file = $('#filed').get(0).files[0]; //创建用来读取此文件的对象 var reader = new FileReader(); //使用该对象读取file文件 reader.readAsDataURL(file); //读取文件成功后执行的方法函数 reader.onload=function(e){ //读取成功后返回的一个参数e,整个的一个进度事件 console.log(e); //选择所要显示图片的img,要赋值给img的src就是e中target下result里面 //的base64编码格式的地址 $('#imgshow').get(0).src = e.target.result; } })
实现日志系统
·202 字·1 分钟· loading · loading
代码块 GoLang代码
//定义一个日志写入器接口(LogWriter),要求写入设备必须遵守这个接口协议才能被日志器(Logger)注册。 //日志器有一个写入器的注册方法(Logger 的 RegisterWriter()方法)。 package main //LogWriter 日志写入器接口 type LogWriter interface { // Write 写入方法 Write(data interface{}) error } // Logger 日志 type Logger struct { //注册的写入器 logWriters []LogWriter } // RegisterWriter 注册日志写入器 func (log *Logger) RegisterWriter(writer LogWriter) { log.logWriters = append(log.logWriters, writer) } //Log 日志写入 func (log *Logger) Log(data interface{}) { for _, writer := range log.logWriters { writer.Write(data) } } // NewLogger 创建日志器实例 func NewLogger() *Logger { return &Logger{} }
十进制整数转化为二进制
·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"); } } }