spring文件上传怎么实现(spring文件上传怎么实现的)

2022-12-13 15:33:00 发布:网友投稿 作者:网友投稿
热度:79


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

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"&

下一篇:泡打粉可以用碱面代替吗?(泡打粉可以用碱面代替吗)
上一篇:白糖糕(白糖糕的做法)