バックグラウンドがフロントエンドにすぐに応答するようにします。フロントエンドは処理プロセスを待機しません
説明:学生の宿題管理システムには電子メールリマインダー機能があります。処理されない場合、フロントエンドは返される結果を待ち、電子メールが送信されるまで送信成功のプロンプトをポップアップしません。
1. try {} finally {}を使用します(より高速になります)
try{
return ApiResult.success("发送成功");
}finally{
//发送邮件代码...
}
try {} finally {}ステートメントを使用して、tryステートメントが最初に結果を直接返し、最後にメーリングコードを実行するようにします。
2.複数のスレッドを使用します(結果を数秒で返します)
new thread(()->{
//发送邮件代码...
}).start();
return ApiResult.success("发送成功");
メインスレッドによって最初に実行されたメール送信コードは、処理のために他のスレッドに渡され、メインスレッドは結果を直接返します。
ページが読み込まれるとすぐに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要求エンティティが大きすぎます)
ファイルをアップロードするときに
、Nginx構成ファイルを変更するために413が報告されました
server {
...
client_max_body_size 20m;
...
}
tomcatの構成ファイルを変更することもできます(application.propertiesで変更)
server.tomcat.max-http-form-post-size=-1
投稿リクエストの最大容量を無制限に設定します
SQLを最適化する
インデックスを作成する
制限ステートメントの最適化
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();