IT虾米网

利用SpringMVC上传文件

leader 2018年06月24日 编程语言 979 0

利用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代码

@Controller 
public 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"; 
       } 
    } 
 
}

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!