欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC案例05--SpringMVC跨服务器文件上传

程序员文章站 2022-06-03 10:37:15
...

项目说明

  • 使用两台服务器,一个用作应用服务器,一个用作文件服务器
  • 用到新的jar包jersey,它是由sun公司提供

环境搭建

准备两个tomcat服务器,修改图片服务器的端口

tomcat应用服务器,HTTP端口号和JMX端口号不用修改

SpringMVC案例05--SpringMVC跨服务器文件上传

tomcat文件服务器,HTTP端口号和JMX端口号都需要修改

SpringMVC案例05--SpringMVC跨服务器文件上传

修改tomcat图片服务器的默认配置

打开tomcat安装路径\conf\web.xml > 添加readonly=false

pom文件

  • 文件上传用到的依赖commons-fileupload
  • 跨服务器用到的依赖jersey
 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <!--    设置版本锁定-->
        <spring.version>5.0.2.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.18.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.18.1</version>
        </dependency>
    </dependencies>

配置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>dispatherServlet</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>dispatherServlet</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>

编写应用服务器端代码

jsp代码

<form action="user/testUpload" method="post" enctype="multipart/form-data">
        选择文件:<input type="file" name="upload"><br>
        <input type="submit" value="上传"><br>
</form>

配置springmvc.xml

  • 配置文件解析器
  • spring创建容器时要扫描的包
  • 配置视图解析器
  • 开启springmvc框架对注解的支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">
    <!--    spring创建容器时要扫描的包-->
    <context:component-scan base-package="com.cncs"></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>
	<!--    配置文件解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
       <property name="maxUploadSize" value="10485760"/>
    </bean>
    <!--    配置不过滤的静态资源-->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

    <!--开启springmvc框架对注解的支持-->
    <mvc:annotation-driven/>

    <mvc:default-servlet-handler/>
</beans>

controller代码

  • testUpload()参数名需要和jsp表单中文件选择域的属性的取值保持一致
  • path路径是指文件服务器的访问地址
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/testUpload")
    public String testUpload(MultipartFile upload) throws Exception {
        System.out.println("testUpload executing...");
        String path="http://localhost:8090/picServer/uploads/";
        //获取文件名
        String filename = upload.getOriginalFilename();
        //获取文件
        Client client = Client.create();
        WebResource webResource = client.resource(path + filename);
        webResource.put(String.class,upload.getBytes());
        return "success";
    }
}

文件服务器端工程建好后,只要能运行就OK,不用编写代码

项目编译后的target文件夹内新建一个uploads文件夹,用于接收上传文件。

PS:上传的文件不要带有中文名,否则会报错: SpringMVC跨服务器上传错误 - Request processing failed; nested exception is com.sun.jersey.api.client.Uniform

相关标签: SpringMVC