網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Axios和Jquery實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
Jquery上傳
jquery文件時(shí),后端好像并沒有經(jīng)過SpringMVC處理,沒有被封裝成一個(gè)MultiPartFIle對(duì)象,可通過原生的Servlet API request.getInputStream()獲取。至于為什么沒被SpringMVC封裝成MultipartFile對(duì)象目前還沒有研究透徹??赡苁钦?qǐng)求內(nèi)容類型沒有設(shè)置成 multipart/form-data。下面是jquery上傳文件的代碼示例,文件名,文件大小等參數(shù)可通過拼在url后面使用request.getParameter()獲取。
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>Title</title>
</head>
<script src="jquery.js"></script>
<script src="axios.js"></script>
<body>
請(qǐng)上傳圖片:<input type="file" name="file" id="file" onchange="fileChange(this)">
<br>
<button onclick="jqueryUpload()">jquery提交</button>
</body>
<script>
? ? var file = undefined
? ? function fileChange(data){
? ? ? ? file = data.files[0]
? ? }
? function jqueryUpload(){
? ? $.ajax({
? ? ? ? url:"/jqueryUpload?filename=test.jpg",
? ? ? ? type:"post",
? ? ? ? data:file,
? ? ? ? contentType:false,
? ? ? ? processData:false,
? ? ? ? success(res){
? ? ? ? ? ? console.log('上傳結(jié)果' + res)
? ? ? ? }
? ? })
? }
</script>
</html>
@PostMapping("jqueryUpload")
? ? public String jqueryUpload(HttpServletRequest request, MultipartFile file) throws Exception{
? ? ? ? System.out.println(file);
? ? ? ? String fileDesc = "D:\\2022\\" + request.getParameter("filename");
? ? ? ? FileOutputStream outputStream = new FileOutputStream(fileDesc);
? ? ? ? ServletInputStream inputStream = request.getInputStream();
? ? ? ? byte[] bytes = new ?byte[1024];
? ? ? ? int line = inputStream.read(bytes);
? ? ? ? while (line != -1){
? ? ? ? ? ? outputStream.write(bytes,0,line);
? ? ? ? ? ? line = inputStream.read(bytes);
? ? ? ? }
? ? ? ? inputStream.close();
? ? ? ? outputStream.close();
? ? ? ? return "success";
? ? }
此時(shí)后臺(tái)打印的multipartfile是null。后續(xù)會(huì)深入研究·······…
Axios上傳
axios上傳不同于jquery,axios將上傳的二進(jìn)制數(shù)據(jù)和其他參數(shù)封裝在了FormData對(duì)象中,值得注意的是,此時(shí)的內(nèi)容類型要改為multipart/form-data。
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>Title</title>
</head>
<script src="jquery.js"></script>
<script src="axios.js"></script>
<body>
請(qǐng)上傳圖片:<input type="file" name="file" id="file" onchange="fileChange(this)">
<br>
<button onclick="jqueryUpload()">jquery提交</button>
<button onclick="axiosUpload()">axios提交</button>
</body>
<script>
? ? var file = undefined
? ? function fileChange(data){
? ? ? ? file = data.files[0]
? ? }
? function axiosUpload(){
? ? var formData = new FormData();
? ? formData.append("file",file);
? ? formData.append("filename","myfile.jpg")
? ? axios.post("/axiosUpload",formData,{
? ? ? ? headers:{ "Content-Type" : "multipart/form-data" }
? ? }).then(res => {
? ? ? ? console.log('上傳結(jié)果' + res)
? ? })
? }
</script>
</html>
@PostMapping("axiosUpload")
? ? public String axiosUpload(HttpServletRequest request,MultipartFile file)throws Exception{
? ? ? ? InputStream inputStream = file.getInputStream();
? ? ? ? String fileDesc = "D:\\2022\\" + request.getParameter("filename");
? ? ? ? FileOutputStream os = new FileOutputStream(fileDesc);
? ? ? ? byte[] bytes = new ?byte[1024];
? ? ? ? int line = inputStream.read(bytes);
? ? ? ? while (line != -1){
? ? ? ? ? ? os.write(bytes,0,line);
? ? ? ? ? ? line = inputStream.read(bytes);
? ? ? ? }
? ? ? ? inputStream.close();
? ? ? ? os.close();
? ? ? ? return "success";
? ? }
感覺還是更喜歡axios這種,明確指定了內(nèi)容類型并且經(jīng)過了SpringMVC處理。
原文鏈接:https://blog.csdn.net/qq_43750656/article/details/121426546
相關(guān)推薦
- 2023-07-24 vxe-grid實(shí)現(xiàn) 二維數(shù)據(jù)聯(lián)動(dòng)
- 2022-06-08 并發(fā)編程--CountdownLatch && CyclicBarrier
- 2023-02-07 基于C#實(shí)現(xiàn)磁性吸附窗體_C#教程
- 2022-05-12 Kotlin 集合也可以進(jìn)行+= -= 還可以根據(jù)條件進(jìn)行刪除(removeIf)
- 2022-10-27 Kotlin?Flow常用封裝類StateFlow使用詳解_Android
- 2022-11-20 解析rust中的struct_Rust語言
- 2022-05-10 remote: error: GE007: Your push would publish a pr
- 2022-10-22 C++中vector的常用接口詳析說明_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支