前两天公司让写个后台管理功能,拿出其中一个小模块做个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" /> </td>
<td><input type="file" multiple="multiple" name="file" id="fileName2" /> </td>
<td><input type="file" multiple="multiple" name="file" id="fileName3" /> </td>
</tr>
<tr>
<td>频道<input type="text" name="channel" id="channel" /> </td>
<td>时间<input type="text" name="time" id="time" /> </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>
?
原文:http://harborchung.iteye.com/blog/2231630