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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章