swfupload批量上传图片实例


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("."));
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值