springmvc上传

springmvc上传

jar

<dependency>
	<groupId>commons-fileupload</groupId>
	<artifactId>commons-fileupload</artifactId>
	<version>1.3.1</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>


配置文件

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <!-- 最大内存大小 -->
        <property name="maxInMemorySize" value="10240"/>
        <!-- 最大文件大小,-1为不限制大小 -->
        <property name="maxUploadSize" value="-1"/>
    </bean>


单个上传

页面

    <form action="/ssm/save" method="post" enctype="multipart/form-data">     
        <label>图片</label><input type="file" name="file"/><br/>
        <input type="submit" value="提  交"/>
    </form>

控制器

@RequestMapping("save")
	public Object save(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {

		// 如果上传的图片不为空
		if (!file.isEmpty()) {
			// 原始名称
			String originalFilename = file.getOriginalFilename();
			System.out.println("图片原始名称:" + originalFilename);
			// 当前路径
			String currentPath = request.getSession().getServletContext().getRealPath("");
			System.out.println("当前路径" + currentPath);
			// 新的图片名称
			String newFileName = System.currentTimeMillis()
					+ originalFilename.substring(originalFilename.lastIndexOf("."));
			System.out.println("图片新名称:" + newFileName);
			// 创建图片存储路径
			File imgPath = new File(currentPath + "/image/");
			System.out.println("图片地址:" + imgPath);
			// 如果该路径不存在则创建一个新路径
			if (!imgPath.exists()) {
				imgPath.mkdirs();
			}
			// 图片
			File newFile = new File(imgPath + "/" + newFileName);

			// 将内存中的数据写入
			try {
				file.transferTo(newFile);
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {
			System.out.println("没有图片");
		}
		return "redirect:show";

	}

多个上传

页面

<form action="/ssm1/uploads" method="Post" enctype="multipart/form-data">
	<input type="file" name="file"><br>
	<input type="file" name="file"><br>
	<input type="file" name="file"><br>
	<input type="submit">
</form>

控制器

	@PostMapping(value = "/uploads")
	public String save(@RequestParam(value = "file", required = false) MultipartFile[] file,
			HttpServletRequest request) {
		//判断第一个图片是否为空
		if (!file[0].isEmpty()) {
			// 存储当前路径
			String currentPath = request.getSession().getServletContext().getRealPath("");
			System.out.println("当前路径:" + currentPath);
			// 创建图片存储目录
			File imgPath = new File(currentPath + "image/");
			System.out.println("图片目录:" + imgPath);
			if (!imgPath.exists()) {
				imgPath.mkdirs();
			}
			// 循环遍历file
			for (int i = 0; i < file.length; i++) {
				// 原始名称
				String originalFilename = file[i].getOriginalFilename();
				System.out.println("原始名称:" + originalFilename);
				// 重新命名 加i是为了区别名称
				String newFileName = System.currentTimeMillis() + i
						+ originalFilename.substring(originalFilename.lastIndexOf("."));
				System.out.println("图片新名称:" + newFileName);
				File newFile = new File(imgPath + "/" + newFileName);
				System.out.println("图片地址:" + newFile);
				try {
					file[i].transferTo(newFile);
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}else{
			System.out.println("没有图片");
		}

		return "show";
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值