1

我正在使用 Spring 3 并实现 Uploadify。问题是,文件正在正确更新,但在文件上传完成后给出 HTTP 错误 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.

截图网址: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' :'s@s.com','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

弹簧配置

<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>
4

3 回答 3

1

也许您只是没有trip/details/1在您的应用程序中分页?

编辑:

更改window.location.href = "trip/details/1";

 window.location.href = "<%= request.getContextPath() %>/trip/details/1";
于 2012-10-08T15:16:30.647 回答
0

文件已上传。值正确存储在数据库中,只是我收到 HTTP 错误 404。

这告诉我的是,您的请求已正确提交到 URL'/photo/*' 并由 postHandler() 方法正确处理。

您收到 404 是因为您的 Web 应用程序不知道如何处理""postHandler()方法试图将您定向到的 url。

很可能(我在这里做了一些假设,如果您包含 web.xml 会很有帮助)请求映射器未设置为处理您的控制器返回的“”;让您的控制器返回某种有意义的视图名称,该名称具有有效的 servlet 映射,您将不会得到 404。

于 2012-10-08T15:26:01.560 回答
0

我也有这个问题。我添加了“@ResponseBody”并得到了正确的结果。我认为问题在于没有注释“@ResponseBody”,返回的字符串由一些奇怪的解析器处理,并且javascript代码得到意外形式的响应。

@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
public @ResponseBody String upload(HttpServletResponse response,
                        HttpServletRequest request) throws IOException{
于 2016-02-18T02:16:03.740 回答