在Spring Boot
中,上传文件;对文件格式、大小进行验证。
获取文件对象
获取文件对象有两种方式,1.注解获取,2.request中获取
需要注意:上传的文件name
名称为file
注解获取文件对象
使用注解方式获取文件对象(单个文件上传)
1 2 3 4 5 6 7 8 9 10 11 12
| @RestController public class FileController {
private static Logger logger = LoggerFactory.getLogger(FileController.class);
@RequestMapping(value = "/upload", method = RequestMethod.POST) public String singleFileUpload(@RequestParam("file") MultipartFile files) { return ""; }
}
|
使用注解方式获取文件对象列表(批量文件上传)
1 2 3 4 5 6 7 8 9 10 11 12
| @RestController public class FileController {
private static Logger logger = LoggerFactory.getLogger(FileController.class);
@RequestMapping(value = "/upload", method = RequestMethod.POST) public String singleFileUpload(@RequestParam("file") MultipartFile[] files) { return ""; }
}
|
Request中获取文件对象
通过Request
对象获取上传的文件列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @RestController public class FileController {
private static Logger logger = LoggerFactory.getLogger(FileController.class);
@RequestMapping(value = "/upload", method = RequestMethod.POST) public String singleFileUpload(HttpServletRequest request) {
if(request instanceof MultipartHttpServletRequest) { List<MultipartFile> files =((MultipartHttpServletRequest)request).getFiles("file");
} return ""; }
}
|
文件写入磁盘
文件写入工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
public class FileUitl {
private static Logger logger = LoggerFactory.getLogger(FileUitl.class);
private static Set<String> EXCLUSION_EXT_NAME = new HashSet<>();
private static volatile String ROOT_PATH = "c:\\file\\";
private static volatile int FILE_MAX_SIZE = 10*1024*1024;
private static volatile List<String> IMAGE_TYPE = null;
static { EXCLUSION_EXT_NAME.add("jar"); EXCLUSION_EXT_NAME.add("jsp"); EXCLUSION_EXT_NAME.add("jspx"); EXCLUSION_EXT_NAME.add("asp"); EXCLUSION_EXT_NAME.add("aspx"); EXCLUSION_EXT_NAME.add("sh"); EXCLUSION_EXT_NAME.add("pl"); EXCLUSION_EXT_NAME.add("php"); EXCLUSION_EXT_NAME.add("bat"); EXCLUSION_EXT_NAME.add("war");
String imageType = "bmp|jpeg|jpg|png|tiff|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|wmf"; IMAGE_TYPE = Splitter.on('|').omitEmptyStrings().splitToList(imageType); }
public static boolean isImage(String extName) { if(null!=IMAGE_TYPE && !IMAGE_TYPE.isEmpty()) { return IMAGE_TYPE.contains(extName); } return false; }
public static String getRootPath() { return ROOT_PATH; }
public static FileEntity upload(MultipartFile file) throws InvokeException{ try { if (file.isEmpty()) { throw new InvokeException(-2002, "文件不能为空!"); }
String fileName = file.getOriginalFilename(); String extName = FilenameUtils.getExtension(fileName);
if(EXCLUSION_EXT_NAME.contains(extName.toLowerCase())) { throw new InvokeException(-2006, "禁止上传["+extName+"]文件类型!"); }
InputStream is = file.getInputStream();
long fileSize = is.available(); if(fileSize>FILE_MAX_SIZE){ throw new InvokeException(-2005,"文件大小超出限制!"); }
String filePath = "/"+new SimpleDateFormat("yyyy/MM/dd").format(new Date())+"/"+StringHelper.getUUID()+"."+extName;
String path = ROOT_PATH+filePath;
path = FileHelper.normalize(path);
File destination = new File(path);
if(!destination.getParentFile().exists()) { destination.getParentFile().mkdirs(); } destination.createNewFile();
FileUtils.copyToFile(file.getInputStream(), destination);
FileEntity fileEntity = new FileEntity(); fileEntity.setFilePath(filePath); fileEntity.setFileSize(fileSize); fileEntity.setFileName(file.getOriginalFilename()); return fileEntity; }catch (IOException e) { logger.error("",e); throw new InvokeException(-2003,"上传失败!"); } catch (InvokeException e) { throw e; }catch (Exception e) { logger.error("",e); throw new InvokeException(-2004,"上传失败!"); } } }
|
文件上传后返回对象(FileEntity)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
public class FileEntity {
private String filePath;
private Long fileSize;
private String fileName;
public String getFilePath() { return filePath; }
public void setFilePath(String filePath) { this.filePath = filePath; }
public Long getFileSize() { return fileSize; }
public void setFileSize(Long fileSize) { this.fileSize = fileSize; }
public String getFileName() { return fileName; }
public void setFileName(String fileName) { this.fileName = fileName; } }
|