天天看點

SpringMVC ---- 檔案上傳

<h1>上傳單個檔案</h1>
<form method="post" action="/file/upload" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <input type="submit"/>
</form>           
<h1>上傳多個檔案</h1>
<form method="post" action="/file/upload2" enctype="multipart/form-data">
    <input type="file" name="file1"/>
    <br/>
    <input type="file" name="file2"/>
    <input type="submit"/>
</form>           

maven項目導入這個就夠了:

<!--檔案上傳-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>           
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 預設編碼 -->
        <property name="defaultEncoding" value="utf-8" />
        <!-- 設定上傳檔案的最大尺寸為5MB -->
        <property name="maxUploadSize" value="5242880"/>
    </bean>           
/**
     * 對關于上傳大小,類型的判斷攔截 https://www.cnblogs.com/com-itheima-crazyStone/p/6807342.html
     * 上傳單個檔案,使用MultipartFile作為方法參數接收傳入的檔案
     * @param request
     * @param file
     * @param model
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String doUploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile file, Model model) throws IOException {
        //擷取一個儲存檔案的路徑
        String path = request.getSession().getServletContext().getRealPath("/upload");
        //路徑可能不存在,要對其判斷,如果不存在則建立
        File sourceFloader = new File(path);
        if (!sourceFloader.exists()) {
            //建立檔案夾
            sourceFloader.mkdir();
        }
        if (!file.isEmpty()) {
            // 得到輸入流
            InputStream inputStream = file.getInputStream();
            // 擷取檔案名
            String filename = file.getOriginalFilename();
            FileUtils.copyInputStreamToFile(inputStream, new File(path, filename));
            model.addAttribute("path", "upload\\" + filename);
        }
        return "success";
    }

    /**
     * 多檔案上傳
     * MultipartHttpServletRequest來解析Request中的檔案
     * @param multiRequest
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/upload2", method=RequestMethod.POST)
    public String doUploadFile2(HttpServletRequest request ,MultipartHttpServletRequest multiRequest ,Model model) throws IOException{
        //擷取一個儲存檔案的路徑
        String path = request.getSession().getServletContext().getRealPath("/upload");
        //路徑可能不存在,要對其判斷,如果不存在則建立
        File sourceFloader = new File(path);
        if (!sourceFloader.exists()) {
            //建立檔案夾
            sourceFloader.mkdir();
        }
        //擷取到上傳的檔案
        Iterator<String> filesNames = multiRequest.getFileNames();
        List list = new ArrayList();
        while(filesNames.hasNext()){
            //擷取到每一個檔案
            String fileName =filesNames.next();
            MultipartFile file =  multiRequest.getFile(fileName);
            // 擷取檔案名
            String filename = file.getOriginalFilename();
            // 得到輸入流
            InputStream inputStream = file.getInputStream();
            if(!file.isEmpty()){
                FileUtils.copyInputStreamToFile(inputStream, new File(path, filename));
            }
           list.add("upload\\"+filename);
        }
        model.addAttribute("list" ,list);
        return "success";
    }           

繼續閱讀