如何详细解析使用Spring Boot进行文件上传的步骤?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1103个文字,预计阅读时间需要5分钟。
这篇文章主要介绍了Spring Boot实现文件上传步骤的解析。文中通过示例代码详细展示了非详细的步骤,对于想要学习或工作的朋友具有一定的参考价值。需要的伙伴可以参考学习。第一步:编写上传的前端页面。
这篇文章主要介绍了springboot实现文件上传步骤解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
第一步编写上传的前段页面
<div> <button type="button" class="btn btn-primary" ng-click="openAddModal()" data-toggle="modal" data-target="#documentOprModal" style="margin-left: 10px;float:left"> 单个文件上传 </button> </div> <!-- 点击单个文件上传弹出的模态框 --> <div class="modal fade" id="documentOprModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static" data-keyboard="false"> <div class="modal-dialog modal-lg" role="document" style="width: 600px;"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel"> 文档入库</h4> </div> <div class="modal-body"> <!--开发--> <form name="docForm" id="docForm"> <div class="form-group"> <label for="jhText">井号:</label> <input type="text" class="form-control" id="jhText" ng-model="document.jh"> </div> <!-- 作者 --> <div class="form-group"> <label for="authorText">作者:</label> <input type="text" class="form-control" id="authorText" ng-model="document.author"> </div> <!-- 单位 --> <div class="form-group"> <label for="unitText">单位:</label> <input type="text" class="form-control" id="unitText" ng-model="document.unit"> </div> <!-- 日期 --> <div class="form-group"> <label for="writeDate">写作日期:</label> <input type="date" class="form-control" id="writeDate" ng-model="document.writeDate"> </div> <!-- 简介 --> <div class="form-group"> <label for="introductionTextArea">简介:</label> <textarea class="form-control" id="introductionTextArea" ng-model="document.introduction" rows="5" cols="60"></textarea> </div> <!-- 可能的查询关键字 --> <div class="form-group"> <label for="keyPackageTextArea">可能的查询关键字:</label> <textarea class="form-control" id="keyPackageTextArea" ng-model="document.keyPackage" rows="5" cols="60"></textarea> </div> <!-- 文件 --> <div class="form-group"> <div id="inputContent"> <input id="importFile" type="file" name="file" class="file-loading"> </div> </div> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" ng-click="submitFileInfo()"> <i class="fa fa-check"></i>保存 </button> <button type="button" class="btn btn-default" data-dismiss="modal" style="margin: 0px 20px;"> <i class="fa fa-ban"></i>取消 </button> </div> </div> </div> </div>
第二步写对应的js页面
/** * @Name:historyStorageCtrl,井史数据入库 * @Date:2019-06-19 * @Author:huofenwei */ (function (angular) { 'use strict'; angular.module('Lujing').controller('historyStorageCtrl', ['$scope', '$file"; /** * 上传文件 * @param file * @return */ @PostMapping("/upload") public Map<String, Object> uploadFile(MultipartFile file){ Map<String, Object> map = new HashMap<String, Object>(); try { //文件的对象 FileEntity fEntity = new FileEntity(); String uuid = UUID.randomUUID().toString(); //文件的id fEntity.setId(uuid.replaceAll("-", ""));//String.valueOf(Snowflake.getNextKey())); //文件的名字 fEntity.setFileName(file.getOriginalFilename()); //上传文件的时间 fEntity.setUploadTime(new Date()); //上传者 fEntity.setUploadBy("admin"); //文件的后缀 String suffix = fEntity.getFileName().substring(fEntity.getFileName().indexOf(".")); //文件存放的完整路径 fEntity.setFinePathName(uploadPath + File.separator + fEntity.getId() + suffix); //文件的类型 fEntity.setDocType(new DocType()); //设置文件的类型 fEntity.getDocType().setId(getDocTypeId(fEntity.getFileName())); //创建文件的对象 File newFile = new File(fEntity.getFinePathName()); //上传文件 file.transferTo(newFile); map.put("result", "success"); map.put("fileId", fEntity.getId()); }catch(Exception e) { // e.printStackTrace(); map.put("result", "fail"); } return map; } /** * 提交表单 * @param df * @return */ @PostMapping("/submit") public Map<String, Object> submitFileInfo(@RequestBody DocumentFile df) { Map<String, Object> map = new HashMap<String, Object>(); try { if (df.getId() == null || df.getId().isEmpty() || df.getId() == "") { df.setId(String.valueOf(Snowflake.getNextKey())); } String suffix = df.getDocName().substring(df.getDocName().indexOf(".")); df.setFilePath(uploadPath + File.separator + df.getFilePath() + suffix); df.setUploadBy("admin");// 用户名称 df.setUploadTime(new java.util.Date()); df.setUploadTime(new Date()); df.setDownloadNumber(0L); df.setHeat(0L); df.setRelated(20); df.setDocType(new DocType()); df.getDocType().setId(getDocTypeId(suffix)); df.setClassification(new DocClassification()); df.getClassification().setId(df.getClassificationId()); dfservice.save(df); map.put("result", "success"); } catch (Exception e) { //e.printStackTrace(); map.put("result", "fail"); } return map; } private Integer getDocTypeId(String fileName) { if (fileName.contains(".doc")) { return 1; } else if (fileName.contains(".xls")) { return 2; } else if (fileName.contains(".pdf")) { return 3; } else if (fileName.contains(".ppt")) { return 4; }else { return 5; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计1103个文字,预计阅读时间需要5分钟。
这篇文章主要介绍了Spring Boot实现文件上传步骤的解析。文中通过示例代码详细展示了非详细的步骤,对于想要学习或工作的朋友具有一定的参考价值。需要的伙伴可以参考学习。第一步:编写上传的前端页面。
这篇文章主要介绍了springboot实现文件上传步骤解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
第一步编写上传的前段页面
<div> <button type="button" class="btn btn-primary" ng-click="openAddModal()" data-toggle="modal" data-target="#documentOprModal" style="margin-left: 10px;float:left"> 单个文件上传 </button> </div> <!-- 点击单个文件上传弹出的模态框 --> <div class="modal fade" id="documentOprModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="static" data-keyboard="false"> <div class="modal-dialog modal-lg" role="document" style="width: 600px;"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel"> 文档入库</h4> </div> <div class="modal-body"> <!--开发--> <form name="docForm" id="docForm"> <div class="form-group"> <label for="jhText">井号:</label> <input type="text" class="form-control" id="jhText" ng-model="document.jh"> </div> <!-- 作者 --> <div class="form-group"> <label for="authorText">作者:</label> <input type="text" class="form-control" id="authorText" ng-model="document.author"> </div> <!-- 单位 --> <div class="form-group"> <label for="unitText">单位:</label> <input type="text" class="form-control" id="unitText" ng-model="document.unit"> </div> <!-- 日期 --> <div class="form-group"> <label for="writeDate">写作日期:</label> <input type="date" class="form-control" id="writeDate" ng-model="document.writeDate"> </div> <!-- 简介 --> <div class="form-group"> <label for="introductionTextArea">简介:</label> <textarea class="form-control" id="introductionTextArea" ng-model="document.introduction" rows="5" cols="60"></textarea> </div> <!-- 可能的查询关键字 --> <div class="form-group"> <label for="keyPackageTextArea">可能的查询关键字:</label> <textarea class="form-control" id="keyPackageTextArea" ng-model="document.keyPackage" rows="5" cols="60"></textarea> </div> <!-- 文件 --> <div class="form-group"> <div id="inputContent"> <input id="importFile" type="file" name="file" class="file-loading"> </div> </div> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" ng-click="submitFileInfo()"> <i class="fa fa-check"></i>保存 </button> <button type="button" class="btn btn-default" data-dismiss="modal" style="margin: 0px 20px;"> <i class="fa fa-ban"></i>取消 </button> </div> </div> </div> </div>
第二步写对应的js页面
/** * @Name:historyStorageCtrl,井史数据入库 * @Date:2019-06-19 * @Author:huofenwei */ (function (angular) { 'use strict'; angular.module('Lujing').controller('historyStorageCtrl', ['$scope', '$file"; /** * 上传文件 * @param file * @return */ @PostMapping("/upload") public Map<String, Object> uploadFile(MultipartFile file){ Map<String, Object> map = new HashMap<String, Object>(); try { //文件的对象 FileEntity fEntity = new FileEntity(); String uuid = UUID.randomUUID().toString(); //文件的id fEntity.setId(uuid.replaceAll("-", ""));//String.valueOf(Snowflake.getNextKey())); //文件的名字 fEntity.setFileName(file.getOriginalFilename()); //上传文件的时间 fEntity.setUploadTime(new Date()); //上传者 fEntity.setUploadBy("admin"); //文件的后缀 String suffix = fEntity.getFileName().substring(fEntity.getFileName().indexOf(".")); //文件存放的完整路径 fEntity.setFinePathName(uploadPath + File.separator + fEntity.getId() + suffix); //文件的类型 fEntity.setDocType(new DocType()); //设置文件的类型 fEntity.getDocType().setId(getDocTypeId(fEntity.getFileName())); //创建文件的对象 File newFile = new File(fEntity.getFinePathName()); //上传文件 file.transferTo(newFile); map.put("result", "success"); map.put("fileId", fEntity.getId()); }catch(Exception e) { // e.printStackTrace(); map.put("result", "fail"); } return map; } /** * 提交表单 * @param df * @return */ @PostMapping("/submit") public Map<String, Object> submitFileInfo(@RequestBody DocumentFile df) { Map<String, Object> map = new HashMap<String, Object>(); try { if (df.getId() == null || df.getId().isEmpty() || df.getId() == "") { df.setId(String.valueOf(Snowflake.getNextKey())); } String suffix = df.getDocName().substring(df.getDocName().indexOf(".")); df.setFilePath(uploadPath + File.separator + df.getFilePath() + suffix); df.setUploadBy("admin");// 用户名称 df.setUploadTime(new java.util.Date()); df.setUploadTime(new Date()); df.setDownloadNumber(0L); df.setHeat(0L); df.setRelated(20); df.setDocType(new DocType()); df.getDocType().setId(getDocTypeId(suffix)); df.setClassification(new DocClassification()); df.getClassification().setId(df.getClassificationId()); dfservice.save(df); map.put("result", "success"); } catch (Exception e) { //e.printStackTrace(); map.put("result", "fail"); } return map; } private Integer getDocTypeId(String fileName) { if (fileName.contains(".doc")) { return 1; } else if (fileName.contains(".xls")) { return 2; } else if (fileName.contains(".pdf")) { return 3; } else if (fileName.contains(".ppt")) { return 4; }else { return 5; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

