网站首页 / 育儿 / 正文

spring文件上传怎么实现(spring文件上传大小限制)

时间:2022-04-13 21:55:27 浏览:6969次 作者:用户投稿 【我要投诉/侵权/举报 删除信息】

多条告白如次剧本只需引入一次

Spring Boot或Spring Cloud赶快实行文献上传

很多功夫咱们都须要在Spring Boot或Spring Cloud中赶快集成文献上传功效,然而对于生人来说减少文献上传功效须要查看很多文书档案。这边给出了示例不妨扶助您赶快将文献上传功效集成到体例中来。

第一步,咱们须要在application.yml中摆设文献上传的巨细

spring: servlet: multipart: max-file-size: 1500MB max-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 归来JSON */ @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"&

版权声明:
本文内容由互联网用户自发贡献,该文观点仅代表作者本人,因此内容不代表本站观点、本站不对文章中的任何观点负责,内容版权归原作者所有、内容只用于提供信息阅读,无任何商业用途。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站(文章、内容、图片、音频、视频)有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至353049283@qq.com举报,一经查实,本站将立刻删除、维护您的正当权益。