1. 程式人生 > >(MultipartHttpServletRequest) request物件轉換錯誤

(MultipartHttpServletRequest) request物件轉換錯誤

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;物件轉換錯誤,

去網上查了一下,一般都是兩處需要注意:

1)首先在servlet.xml裡需要配置(SpringMVC封裝了commons-fileupload上傳元件)


<bean id="multipartResolver"   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    	<!-- 設定上傳檔案的最大尺寸為10MB -->  
	    <property name="maxUploadSize">  
	        <value>1048576000</value>  
	    </property>  
	    	<property name="maxInMemorySize">
			<value>1024</value>
		</property>
    </bean>

2)需要檢查一下form表單是否有這個屬性enctype="multipart/form-data"

<form name="form" method="post" action="home.htm" enctype="multipart/form-data" id="documentForm">

一般這兩處都加上,(MultipartHttpServletRequest) request;轉換都正常了,可是我在專案裡還是報錯。於是在方法判斷一下,尼瑪,提示沒有enctype="multipart/form-data"。見鬼了。

action程式碼:


 public void upsendFile(String path,HttpServletRequest request) throws IllegalStateException, IOException{
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
        if (multipartResolver.isMultipart(request)) {  
            System.out.println("ss");
        }
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      
        MultipartFile multipartFile = multipartRequest.getFile("fileName");
        File file = new File(path + multipartFile.getOriginalFilename());
        multipartFile.transferTo(file);
        log.debug(multipartFile.getOriginalFilename());
    }

在Google百度一會也沒有找到問題所在。就在想是不是js哪裡有問呢。js哪裡是使用window.location.href("")跳轉的。跳轉的時候沒提交表單?但也不會,前面的重新命名也是用window.location.href("")跳轉也沒有問題

於是乎我就改成document.documentForm.action="";果然是沒有提交表單。

js程式碼:


$("#execUpsend").click(function(){
		 var endPath=$("input[name=fileName]").val();
		 if (""==endPath) 
		 {
			alert("請選擇檔案!");
			return;
		 }
		 document.documentForm.action = "/swbz/ywgl?fileB=2&paths="+paths+"&endPath="+endPath;
         document.documentForm.submit();
	 });