springboot 上传文件
< 返回列表时间: 2020-08-04来源:OSCHINA
package com.taven.demo; import org.springframework.web.bind.annotation. PostMapping ; 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 java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController @RequestMapping ( "/file" ) public class FileController { @PostMapping ( "/upload1" ) public Map<String, String> upload1( @RequestParam ( "file" ) MultipartFile file) throws IOException { file.transferTo( new File( "/Users/taven/demo/" + file.getOriginalFilename())); Map<String, String> result = new HashMap<>(); result.put( "contentType" , file.getContentType()); result.put( "fileName" , file.getOriginalFilename()); result.put( "fileSize" , file.getSize() + "" ); return result; } @PostMapping ( "/upload2" ) public List<Map<String, String>> upload2( @RequestParam ( "file" ) MultipartFile[] files) throws IOException { if (files == null || files. length == 0 ) { return null ; } List<Map<String, String>> results = new ArrayList<>(); for (MultipartFile file : files) { file.transferTo( new File( "/Users/taven/demo/" + file.getOriginalFilename())); Map<String, String> map = new HashMap<>(); map.put( "contentType" , file.getContentType()); map.put( "fileName" , file.getOriginalFilename()); map.put( "fileSize" , file.getSize() + "" ); results.add(map); } return results; } }
热门排行