CommonsMultipartResolver 上传文件

controller:

 
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)) {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            //获取multiRequest 中所有的文件名
            Iterator iter = multiRequest.getFileNames();

            while (iter.hasNext()) {
                //一次遍历所有文件
                MultipartFile file = multiRequest.getFile(iter.next().toString());
                if (file != null) {
                    //得到文件名
                    String filename = file.getOriginalFilename();
                    //文件名唯一性
                    filename = UUID.randomUUID().toString() + "_" + filename;
                    //在服务器创建同名的文件夹
                    String Webpath = "/upload/imgs/";
                    //组成完整的服务器文件名
                    String filepath = request.getServletContext().getRealPath(Webpath + filename);
                    //创建文件
                    File uploadfie = new File(filepath);
                    uploadfie.getParentFile().mkdirs();
                    //添加图片路径,数据库保存文件路径
                    store.setStorePanoramaExtend(filepath);
                    //上传
                    try {
                        file.transferTo(new File(filepath));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }

        }

jsp:

<div class="controls">
    <input  name="store_img1"  type="file"  
        id="storeImg1" accept="image/*" />
</div>

springMVC 配置文件:

  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>


阅读更多
上一篇solr配置中文分析器
下一篇SimpleDateFormat 日期格式化类
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