多条告白如次剧本只需引入一次
Spring Boot或Spring Cloud赶快实行文献上传
很多功夫咱们都须要在Spring Boot或Spring Cloud中赶快集成文献上传功效,然而对于生人来说减少文献上传功效须要查看很多文书档案 。这边给出了示例不妨扶助您赶快将文献上传功效集成到体例中来 。
之一步,咱们须要在application.yml中摆设文献上传的巨细
【spring文件上传大小限制spring文件上传怎么实现】spring:servlet:multipart:max-file-size: 1500MBmax-request-size: 1500MB第二步,为了能赶快处置文献名和URL,咱们要用到FilenameUtils,在pom.xml的dependencies中引入Apache Commons IO,提防能否仍旧有援用,制止本子辩论
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency>第三步,写一个Controller,处置文献上传的乞求
import org.apache.commons.io.FilenameUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.io.File;import java.util.LinkedHashMap;import java.util.Map;import java.util.UUID;/** * 文献上传遏制器 * * @author 杨若瑜 */@RestController@RequestMapping("/platform/")public class UploadFileController {// 对立于名目根路途的上传路途private static final String UPLOAD_FOLDER = "/upload/";// 归来给前者的效劳器根路途(散布式、CDN场景很有效)private static final String URL_SERVER = "http://127.0.0.1:8080/";// 承诺上传的文献扩充名private static final String[] ALLOW_EXTENSIONS = new String[]{// 图片"jpg", "jpeg", "png", "gif", "bmp",// 收缩包"zip", "rar", "gz", "7z", "cab",// 音视频,"wav", "avi", "mp4", "mp3", "m3u8", "ogg", "wma", "wmv", "rm", "rmvb", "aac", "mov", "asf", "flv",// 文书档案"doc", "docx", "xls", "xlsx", "ppt", "pptx", "pot", "txt", "csv", "md", "pdf"};/*** 确定文献名能否承诺上传* @param fileName 文献名* @return*/public boolean isAllow(String fileName) {String ext = FilenameUtils.getExtension(fileName).toLowerCase();for (String allowExtension : ALLOW_EXTENSIONS) {if (allowExtension.toLowerCase().equals(ext)) {return true;}}return false;}/*** 上传文献* @param request 乞求* @param file 文献,与前台提交表单的file相普遍* @return 归来 *** ON*/@RequestMapping("upload")public Object upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) {String webAppFolder = request.getServletContext().getRealPath("/");String fileName = file.getOriginalFilename();if (isAllow(fileName)) {String ext = FilenameUtils.getExtension(fileName).toLowerCase();String newFileName = UUID.randomUUID().toString().replace("-", "");// 机动创造上传目次String targetPath = FilenameUtils.normalize(webAppFolder + "/" + UPLOAD_FOLDER);String targetFile = FilenameUtils.normalize(targetPath + "/" + newFileName + "." + ext);new File(targetPath).mkdirs();try {String urlPath = URL_SERVER + "/" + UPLOAD_FOLDER + "/" + newFileName + "." + ext;file.transferTo(new File(targetFile));Map<String, Object> resJson = new LinkedHashMap<>();resJson.put("status", "success");resJson.put("data", FilenameUtils.normalize(urlPath,true).replace("http:/","http://").replace("https:/","https://"));return resJson;} catch (Exception e) {e.printStackTrace();Map<String, Object> resJson = new LinkedHashMap<>();resJson.put("status", "error");resJson.put("message", "文献上传波折:" + e.getMessage());return resJson;}} else {Map<String, Object> resJson = new LinkedHashMap<>();resJson.put("status", "error");resJson.put("message", "该典型文献不承诺上传");return resJson;}}}第四步、写一个尝试网页upload.html,启用并尝试一下能否好用 。
<!doctype html><html lang="zh"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"&
推荐阅读
- 电脑里的文件打不开是什么原因 电脑为什么打不开doc文件
- 怎么合并多个PDF文件,单页pdf怎么合并成一个文件?
- tmp格式用什么打开 tmp格式文件怎么打开
- 收藏里文件如何发朋友圈 微信收藏怎么转发朋友圈
- 如何给文件夹加密码,如何给文件夹加密码才能打开?
- indesign崩溃了恢复文件的 *** indesign闪退怎么解决
- icloud在哪里打开「icloud在哪里打开文件」
- spring上传到文件服务器 spring文件上传大小限制
- 误删视频恢复软件操作 恢复删除文件软件怎么恢复
- class文件查看安卓版 class文件怎么打开乱码