利用SpringMVC上传文件

m4ed的头像 m4ed 15 2015-01-31 20:20 0

 基本信息

× 1   

浏览数: 2729

分享时间: 4 年 前

利用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";
       }
    }

}


  • perman的头像 perman 2015-10-19 23:32 代码数:0
    太简单了吧,不怎么滴。
  • ossaa的头像 ossaa 2019-01-27 15:59 代码数:0

    You’ve got some interesting points in this article. I would have never considered any of these if I didn’t come across this. Thanks!.

    Simple Web Tutorials

  • ossaa的头像 ossaa 2019-02-12 23:52 代码数:0

    Great content material and great layout. Your website deserves all of the positive feedback it’s been getting.

    Omaha SEO

  • ossaa的头像 ossaa 2019-02-14 06:50 代码数:0

    Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks, I’ll probably keep it short and sweet and link  to this instead if thats cool. Thanks.

    proroid canada

  • ossaa的头像 ossaa 2019-02-15 05:39 代码数:0

    Thanks for providing recent updates regarding the concern, I look forward to read more.

    self-disinfection

您的评论: