2012-10-08 40 views
1

我使用Spring 3和實施Uploadify。問題是,這些文件正在更新,但在完成文件上傳時它會提供HTTP錯誤404。我嘗試了所有可能的解決方案,但都沒有成功。彈簧3 uploadify給予的404

上傳文件。值正確存儲在數據庫中,只是我得到HTTP錯誤404。任何幫助表示讚賞,並提前致謝。

解決方案是:

Finally i found the solution but it is lame. 

I removed the return "" and changed the method as void. Thats it. 

But still i don't understand why the same code is working in Spring 2.5.6 and not in 3. 

截圖的URL:http://imgur.com/bf3qo

JSP頁面

$(function() { 
    $('#file_upload').uploadify({ 
     'swf'  : 'scripts/uploadify.swf',    
     'fileObjName' : 'the_file', 
     'fileTypeExts' : '*.gif; *.jpg; *.jpeg; *.png',   
     'multi' : true,   
     'uploader' : '/photo/savePhoto', 
     'fileSizeLimit' : '10MB', 
     'uploadLimit' : 50, 
     'onUploadStart' : function(file) { 
      $('#file_upload').uploadify('settings', 'formData', {'trip_id' :'1', 'trip_name' :'Sample Trip', 'destination_trip' :'Mumbai','user_id' :'1','email' :'[email protected]','city_id' :'12'}); 
      }, 
     'onQueueComplete' : function(queueData) { 
      console.log('queueData : '+queueData); 
      window.location.href = "trip/details/1"; 
     } 
    }); 
}); 

的控制器

@RequestMapping(value="photo/{action}", method=RequestMethod.POST) 
public String postHandler(@PathVariable("action") String action, HttpServletRequest request) { 
if(action.equals("savePhoto")) 
{ 
     try{ 
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request; 
    MultipartFile file = multipartRequest.getFile("the_file"); 
    String trip_id = request.getParameter("trip_id"); 
    String trip_name = request.getParameter("trip_name"); 
    String destination_trip = request.getParameter("destination_trip"); 
    String user_id = request.getParameter("user_id"); 
    String email = request.getParameter("email"); 
    String city_id = request.getParameter("city_id"); 
     photo.savePhoto(file,trip_id,trip_name,destination_trip,user_id,email,city_id); 
    photo.updatetrip(photo_id,trip_id); 
    }catch(Exception e){e.printStackTrace();} 
} 
return ""; 
} **Solution** : Change the method return type as void and remove the return 

Spring配置

<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"> 
    <property name="maxUploadSize" value="10000000"/> 
</bean> 

web.xml文件中

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>project_name</display-name> 
    <distributable/> 
    <context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>classpath:log4j.properties</param-value> 
    </context-param> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/project_name-servlet.xml,/WEB-INF/applicationContext-jdbc.xml</param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
    <servlet-name>project_name</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>project_name</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <session-config> 
    <session-timeout>60</session-timeout> 
    </session-config> 
</web-app> 

回答

1

也許你根本就沒有trip/details/1頁面在您的應用程序?

編輯:

變化window.location.href = "trip/details/1";

window.location.href = "<%= request.getContextPath() %>/trip/details/1"; 
+0

旅程/細節在那裏。它也會重定向到頁面,但是一旦上傳爲100%,就會給出HTTP錯誤404。 – Rajkumar

+0

因此,這意味着您要轉到不存在的頁面。我知道如何檢查它。當您收到404錯誤時,請張貼您上傳照片(從瀏覽器複製粘貼)和網址的網址。 – user1516873

+0

@ user1516873我的意思是一旦上傳完成了100%,就會給我錯誤,然後重定向到「trip/details/1」,這意味着onQueueComplete操作正確觸發。我唯一擔心的是,它爲什麼會給出「404」錯誤。請點擊這個網址,看看我究竟在說什麼(http://imgur.com/bf3qo)。 – Rajkumar

0

的文件上傳。值在DB妥善儲存,只是我 我得到HTTP錯誤404

這告訴我的是,你的要求是正確的被提交到網址在'/photo/*' 並正確由postHandler處理()方法。

因爲您的Web應用程序不知道如何處理""postHandler()方法試圖引導您的網址,所以您會收到404。

有最有可能的(我在這裏做一些假設,這將會是有益的,如果你包括在web.xml)沒有設立處理「」你的控制器返回的請求映射器;讓你的控制器返回某種有意義的視圖名稱的,有一個有效的servlet映射,你不會得到一個404

+0

更新了主帖,並添加了web.xml。這個完全相同的代碼在Spring 2.5.6下工作正常,url看起來像這樣PhotoAction.do?action=savePhoto和其他代碼是相同的。這是我無法找到問題的東西。 – Rajkumar

0

我也有這個問題。我添加了「@ResponseBody」並獲得了正確的結果。 我認爲問題是,如果沒有標註「@ResponseBody」,返回的字符串是由一些奇怪的分解處理和JavaScript代碼獲取意想不到的形式的響應。

@RequestMapping(value="/uploadFile",method=RequestMethod.POST) 
public @ResponseBody String upload(HttpServletResponse response, 
         HttpServletRequest request) throws IOException{