上傳文件夾,后台獲取里面的文件;批量上傳文件夾中的圖片


1、上傳文件夾,提取文件夾中多個文件;若文件夾中存在目錄,也會提取目錄中的文件;

html處理方式:

<input type="file" name="file" multiple="multiple" webkitdirectory>
function getImpImageTpl() {
        var tpl = '<div style="padding: 10px;">' +
            '<form id="impForm" action="<%=basePath%>commericalPart/importModelPart" method="POST" enctype="multipart/form-data" target="impFrame">' +
            '<iframe name="impFrame" style="display: none;"></iframe>' +
            '<table id="impTable" class="table">' +
            '<tr>' +
            '<td>選擇文件:</td>' +
            '<td><input type="file" name="file" multiple="multiple" webkitdirectory></td>' +
            '</tr>' +
            '<tr>' +
            '<td>導入配件圖片的目錄</td>' +
            '<td><input type="text" name="imagePath"   ></td>' +
            '</tr>' +
            '<tr>' +
            '<td></td>' +
            '<td><input type="button" value="開始導入" class="btn btn-primary" id="submitForm" onclick="doStartImpImage();" ></td>' +
            '</tr>' +
            '</table>' +
            '</form>' +
            '</div>';
        return tpl;
    }

  后台接收:

 /**
     * 導入配件圖片
     * @return
     */
    @RequestMapping("/importPartImage")
    @ResponseBody
    public Object importPartImage(@RequestParam("file") List<MultipartFile> file,String imagePath){
        Map<String,Object> map = new HashMap<String, Object>();
        String filePath = "E:/lazyli/image/part/" + imagePath+"/";
        File file2 = new File(filePath);
        if(!file2.exists()){
            file2.mkdirs();
        }
        OutputStream os = null;
        String exceptionImage = "";
        try{
            for(MultipartFile file1 : file){
                //圖片的原始名稱
                String oriName = file1.getOriginalFilename();
                exceptionImage = oriName;
                String filePath2 = filePath+oriName;
                File file3 = new File(filePath2);
                if(file3.exists()){
                    System.out.println("已存在圖片,無需寫入磁盤中");
                    continue;
                }
                System.out.println("沒有存在,將圖片存放到磁盤中");
                os = new FileOutputStream(filePath2);
                os.write(file1.getBytes());
            }
            map.put("success",true);
            System.out.println("圖片存放磁盤目錄:"+imagePath);
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false );
            map.put("exceptionImage", exceptionImage);
        }

        return map;
    }

  


免責聲明!

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



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