Springmvc下一次选多个文件上传(一)Form提交

 

Eclipse下搭建基本的基于注解的springmvc项目,除了spring的包以外多加2个包:

commons-fileupload和commons-io的jar包,随处可下载:

jsp页面:

可接受多个值的文件上传字段:

<input type="file" name="img" multiple="multiple" />
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.6.4.min.js"></script>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Index2</title>
	<script type="text/JavaScript" src="http://apps.bdimg.com/libs/jQuery/1.6.4/jquery.min.js"></script>
	<link href="http://libs.baidu.com/bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet">
	<script src="http://libs.baidu.com/bootstrap/2.3.2/js/bootstrap.min.js"></script>
</head>

<body> 
	<form action="<%=basePath%>upload2.htm" method="post" enctype="multipart/form-data">
   		<input class="filePrew"  tabIndex="3" type="file" size="3" name="graphTheories" multiple/>
   		<input type="submit" value="提交"/>
	</form>    
</body>    	
</html>


springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.2.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	<!-- 自动扫描包下面的文件,类上带有@Controller的会注册为bean -->
	<context:component-scan base-package="com.taray.controller"></context:component-scan>
	<!-- 代替处理器映射器、处理器适配器 ,mvc注解驱动-->
	<mvc:annotation-driven/>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置jsp路径的前缀,后缀,那样在controller中设置视图时候不需要再加了 -->
		<!-- prefix前缀如果 不添加,那么如果在@controller注解下有一个@requestmapping("name")访问时候会出错-->
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	 <!-- 支持上传文件,不清楚,这个示例没这玩意一样用 -->  
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>  
</beans>


Controller

package com.taray.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@Scope("prototype")
public class UpController {
	@RequestMapping("load")
	public String TestOne(HttpServletRequest request,HttpServletResponse response){
		//request.setAttribute("name", "你好,Siri");
		return "index2";
	}
	
    @RequestMapping("upload2")
    public String uploadpic(@RequestParam(value="graphTheories") MultipartFile[] graphTheories, HttpServletRequest request) throws Exception{
    	List<String>newFileNames=this.uploadFileList(graphTheories, request);
        for (String newFileName:newFileNames){
             System.out.println("newfile"+newFileName);
        }
        /*for(int i=0;i<graphTheories.length;i++){
    		 String filename=graphTheories[i].getOriginalFilename();
    		 System.out.println(filename);
    	 }*/
    	return null;
    }
    
    /**
     * 批量上传文件 返回值为文件的新名字 UUID.randomUUID()+originalFilename
     * @param multipartFiles
     * @param request
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    public List<String> uploadFileList(MultipartFile multipartFiles[], HttpServletRequest request) throws Exception {
        List<String>newFileNames=new ArrayList<>();
        try {
            for(MultipartFile multipartFile:multipartFiles){
                //文件的原始名称
                String originalFilename=multipartFile.getOriginalFilename();
                String newFileName=null;
                if (multipartFile!=null&&originalFilename!=null&&originalFilename.length()>0){
                    newFileName= UUID.randomUUID()+originalFilename;
                    //存储图片的物理路径
                    String pic_path = request.getSession().getServletContext().getRealPath("/upload");
                    //如果文件夹不存在则创建
                    File file=new File(pic_path);
                    if(!file.exists()){
                    	System.out.println("file not exists");
                    	file.mkdir();
                    }	
                    /**
                     * 获取新文件的File实例,通过spring的org.springframework.web.multipartInterface MultipartFile
                     * 下的transferTo方法,这个可以移动文件的文件系统,复制文件系统中的文件或内存内容保存到目标文件.
                     * 如果目标文件已经存在,它将被删除。
                     */
                    //新文件路径实例
                    File targetFile = new File(pic_path, newFileName);
                    //内存数据读入磁盘
                    multipartFile.transferTo(targetFile);
                    newFileNames.add(newFileName);
                }
            }
        }catch (IOException e){
            throw new Exception();
        }
        return newFileNames;
    }
}

简单了解了multiple选中多图片上传的用法,属于form表单提交,无法使用到实际项目中。

下一篇:一次选择多个文件上传:异步提交

http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值