引入angular和ng-file-upload。
前端代码
1 Upload.upload({ 2 url: 'upload', 3 fields: {'username': 'zouroto'}, // additional data to send 4 file: file 5 }).progress(function (evt) { 6 var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); 7 console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name); 8 }).success(function (data, status, headers, config) { 9 console.log('file ' + config.file.name + 'uploaded. Response: ' + data);10 });
springMVC代码:
1 @Controller 2 public class UiController { 3 4 @ResponseStatus(HttpStatus.OK) 5 @RequestMapping(value = "/upload") 6 public void upload(@RequestParam("file") MultipartFile file, @RequestParam("username") String username ) throws IOException { 7 8 byte[] bytes; 9 10 if (!file.isEmpty()) {11 bytes = file.getBytes();12 //store file in storage13 }14 15 System.out.println(String.format("receive %s from %s", file.getOriginalFilename(), username));16 }17 18 }
application config
13 4
maven
12 commons-fileupload 3commons-fileupload 41.3.1 5