Spring mvc多图片上传然后在页面展示

@RequestMapping("/insert")
	@ResponseBody
	public Object insert(HttpServletRequest request,
			HttpServletResponse response, @RequestParam MultipartFile[] urlName)
			throws IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String name=request.getParameter("name");
		
		name=new String(name.getBytes("ISO-8859-1"),"utf-8");
	System.out.println(name);
		String title = request.getParameter("title");
		title=new String(title.getBytes("ISO-8859-1"),"utf-8");
		System.out.println(title);
		String path = "";//定义路径
		
		String fileName = urlName[0].getOriginalFilename();//获取上传第一个图片的名字
		System.out.println(urlName[0].getOriginalFilename());
		String ext = fileName.substring(fileName.lastIndexOf("."));//根据点截取
		String bgimg = UUID.randomUUID().toString().replaceAll("-", "") + ext;

		fileName = urlName[1].getOriginalFilename();
		System.out.println(urlName[1].getOriginalFilename());
		ext = fileName.substring(fileName.lastIndexOf("."));
		String selectimg = UUID.randomUUID().toString().replaceAll("-", "")
				+ ext;

		for (int i = 0; i < urlName.length; i++) {
			MultipartFile myfile = urlName[i];
			if (myfile.isEmpty()) {
				System.out.println("文件未上传");
			} else {
				path = request.getSession().getServletContext()
						.getRealPath("image");

				File file = new File(path);
				if (!file.exists()) {
					file.mkdirs();
				}
				// 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
				// FileUtils.copyInputStreamToFile(myfile.getInputStream(),
				// new File(path, myfile.getOriginalFilename()));
				if (i == 0) {
					FileUtils.writeByteArrayToFile(new File(path, bgimg),
							myfile.getBytes());
				} else if (i == 1) {
					FileUtils.writeByteArrayToFile(new File(path, selectimg),
							myfile.getBytes());
				}
			}

		}
		// @RequestParam MultipartFile[] urlName,
		TplayerVersion tplayVersion = tplayerVersionService.selectName(name);
		TplayerTheme tplayerTheme = new TplayerTheme();
		tplayerTheme.setApp_version_id(tplayVersion.getId());
		tplayerTheme.setTitle(title);
		tplayerTheme.setBg_img("<img" + " " + "src='image/" + bgimg
				+ "'" + ">");//把图片的路径插入到数据库,因为ext显示图片必须加上img标签才能显示图片
		tplayerTheme.setSelected_img("<img" + " " + "src='image/"
				+ selectimg +"'"+ ">");
		tplayerTheme.setIs_on(true);
		tplayerTheme.setSort(0);
		Date date = new Date();
		Timestamp nousedate = new Timestamp(date.getTime());
		tplayerTheme.setCreatetime(nousedate);
		tplayerTheme.setModifytime(nousedate);
		tplayerThemeService.insert(tplayerTheme);
		System.out.println("22222222222");
		this.success = true;
		String msg = "{success:true}";
		return msg;
	}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值