1. 程式人生 > >MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;型別轉換異常問題

MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;型別轉換異常問題

	String root = request.getParameter("root");
		// 先例項化一個檔案解析器
        CommonsMultipartResolver coMultipartResolver = new CommonsMultipartResolver(request.getSession()
                .getServletContext());

        // 判斷request請求中是否有檔案上傳
        if (coMultipartResolver.isMultipart(request)) {
            // 轉換request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            // 獲得檔案
            MultipartFile file = multiRequest.getFile("Filedata");
            if(!file.isEmpty()){
            	resourceMng.saveFile(root, file);
            }
        }
		request.setAttribute("root", root);
		
		ResponseUtils.renderText(response, "");
		return null;
	}
上傳檔案時,
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

報錯型別轉換異常,原來是需要在Spring的配置檔案進行如下配置:

<!-- 專題上傳檔案 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
     <property name="maxUploadSize" value="104857600"/>  
       <property name="maxInMemorySize" value="4096"/>  
    </bean>