Spring MVC多图片上传,多值上传

servlet-context.xml

<?xml version =“1.0”encoding =“UTF-8”?>
<beans xmlns =“http://www.springframework.org/schema/beans”
       xmlns:xsi =“http://www.w3.org / 2001 / XMLSchema-instance“
       xmlns:context =”http://www.springframework.org/schema/context“
       xmlns:mvc =”http://www.springframework.org/schema/mvc“
       xmlns:p =” http://www.springframework.org/schema/p“xmlns:util
       =”http://www.springframework.org/schema/util“
       xsi:schemaLocation =”http://www.springframework.org/schema/ mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/util http:/ /www.springframework.org/schema/util/spring-util-3.0.xsd“>


    <context:component-scan base-package =”com“/>


    <! -
        <context:property-placeholder 
        location =”/ WEB 。-INF /弹簧/ *属性”
        系统的属性模式= “OVERRIDE”/>
    - >
    <MVC:拦截>
        <MVC:拦截>
            <MVC:映射路径= “/ longinghou / *”/>
            <bean类=“com.sino.interceptor.longingInterceptor”/>
        </ mvc:interceptor>
    </ mvc:interceptors>
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <bean class =“org.springframework .web.servlet.view.InternalResourceViewResolver“
          p:prefix =”/ WEB-INF / jsp /“p:suffix =”。jsp“/>
    
    <util:properties id =”controller“location =”/ WEB-INF / spring /controller.properties“/>
    <bean id =”multipartResolver“  
    class =”org.springframework.web.multipart.commons.CommonsMultipartResolver“>  
    <! - 设置上传文件的最大尺寸为1MB - >  
    <property name =” maxUploa dSize“>  
        <value> 1048576 </ value>  
    </ property>  
</ bean>  
</ beans>







HTML:


<% - 
    文件:longing
    创建日期:2014-7-28,14:53:08
    作者:管理员
- %>


