#跨服务器文件上传 @FDDLC
程序员文章站
2022-06-02 14:12:05
...
先修课:#文件上传 #SpringMVC文件上传 @FDDLC
Maven依赖:明面上只需要导spring-webmvc、commons-fileupload(由浏览器上传到当前服务器)、jersey-client(由当前服务器上传到文件服务器)。
需要两台服务器,学习的话在本地跑两个Web项目就行,端口号要不一样,不然会有冲突!
表单核心部分:
<form method="post" enctype="multipart/form-data" action="test">
请选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="提交"/>
</form>
文件上传的核心代码:
public String test(@RequestParam("upload") MultipartFile multipartFile) throws Exception {
String path = "http://localhost:9080/file_server_war_exploded/upload/";
Client client = Client.create();
WebResource resource = client.resource(path + multipartFile.getOriginalFilename());
resource.put(multipartFile.getBytes());
return "success";
}
说明:
1、需要在文件服务器下新建upload文件夹!如图:
2、主服务器可能会被禁止往文件服务器里写文件:jersey.api.client.UniformInterfaceException: PUT @FDDLC
最后附上源码:
项目结构:
pom.xml的内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>P058_FileUpload_AcrossServer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>P058_FileUpload_AcrossServer Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>
</dependencies>
<build>
<finalName>P058_FileUpload_AcrossServer</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
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>
<!--关联springmvc.xml,让其中的配置生效!-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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:mvc="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启对Spring MVC注解的支持-->
<mvc:component-scan base-package="cn.liuxingchang.controller"></mvc:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--配置对上传文件进行解析的文件解析器-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<property name="maxUploadSize" value="10485760"></property><!--以字节为单位-->
</bean>
</beans>
index.jsp的内容:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form method="post" enctype="multipart/form-data" action="test">
请选择文件:<input type="file" name="upload"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
success.jsp的内容:
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>success</h3>
</body>
</html>
HelloController.java的内容:
package cn.liuxingchang.controller;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class HelloController {
@RequestMapping("/test")
public String test(@RequestParam("upload") MultipartFile multipartFile) throws Exception {
String path = "http://localhost:9080/file_server_war_exploded/upload/";
Client client = Client.create();
WebResource resource = client.resource(path + multipartFile.getOriginalFilename());
resource.put(multipartFile.getBytes());
return "success";
}
}
上一篇: DVWA之反射型XSS
下一篇: Python3的FTP文件上传下载