Spring上传文件代码示例,spring上传文件示例,利用SpringMVC上
分享于 点击 49754 次 点评:231
Spring上传文件代码示例,spring上传文件示例,利用SpringMVC上
利用SpringMVC上传文件,也可以一次上传多个文件。
上传多个文件的时候用MultipartFile[] file即可
页面表单
<html> <head> <title>Upload a file please</title> </head> <body> <h1>Please upload a file</h1> <form method="post" action="/form" enctype="multipart/form-data"> <input type="text" name="name"/> <input type="file" name="file"/> <input type="submit"/> </form> </body></html>
控制层JAVA代码
@Controllerpublic class FileUploadController { @RequestMapping(value = "/form", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { byte[] bytes = file.getBytes(); // store the bytes somewhere return "redirect:uploadSuccess"; } else { return "redirect:uploadFailure"; } }}
用户点评