upload file Spring mvc

@RequestMapping(value="/upload", method=RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file){ if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name))); stream.write(bytes); stream.close(); System.out.println("You successfully uploaded " + name + "!"); return "You successfully uploaded " + name + "!"; } catch (Exception e) { System.out.println("You failed to upload " + name + " => " + e.getMessage()); return "You failed to upload " + name + " => " + e.getMessage(); } } else { System.out.println("You failed to upload " + name + " because the file was empty."); return "You failed to upload " + name + " because the file was empty."; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.