温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用SpringBoot怎么实现一个单文件上传功能

发布时间:2021-04-17 16:28:26 来源:亿速云 阅读:184 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关使用SpringBoot怎么实现一个单文件上传功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1. 页面testFile.html

<!DOCTYPE html> <html> <head>  <meta charset="UTF-8">  <title>测试文件上传</title>    <script src="../static/jquery/jquery-2.1.1.min.js" th:src="@{/jquery/jquery-2.1.1.min.js}"></script>      <script type="text/javascript">   $(function () {     $("#upload1").click(function () {       var formData = new FormData();       formData.append("file", document.getElementById("file").files[0]);         $.ajax({         url: "/file/upload1",         type: "POST",         data: formData,                  //必须false才会自动加上正确的Content-Type         contentType: false,                  //必须false才会避开jquery对 formdata 的默认处理         //XMLHttpRequest会对 formdata 进行正确的处理         processData: false,                  success: function (data) {           if (data.status == "true") {             alert("上传成功!");           }           if (data.status == "error") {             alert(data.msg);           }         },         error: function () {           alert("上传失败!");         }       });     });   });   </script> </head> <body>     <form method="POST" enctype="multipart/form-data" action="/file/upload1">    <fieldset>      <legend>单一文件上传实例:</legend>        文件1:<input type="file" name="file" id="file"/><br/>              <input type="button" id="upload1" value="上传"/><br/>     </fieldset>   </form> </body> </html>

2. FileController.java

package com.stormkai.controller; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import lombok.extern.slf4j.Slf4j; @Controller @RequestMapping("/file") @Slf4j public class FileController {    @GetMapping("/index")  public String index() {  return "testFile";  }    @PostMapping("/upload1")   @ResponseBody   public Map<String, Object> upload1(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {          log.info("系统路径={}",request.getSession().getServletContext().getRealPath(""));     String path = "F:\\uploadfile\\";     if(!new File(path).exists()){      new File(path).mkdirs();  }     file.transferTo(new File(path + file.getOriginalFilename()));     Map<String, Object> result = new HashMap<>();     result.put("status", "true");     result.put("data", null);     return result;   } }

上述就是小编为大家分享的使用SpringBoot怎么实现一个单文件上传功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI