html向后端发送请求
生活随笔
收集整理的這篇文章主要介紹了
html向后端发送请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
?
?
index.html:?? ??? ??? ?<form class="form-horizontal" action="upload" enctype="multipart/form-data" method="post">
前端具體實例如下:
<form class="form-horizontal" action="upload" enctype="multipart/form-data" method="post"><div class="form-group"><div class="input-group col-md-4"><span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span><input class="form-control" placeholder="文件描述" type="text" name="description" /></div></div><div class="form-group"><div class="input-group col-md-4"><span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span><input class="form-control" placeholder="請選擇文件" type="file" name="file"/></div></div><div class="form-group"><div class="col-md-4"><div class="btn-group btn-group-justified" ><div class="btn-group" ><button type="submit" class="btn btn-success" id="submitbtn"><span class="glyphicon glyphicon-share"></span> 文件上傳</button></div></div></div></div></form>
上述這種用法的請求類型是post
后臺能收到的變量是:file和description
?
后端接收辦法實例:
@PostMapping(value="/upload")public String upload(HttpServletRequest request,@RequestParam("description") String description,@RequestParam("file") MultipartFile file) throws Exception{// 接收參數descriptionSystem.out.println("description = " + description);// 如果文件不為空,寫入上傳路徑if(!file.isEmpty()){// 上傳文件路徑String path = request.getServletContext().getRealPath("/upload/");System.out.println("path = " + path);// 上傳文件名String filename = file.getOriginalFilename();//上傳的文件名是啥這里就是啥。File filepath = new File(path,filename);System.out.println("這里是upload路徑:"+filepath);// 判斷路徑是否存在,如果不存在就創建一個if (!filepath.getParentFile().exists()){filepath.getParentFile().mkdirs();}// 將上傳文件保存到一個目標文件當中file.transferTo(new File(path+File.separator+ filename));return "success";}else{return "error";//如果文件為空,返回錯誤頁面}}上述代碼來自書上
總結
以上是生活随笔為你收集整理的html向后端发送请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为啥通过MSIE判断浏览器信息
- 下一篇: intellij设置java中的代码中的