Review学生作业管理系统

让后台立即响应前端,前端不等待处理过程

描述:学生作业管理系统里面有一个邮件提醒的功能,如果不加以处理前端就会一直等待返回结果不会弹出发送成功的提示,直到邮件已经发送出去。
1、使用try{}finally{}(会快一点)

try{
    
    
	return ApiResult.success("发送成功");
}finally{
    
    
	//发送邮件代码...
}

使用try{}finally{}语句,让try语句直接先返回结果,最后才执行发送邮件代码。

2、使用多线程(秒返回结果)

new thread(()->{
    
    
	//发送邮件代码...
}).start();
return ApiResult.success("发送成功");

原本由main线程执行的发送邮件代码交给其他线程处理,main线程直接返回结果。

让页面加载完成就立即发送Ajax请求

在这里插入图片描述

$(document).ready(function () {
    
     //此处页面打开即请求api
        $.ajax({
    
    
            type: 'GET',
            url: '/ssm/homework/getHomeWork',
            dataType: "json",
            async: false,
            success: function (data) {
    
    
             
                }
            }
        });
    });

以前一直要刷新几次才显示数据出来,原来是要加async:false把异步请求变为同步请求,以前一直以为加了$(document).ready()就可以加载出来。Ajax参数详解

SpringBoot热部署

在pom.xml加入spring-boot-devtools

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
</dependency>

如果改完代码需要按Ctrl + Shift + F9才可以完成热部署,以前只知道devtools可以用来热部署。SpringBoot 在IDEA中实现热部署

413 (Request Entity Too Large)

在上传文件是报出413
修改Nginx的配置文件

server {
    
      
    ...  
    client_max_body_size 20m;  
    ...  
}

还可以修改tomcat的配置文件(在application.properties修改)

server.tomcat.max-http-form-post-size=-1

设置post请求的最大容量为不限

优化SQL

创建索引

优化limit语句

select * from operation_log 1 limit 3000000 , 10;

select * from operation_log t , (select id from operation_log order by id limit
3000000,10) b where t.id = b.id ;

文件下载方式

1、利用springmvc提供的ResponseEntity类型,使用它可以很方便地定义返回的HttpHeaders和HttpStatus。(单个文件)

@RequestMapping("/download")  
	    public ResponseEntity<byte[]> export(String fileName,String filePath) throws IOException {
    
      
	    	
	        HttpHeaders headers = new HttpHeaders();    
	        File file = new File(filePath);
	        
	        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);    
	        headers.setContentDispositionFormData("attachment", fileName);    
	       
	        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
	                                              headers, HttpStatus.CREATED);    
	    } 

2、多文件下载打包成zip包

//批量文件压缩后下载
	@RequestMapping("/downLoad2")
	public ResponseEntity<byte[]> download2(HttpServletRequest request) throws IOException {
    
    
		
		//需要压缩的文件
		List<String> list = new ArrayList<String>();
		list.add("test.txt");
		list.add("test2.txt");
		
		//压缩后的文件
		String resourcesName = "test.zip";
		
		ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("D:/"+resourcesName));
		InputStream input = null;
		
		for (String str : list) {
    
    
			String name = "D:/"+str;
			input = new FileInputStream(new File(name));  
            zipOut.putNextEntry(new ZipEntry(str));  
            int temp = 0;  
            while((temp = input.read()) != -1){
    
      
                zipOut.write(temp);  
            }  
            input.close();
		}
		zipOut.close();
		File file = new File("D:/"+resourcesName);
		HttpHeaders headers = new HttpHeaders();
		String filename = new String(resourcesName.getBytes("utf-8"),"iso-8859-1");
		headers.setContentDispositionFormData("attachment", filename);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
	}

前端代码:
1、使用Ajax请求

<button type="button" onclick="download()">导出</button>
function download() {
    
    
  var url = 'download/?filename=aaa.txt';
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);    // 也可以使用POST方式,根据接口
  xhr.responseType = "blob";  // 返回类型blob
  // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
  xhr.onload = function () {
    
    
    // 请求完成
    if (this.status === 200) {
    
    
      // 返回200
      var blob = this.response;
      var reader = new FileReader();
      reader.readAsDataURL(blob);  // 转换为base64,可以直接放入a表情href
      reader.onload = function (e) {
    
    
        // 转换完成,创建一个a标签用于下载
        var a = document.createElement('a');
        a.download = 'data.xlsx';
        a.href = e.target.result;
        $("body").append(a);  // 修复firefox中无法触发click
        a.click();
        $(a).remove();
      }
    }
  };
  // 发送ajax请求
  xhr.send()
}

2、使用隐藏表单提交(推荐)

var form = $("<form>");
form.attr("style","display:none");
form.attr("target","");
form.attr("method","post");
form.attr("action",rootPath + "T_academic_essay/DownloadZipFile.do");
var input1 = $("<input>");
input1.attr("type","hidden");
input1.attr("name","strZipPath");
input1.attr("value",strZipPath);
$("body").append(form);
form.append(input1);
form.submit();
form.remove();

猜你喜欢

转载自blog.csdn.net/weixin_41699562/article/details/105002897