我正在使用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
spring config
<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>
答案 0 :(得分:1)
也许你的应用程序中没有页面trip/details/1
?
编辑:
将window.location.href = "trip/details/1";
更改为
window.location.href = "<%= request.getContextPath() %>/trip/details/1";
答案 1 :(得分:0)
上传文件。值正确存储在DB中,仅存储在i中 我收到HTTP错误404.
这告诉我的是,您的请求已正确提交到'/photo/*'
的网址
并且由postHandler()方法正确处理。
您收到的是404,因为您的网络应用程序不知道如何使用""
方法试图引导您的postHandler()
网址。
最有可能(我在这里做了一些假设,如果你包含web.xml会很有帮助)请求映射器没有设置来处理你的控制器返回的“”;让你的控制器返回某种有意义的视图名称,它具有有效的servlet映射,你不会得到404.
答案 2 :(得分:0)
我也有这个问题。我添加了#34; @ ResponseBody&#34;并得到了正确的结果。 我认为问题是没有注释&#34; @ ResponseBody&#34;,返回的字符串由一些奇怪的解析器处理,javascript代码获得意外形式的响应。
@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
public @ResponseBody String upload(HttpServletResponse response,
HttpServletRequest request) throws IOException{