首页 > 编程语言 > 详细

浅谈一下Spring的文件上传功能,小Demo吧算

时间:2015-07-31 02:10:03      阅读:232      评论:0      收藏:0      [点我收藏+]

前两天公司让写个后台管理功能,拿出其中一个小模块做个Demo吧。

这个是配置Resolver的代码:(记得添加相应jar昂自己)

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <property name="maxUploadSize">//文件最大上传 
             <value>1048576</value>
         </property>
</bean>

?

?

@RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveAndUpload(
            @RequestParam(value = "channel", required = true)String channel,
            @RequestParam(value = "time", required = true)String time,
            @RequestParam(value="file") CommonsMultipartFile[] files) throws IOException {

        String picStr = "";
        //文件上传功能
        if (files.length!=0) {
            for(MultipartFile file:files){
                String fileName = SSUtil.generateNewPicName(file.getOriginalFilename());
                String picpath = DateUtil.getNowDay() + File.separator + fileName;
                String realPath = ConstUtils.PIC_ROOT + File.separator + picpath;
                File f = new File(realPath);
                FileUtils.copyInputStreamToFile(file.getInputStream(), f);
                picStr += picpath + ",";
              
            }
        }
        try (Jedis jedis = redisFactory.getJedis()) {

            //设置图片
            jedis.set(CACHE_PREFIX + channel + ":pics" , picStr);

            //播放时间
            jedis.set(CACHE_PREFIX + channel + ":time",time);

        }
        return "redirect:/live/editor";
    }

?其中包含了jedis的代码 大家只看上传部分的就好啦!

?

以下是前段上传代码:

?

<form action="save" method="post" enctype="multipart/form-data" onsubmit="return checkFile(this);">
          <div id="">
            <table>
              <tr>添加一个新的直播信息:
                <td><input type="file" multiple="multiple" name="file" id="fileName1" />&nbsp;</td>
                <td><input type="file" multiple="multiple" name="file" id="fileName2" />&nbsp;</td>
                <td><input type="file" multiple="multiple" name="file" id="fileName3" />&nbsp;</td>
              </tr>
              <tr>
                <td>频道<input type="text" name="channel" id="channel" />&nbsp;</td>
                <td>时间<input type="text" name="time" id="time" />&nbsp;</td>
                <td><input type="submit" value="确认"/></td>
            </tr>
            </table>
          </div>
        </form>

?

?以下是前端显示代码:

?

<div id="main_right_middle03">
          <c:set var="condition" value=‘http‘ />
          <c:forEach items="${results}" var="result" varStatus="status">
            <c:set var="toTest" value=‘${result}‘ />
            <c:if test="${fn:contains(toTest,condition)}">
              <img src="${result}" alt="上传的图片"/>
              <%--图片分组,3个为一组--%>
              <c:if test="${(status.index+1)%4==0}">
                <br/>
              </c:if>
            </c:if>

            <c:if test="${!fn:contains(toTest,condition)}">
              ${result}<br/>
            </c:if>
          </c:forEach>
</div>

?

浅谈一下Spring的文件上传功能,小Demo吧算

原文:http://harborchung.iteye.com/blog/2231630

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