<%@ page import =“java.util.Date”%>
<%@ taglib prefix =“ c“uri =”http://java.sun.com/jsp/jstl/core“%>
<%@ taglib uri =”http://java.sun.com/jsp/jstl/functions“prefix =”fn “%>
<%@ page language =”java“contentType =”text / html; charset = UTF-8“pageEncoding =”UTF-8“%>
<% - <%@ page contentType =”text / html“pageEncoding =“UTF-8”%> - %>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv =“Content-Type”content =“text / html; charset = UTF-8”>
        <title> JSP页面</ title>
        <script type =“text / javascript”src =“<%= request.getContextPath()%> / js / jq.min.js”> </ script>
        <script type =“text / javascript”>
            var chenggongyu =“$ {chenggongyu }“
            if(chenggongyu!=“”){
                alert(“生成成功”);
            }
            function addone(){
                var ss =“<div>标题:<input type =' text'name ='title'> <br>描述:<input type ='text'name ='miaoshu'> <br> URL :<input type ='text'name ='url'> <br> image:<input type ='file'name ='urlName'> <br> <br> <br> </ div>“;
// alert($(“#xuanze”)。html())
// $(“#xuanze”)。html + ss;
                
                // $(“#xuanze”)。html($(“#xuanze”)。html()+ ss);
                $(“#xuanze”)。append(ss);
// document.getElementById(“xuanze”)。innerHTML + = ss;
            }


            function submitss(){
                var titlearr = new Array();
                var miaoshuarr = new Array();
                var urlarr = new Array();
                $(“input [name ='title']”)。each(
                        function(){
                            titlearr.push($(this).val());
                        }


                );
                $(“input [name ='miaoshu']”)。each(
                        function(){
                            miaoshuarr.push($(this).val());
                        }


                );
                $(“input [name ='url']”)。each(
                        function(){
                            urlarr.push($(this).val());
                        }


                );
                $(“#titlearr”)。val(titlearr);
                $(“#miaoshuarr”)。val(miaoshuarr);
                $(“#urlarr”)。val(urlarr);
                返回真
            }
        </ script>
    </ head>
    <body>
        <form method =“post”id =“form1”action =“<%= path%> / longinghou / succesfile”enctype =“multipart / form-data”onsubmit =返回submitss()“>
            欢迎您:$ {username}
            <div id =”xuanze“>
                    第一行内容(头标题)<br>
                    标题:<input type =”text“name =”title“> <br>
                    描述:<input type =“text”name =“miaoshu”> <br>
                    URL:<input type =“text”name =“url”> <br>
                    image:<input type =“file”name =“urlName” > <br>中国
            </ DIV>
            <INPUT TYPE = “提交”值= “提交”>&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP&NBSP
            <INPUT TYPE = “按钮”值= “再来一行”的onclick = “addone()”>
            <输入type =“hidden”id =“titlearr”name =“titlearr”/>
            <input type =“hidden”id =“miaoshuarr”name =“miaoshuarr”/>
            <input type =“hidden”id =“urlarr” =“urlarr”/>
        </ form>


        <! - //动作里的html是后缀名,不是HTML文件,用于spring对请求进行拦截判断
                <form method =“post”action =“<%= path %> / longinghou / succesfile“enctype =”multipart / form-data“>
                       <input type =”text“name =”name“/>
                    <input type =”file“name =”urlName“/>
                    <input typ e =“file”name =“urlName”/>
                    <input type =“submit”/>
                </ form> - >
    </ body>
</ html>




控制器:


    @RequestMapping(value =“/ succesfile”)
    public String handleRequest(HttpServletRequest request,
            HttpServletResponse response,@RequestParam MultipartFile [] urlName,@RequestParam String [] titlearr,@RequestParam String [] miaoshuarr,@RequestParam String [] urlarr)throws Exception {


// for(int i = 0; i <urlName.length; i ++){
// String fileName = urlName [i] .getOriginalFilename(); //获取上传第一个图片的名字  
// System.out.println (urlName [i] .getOriginalFilename());
//}
// for(int i = 0; i <titlearr.length; i ++){
// String fileName = titlearr [i]; //获取上传第一个图片的名字  
// System.out.println(titlearr [一世]);
//}
// for(int i = 0; i <miaoshuarr.length; i ++){
// String fileName = miaoshuarr [i]; //获取上传第一个图片的名字  
// System.out.println(miaoshuarr [一世]);
//}
// for(int i = 0; i <urlarr.length; i ++){
// String fileName = urlarr [i]; //获取上传第一个图片的名字  
// System.out.println(urlarr [一世]);
//}
        String neirong =“”;
        尝试{
            String encoding =“GBK”;
            File file = new File(feile +“/temp.txt”);
            if(file.isFile()&& file.exists()){//
                判断文件是否存在InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding); //考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read) ;
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine())!= null){
                    neirong + = lineTxt;
                }
                read.close();
            } else {
                System.out.println(“找不到指定的文件”);
            }
        } catch(Exception e){
            System.out.println(“读取文件内容出错”);
            e.printStackTrace();
        }


        字符串lineirong = “”;
        for(int i = 0; i <urlName.length; i ++){
            String neirongtihuan = tihuanneirong;
            String title = titlearr [i];
            字符串miaoshu = miaoshuarr [i];
            String url = urlarr [i];
            String img = urlName [i] .getOriginalFilename();
            if(i == 0){
                neirong = neirong.replace(“{title1}”,title);
                neirong = neirong.replace(“{miaoshu1}”,miaoshu);
                neirong = neirong.replace(“{url1}”,url);
                neirong = neirong.replace(“{img1}”,img);
            } else {
                neirongtihuan = neirongtihuan.replace(“{title}”,title);
                neirongtihuan = neirongtihuan.replace(“{miaoshu}”,miaoshu);
                neirongtihuan = neirongtihuan.replace(“{url}”,url);
                neirongtihuan = neirongtihuan.replace(“{img}”,img);
                lineirong + = neirongtihuan;
            }
        }
        neirong = neirong.replace(“{duogeli}”,lineirong);


        SimpleDateFormat df = new SimpleDateFormat(“MMdd”); //设置日期格式
        String wenjianj = df.format(new Date());
        File outfile = new File(feile +“/”+ wenjianj);
        if(!outfile.exists()){
            outfile.mkdirs();
        } else {
            fu.delFolder(feile +“/”+ wenjianj);
            outfile.mkdirs();
        }
        文件htmlfile = new File(feile +“/”+ wenjianj +“/hq.html”);
        htmlfile.createNewFile();
        尝试{
            FileOutputStream writerStream = new FileOutputStream(feile +“/”+ wenjianj +“/hq.html”); 
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(writerStream,“UTF-8”)); //写入流
            output.write(neirong); // s1为写入的字符串
            output.flush();
            output.close();
        } catch(Exception e){
            log.info(“html文件写入失败”);
        }
        file imgfile = new File(feile +“/”+ wenjianj +“/ img”);
        imgfile.mkdirs();


        //上传图片
        for(int i = 0; i <urlName.length; i ++){
            MultipartFile myfile = urlName [i];
            if(myfile.isEmpty()){
                log.info(“文件未上传”);
            } else {
                String path = feile +“/”+ wenjianj +“/ img”;


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


        }
        request.setAttribute(“chenggongyu”,“1”);
        返回“longinghou”;
    }




完整项目名称

neirongfabu


http://download.csdn.net/detail/ufo2910628/9808994 
完整项目下载地址

发布了33 篇原创文章 · 获赞 18 · 访问量 123万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章