首页 > 编程语言 > 详细

springmvc实现文件上传

时间:2017-01-28 20:19:52      阅读:172      评论:0      收藏:0      [点我收藏+]

1.通过commons-fileupload来实现。导入相关jar包:commons-fileupload,commons-io

 

2.配置springmvc的配置解析器

 

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="10485760000"></property>
        <property name="maxInMemorySize" value="40960"></property>
    </bean>

 

3.jsp页面

 

<form action="upload.do" method="post" enctype="multipart/form-data">
file:<input type="file" name="file" /><input type="submit" value="submit"/>
</form>

 

4.Controller代码

 

public class FileuploadController {
    @RequestMapping("/upload")
    public String fileupload(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest req) throws IOException{
        //获取文件名 file.getOriginalFilename();
        String path = req.getRealPath("/fileupload");//获取上传文件的路径
        InputStream is = file.getInputStream();
        OutputStream os = new FileOutputStream(new File(path,file.getOriginalFilename()));
        int len = 0;
        byte[] buffer = new byte[400];
        while((len=is.read(buffer)) != -1){
            os.write(buffer, 0, len);
        }
        os.close();
        is.close();
        return "/index.jsp";
    }
}

 

 

 

 

 

 

springmvc实现文件上传

原文:http://www.cnblogs.com/realvie/p/6354430.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!