LayUIコンポーネントjavaベースのファイルアップロード


LayUIコンポーネントjavaベースのファイルアップロード
  • Mavenガイド
    	<dependency>
    	    <groupId>commons-fileuploadgroupId>
    	    <artifactId>commons-fileuploadartifactId>
    	dependency>
    
  • jspページ
  • jsページ
    layui.use('upload', function(){
           
        var upload = layui.upload;
        //  Excel  
    	var uploadInst=upload.render({
           
    	    elem: '#uploadfile', //    
    	    url: projectName+'/hardware/uploadfile.do', //    
    	    method:'POST',
    	    accept: 'file', //         
    	    size: 204800, //              ,   KB
    	    done: function(res){
            //      
    	    	console.log(res)
    	    	layer.msg("    ",{
           icon: 1});
    	        parent.location.reload();
    	    },
    
    	    error: function(){
           
    	      //      
    	    	layer.msg("    ",{
           icon: 0});
    	    }
    	  });
    }
  • 構成SpringMVCアップロード
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	    
    	    <property name="maxUploadSizePerFile" value="5242880"/>
    	    <property name="defaultEncoding" value="UTF-8"/>
    	    <property name="resolveLazily" value="true"/>
    bean>
    
  • javaバックグラウンド
    /**
    	 *   Excel  
    	 * @throws IOException 
    	 * @throws IllegalStateException 
    	 */
    @RequestMapping("/uploadfile.do")
    @ResponseBody
    public Map<String,String> uploadfile(@RequestParam("file") MultipartFile file,HttpServletRequest request) 
        throws IllegalStateException, IOException{
           
        if(null!=file){
           
            String fileOrigName=file.getOriginalFilename();//      
            if (!fileOrigName.contains(".")) {
           
                throw new IllegalArgumentException("     ");
            }
            //     
            fileOrigName = fileOrigName.substring(fileOrigName.lastIndexOf("."));
            String newfileName =CommonFunction.strAutoInfo() +fileOrigName;
            //            D:\apache-tomcat-8.0.53\webapps\QrCode\
            String uploadPath=request.getServletContext().getRealPath("");
            //     QrCode
            String projectName=request.getServletContext().getContextPath().substring(1);
            //             D:\apache-tomcat-8.0.53\webapps/uploadfile\
            String newdestPath= uploadPath.replaceAll(projectName, "uploadfile")+newfileName;
            //             
            File newfile=new File(newdestPath);
            if(!newfile.exists() && !newfile.isDirectory()){
           
                System.out.println(newfile+"     ,    ");
                //    
                newfile.mkdirs();
            }
            file.transferTo(newfile);
        }
        Map<String,String> map=new HashMap<String,String>();
        map.put("code", "0");
        map.put("msg","    ");
        return map;	
    }