facilityPhotoUpload.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<script type="text/javascript" src="${ctx}/static/js/uploadPic.js"></script>
<script type="text/javascript" src="${ctx}/static/js/groupAlbum.js"></script>
<link href="${ctx}/static/css/album.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" >
function loadurl() {
var flashs = getSWF("yjyUploadSwf");
flashs.changeUrl("${uploadUrl}/sys/overview/uploadPicture/3");
};
function sltphoto(){
var flashs = getSWF("yjyUploadSwf");
flashs.changeUrl("${uploadUrl}/sys/overview/uploadPicture/3");
}
function setDesc(picNum){
alert("图片上传成功!");
window.location.href="${ctx}/sys/overview/pages/1/3";
}
</script>
<br />
<div class="wrap" >
<h4 class="prepend-top">环境设施图片上传</h4>
<div class="album_box">
<div style="text-align:center;">
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
id="yjyUploadSwf" width="725" height="528" align="middle">
<param name="loop" value="true" />
<param name="play" value="true" />
<param name="allowScriptAccess" value="always" />
<param name="flashvars" value="urls=/sys/overview/uploadPicture/3">
<param name="movie" value="${ctx}/static/swfupload/jzxx_upload_yijay.swf" />
<embed width="725" height="528" loop="true" play="true" align="middle"
src="${ctx}/static/swfupload/jzxx_upload_yijay.swf"
allowscriptaccess="always"
name="yjyUploadSwf" type="application/x-shockwave-flash"
flashvars="urls=/sys/overview/uploadPicture/3"
pluginspage="http://www.macromedia.com/go/getflashplayer">
</embed>
</object>
</div>
</div>
</div>
controller
/**
* 上传照片页
*
* @param groupId
* @param photoId
* @param model
* @return
*/
@RequestMapping(value = "uploadIndex")
public String uploadIndex(Model model) {
String imageUploadUrl = PathUtil.getResource("image_upload_url");
model.addAttribute("uploadUrl", imageUploadUrl);
return "sys/overview/facilityPhotoUpload";
}
/**
* 上传环境设施图片
*
* @param request
* @param response
*/
@RequestMapping(value = "/uploadPicture/{photoType}")
@ResponseBody
public String uploadPicture(@PathVariable Integer photoType, RedirectAttributes redirectAttributes,
HttpServletRequest request, HttpServletResponse response) {
Photo photo = new Photo();
/* =============文件上传================ */
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
String fileDir = "";
String url = "";
if(photoType != null && photoType == 3) {
fileDir = request.getSession().getServletContext().getRealPath("/") + "/upload/facility";
url = "/upload/facility";
} else {
fileDir = request.getSession().getServletContext().getRealPath("/") + "/upload/album";
url = "/upload/album";
}
Map files = multipartRequest.getFileMap();
Iterator<String> fileNames = multipartRequest.getFileNames();
boolean flag = false;
for (; fileNames.hasNext();) {
String filename = (String) fileNames.next();
CommonsMultipartFile file = (CommonsMultipartFile) files.get(filename);
byte[] bytes = file.getBytes();
if (bytes.length != 0) {
if (!flag) {
File dirPath = new File(fileDir);
if (!dirPath.exists()) {
flag = dirPath.mkdirs();
}
}
String randomFileName = createRandomFileName(file.getOriginalFilename());// 生成的随机文件名
try {
FileManager.saveFileInputStream(file.getInputStream(), randomFileName, fileDir);
photo.setName(file.getOriginalFilename());
photo.setTime(new Date());
photo.setUrl(url+"/"+randomFileName);
System.out.println(url+"/"+randomFileName);
photo.setPicSize(file.getSize());
photo.setPhotoType(photoType);
photoManager.savePhoto(photo); // 保存相片
} catch (IOException e) {
e.printStackTrace();
}
}
}
redirectAttributes.addFlashAttribute("message", "上传图片成功");
return "success";
}
/**
* 随机生成图片名称
*
* @param originalFileName
* @param userid
* @return
*/
private String createRandomFileName(String originalFileName) {
return System.currentTimeMillis() + "_" + originalFileName.substring(originalFileName.lastIndexOf("."));
}