spring mvc 選中多文件同時上傳(利用input元素的multiple屬性)


原文:http://m.blog.csdn.net/article/details?id=51351388

 

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="upload.do" method="post" enctype="multipart/form-data">
        <p>一個選擇上傳的文件</p>
        <input type="text" name="reck" value=""/><br/> 
        <!-- 可以選取一張或者多種圖片上傳 -->
        <input type="file" name="files" multiple/><br/> 
        <!-- 每個input元素只能選中一個文件 -->
<!--        <input type="file" name="files" /><br/> 
        <input type="file" name="files" /><br/> 
        <input type="file" name="files" /><br/> -->
        <input type="submit" value="Submit"/>
    </form>
</body>
</html> 

 

spring中配置:

 

 <!-- ========文件上傳======= -->
    <!--200*1024*1024即200M resolveLazily屬性啟用是為了推遲文件解析,以便捕獲文件大小異常 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="209715200" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="resolveLazily" value="true" />
    </bean>

 

java代碼:

package org.wxy.demo.action;

import java.io.File;
import java.io.IOException;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;
/**
 * 
 * <一句話功能簡述><p>
 * MultipartFile類常用的一些方法:
 * String getContentType()          //獲取文件MIME類型
 * InputStream getInputStream()     //后去文件流
 * String getName()                 //獲取表單中文件組件的名字
 * String getOriginalFilename()     //獲取上傳文件的原名
 * long getSize()                   //獲取文件的字節大小,單位byte
 * boolean isEmpty()                //是否為空
 * void transferTo(File dest)       //保存到一個目標文件中。
 *
 * @author wangxy
 * @param <E>
 *
 * @see  [相關類/方法]
 * @since  [產品/模塊版本]
 */
@Controller
public class FileUpDownCtrl<E> {

    /**
     * 上傳文件
     * @param file
     * @return
     */
    @RequestMapping(value="/upload.do")
    public String fileUpload(@RequestParam("files") MultipartFile[] files,HttpServletRequest request) { 
        String rect = (String) request.getAttribute("rect");
        System.out.println(rect);

        //判斷file數組不能為空並且長度大於0  
        if(files!=null && files.length>0){  
            //循環獲取file數組中得文件  
            for(int i = 0;i<files.length;i++){  
                MultipartFile file = files[i];  

                try {
                    //獲取存取路徑
                    String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/" + file.getOriginalFilename();  
                    // 轉存文件  
                    file.transferTo(new File(filePath)); 
                } catch (IOException e) {
                    e.printStackTrace();
                }  
            }
            // 重定向  
            return "redirect:/list.do"; 
        }  
        // 重定向  
        return "redirect:/fail.html";  
    } 

    @RequestMapping(value="/list.do")
    @ResponseBody
    public String list(HttpServletRequest request,HttpServletResponse response) {
        String filePath = request.getSession().getServletContext().getRealPath("/") + "upload";
        File[] files = new File(filePath).listFiles();

        StringBuilder fileNames = new StringBuilder();
        for(File f : files){
            System.out.println(f.getName());
            fileNames.append(f.getName()+"\n");
        }
        return fileNames.toString(); 
    }

    /**
     * 配置了 <property name="resolveLazily" value="true" />
     * 才有效
     * @author wangxy
     *
     * @param ex
     */
    @ExceptionHandler
    public void doExcepiton(Exception ex){
        if(ex instanceof MaxUploadSizeExceededException){
            System.out.println("文件太大");
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM