记录调了很久的bug,springmvc跨域上传文件的错误。基本上除了404外,其他400 403 405 409都可以在站内找到讲的很好的解决方法,但404是真的坑。
这次遇到的404是因为两个服务器都配置在同一个“物理”服务器(或者说同一服务器软件)上引起的,可能是同时占用\apache-tomcat-9.0.7\webapps\ROOT\路径(一开始只安装配置apache-tomcat-9.0.7,且上传文件默认这个路径)的原因。解决方法是再安一个tomcat作为其中一个的服务器。
不同环境下这个解决方法可能不适用吧。能解决就很幸运。
源码
web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
resources目录下springmvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.itcast"></context:component-scan>
<!--视图解析器对象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--前端控制器,不被拦截的资源-->
<mvc:resources mapping="/css/**" location="/scc/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<!--文件解析器对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<!--开启springmvc框架的注解支持-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
jsp文件
<h3>springmvc文件上传</h3>
<form action="/user/fileupload3" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="上传">
</form><br/>
Controller.java文件
@Controller
@RequestMapping("/user")
public class UserController {
/**
* 跨服务器文件上传
* @param upload
* @return
* @throws IOException
*/
@RequestMapping("/fileupload3")
public String fileupload3(MultipartFile upload) throws IOException {
System.out.println("跨站 springmvc upload...");
//定义上传文件服务器路径
String path = "http://localhost:9090/uploads/";
//说明上传文件项
//获取上传文件名
String filename = upload.getOriginalFilename();
//将文件名设置为唯一
String uuid = UUID.randomUUID().toString().replace("-", "");
//文件名不是全英文会导致400错误!!!
//filename = uuid + "_" + filename;
filename = uuid + filename.substring(filename.lastIndexOf("."));
System.out.println(filename);
//创建客户端对象
Client client = Client.create();
//和文件服务器进行连接
WebResource webResource = client.resource(path + filename);
//上传文件
webResource.put(upload.getBytes());
return "success";
}
}
错误400
原因:上传文件名不是全英文。上面源码已经可以解决了。
措施:源码处
错误404
原因:在idea中配置的两tomcat服务器(上传服务器,文件服务器),在物理上是同一个软件。默认下,项目上传的文件会保存在F:\apache-tomcat-9.0.7\webapps\ROOT\uploads\下(uploads是自定义的),当配置的上传服务器和文件服务器在物理上是同一个软件时,会共用这个目录,猜测会起冲突。解决后也反映了这个问题。
错误的配置:上传服务器,文件服务器(在我电脑上tomcat9只安装了一个)
正确配置:再安装一个tomcat,将其中一个(我选文件服务器)配置到不同的tomcat上
结果:跨站上传成功,文件上传到文件服务器的一个半默认目录F:\apache-tomcat-8.5.31\webapps\ROOT\uploads下。但是存在一个问题,每次从新部署文件服务器,这个目录都会被清空。