代码块
js获取地址栏参数
·43 字·1 分钟·
loading
·
loading
代码块
前端代码
//variable为地址栏中字段名 function getQueryVariable(variable){ var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); if(pair[0] == variable){return pair[1];} } return(false); }
jsp实现分页功能
·342 字·1 分钟·
loading
·
loading
代码块
Java代码
JSP: # <tr> <td colspan="9">共${pageInfo.pages}页,当前第${pageInfo.pageNum}页 <div style="float: right"> <button ${pageInfo.pageNum == 1 ? "disabled = 'disabled'":""} onclick="pageGo(${pageInfo.pageNum - 1})"> 上一页 </button> <c:forEach begin="${pageInfo.pageNum < 3? 1 : pageInfo.pageNum - 2}" end="${pageInfo.pageNum < 3? 1 + 4 : pageInfo.pageNum - 2 + 4}" var="num"> <c:choose> <%-- 当按钮超过总页数,不可点击 --%> <c:when test="${num > pageInfo.pages}"> <button disabled="disabled">${num}</button> </c:when> <%-- 当按钮等于当前页数,不可点击,更改样式 --%> <c:when test="${num == pageInfo.pageNum}"> <button onclick="pageGo(${num})" disabled="disabled">${num}</button> </c:when> <%-- 其他情况正常点击 --%> <c:otherwise> <button onclick="pageGo(${num})">${num}</button> </c:otherwise> </c:choose> </c:forEach> <button ${pageInfo.pageNum == pageInfo.pages ? "disabled = 'disabled'":""} onclick="pageGo(${pageInfo.pageNum + 1})"> 下一页 </button> </div> </td> </tr> js: # function pageGo(pageGo){ location.href="findAll.do?pageGo="+pageGo; } java: # 如果是SpringMVC,需要在spring配置文件中添加插件
IO复制文件
·330 字·1 分钟·
loading
·
loading
代码块
Java代码
//使用IO流实现文件复制 public static void copy(String by,String to) throws IOException { File fileby = new File(by); File fileto = new File(to); BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(fileby)); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileto)); byte[] bytes = new byte[1024]; int i; while ((i = bufferedInputStream.read(bytes)) != -1){ bufferedOutputStream.write(bytes,0,i); } bufferedInputStream.close(); bufferedOutputStream.flush(); bufferedOutputStream.close(); } /** * 复制文件夹(使用缓冲字节流) * @param sourcePath 源文件夹路径 * @param targetPath 目标文件夹路径 */ public static void copyFolder(String sourcePath,String targetPath) throws Exception{ //源文件夹路径 File sourceFile = new File(sourcePath); //目标文件夹路径 File targetFile = new File(targetPath); if(!sourceFile.exists()){ throw new Exception("文件夹不存在"); } if(!sourceFile.isDirectory()){ throw new Exception("源文件夹不是目录"); } if(!targetFile.exists()){ targetFile.mkdirs(); } if(!targetFile.isDirectory()){ throw new Exception("目标文件夹不是目录"); } File[] files = sourceFile.listFiles(); if(files == null || files.length == 0){ return; } for(File file : files){ //文件要移动的路径 String movePath = targetFile+File.separator+file.getName(); if(file.isDirectory()){ //如果是目录则递归调用 copyFolder(file.getAbsolutePath(),movePath); }else { //如果是文件则复制文件 BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(movePath)); byte[] b = new byte[1024]; int temp = 0; while((temp = in.read(b)) != -1){ out.write(b,0,temp); } out.close(); in.close(); } } }
axios实现post文件下载
·181 字·1 分钟·
loading
·
loading
代码块
前端代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> </head> <body> <button id="b">donwload</button> </body> </html> // 处理Blob数据下载 function executeDownload(data, fileName) { if (!data) { return } let url = window.URL.createObjectURL(new Blob([data])); let link = document.createElement('a'); link.style.display = 'none'; link.href = url; link.setAttribute('download', fileName); document.body.appendChild(link); link.click(); document.body.removeChild(link); alert("download success") } let url = "http://example" let params = {} // 发送请求,接收文件数据并解析文件名 $("#b").click(function(){ axios.post( url, params, {responseType: 'blob'} ) .then(res => { let reader = new FileReader(); let data = res.data; reader.onload = e => { if (e.target.result.indexOf('Result') != -1 && JSON.parse(e.target.result).Result == false) { // 进行错误处理 } else { // 获取响应,前后端分离需要后端主动暴露响应头 // resp.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); let contentDisposition = res.headers['content-disposition']; if (contentDisposition) { fileName = window.decodeURI(res.headers['content-disposition'].split('=')[1], "UTF-8"); } executeDownload(data, fileName); } }; reader.readAsText(data); }) })
99乘法表
·41 字·1 分钟·
loading
·
loading
代码块
Java代码
class ForTest{ public static void main(String[] args){ int a,b = 1; while (b < 10){ for (a = 1;a <= b;a++){ System.out.print(a + "x" + b + "=" + (a * b) + " "); } System.out.print("\n"); b++; } } }
100以内的所有质数
·313 字·1 分钟·
loading
·
loading
代码块
Java代码
方法一 # public class ArraysTest { public static void main(String[] args) { int num = 0; // 遍历2~100的所有整数 for (int i = 2; i <= 100; i++) { // 内层循环,遍历2到i for (int j = 2; j <= i; j++) { // 如果可以被除了i本身以外的数整除,跳出内层循环 if (i % j == 0 && i != j) { break; // 如果不可以整除,重新开始内层循环 } else if (i % j != 0) { continue; // 输出质数,并且叠加计数num } else { System.out.print(i + ","); num++; } } } System.out.println("一共" + num + "个"); } } 方法二 # public class ArraysTest { public static void main(String[] args) { int sum = 0; for (int i = 2; i <= 100; i++) { boolean isFlag = true; for (int j = 2; j < i; j++) { if (i % j == 0) { isFlag = false; } } if (isFlag == true) { System.out.print(i + ","); sum++; } } System.out.println("一共" + sum + "个"); } }