package com.itheima.web.controller;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
/**
* springmvc提供的文件上传操作
*
* @return
* @throws Exception
*/
@Controller("fileUploadController3")
@RequestMapping("/springmvc")
public class FileUploadController3 {
//定义一个变量用于指定图片服务器的地址,在实际的开发中,可以写到配置文件中
private static final String IMAGESERVERPATH = "http://localhost:9090/day02_springmvc5_02image/uploads/";
/**
* 跨服务器的文件上传
* @param imageName
* @param uploadFile
* @param request
* @return
* @throws Exception
*/
@RequestMapping("/testFileUpload3")
public String testFileUpload2(String imageName, MultipartFile uploadFile, HttpServletRequest request)
throws Exception {
System.out.println("文件上传的方法执行了");
//1.获取文件名
String fileName = uploadFile.getOriginalFilename();
//2.随机化文件名
String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
fileName = uuid + "_" + fileName;
//3.获取jersey的jar包中提供Client对象
Client client = Client.create();
//4.建立和图片服务器的联系
WebResource resource = client.resource(IMAGESERVERPATH + fileName);
//5.把文件写到域
String result = resource.put(String.class, uploadFile.getBytes());
System.out.println(result);
return "success";
}
}
FileUploadController3跨服务器上传文件类
<?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/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 告知spring创建容器时要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 配置springmvc的视图解析器,用于找到响应的jsp -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置springmvc提供的文件解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
<!-- 开启springmvc的注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 设置静态资源不参与springmvc编码的过滤 -->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/scripts/" mapping="/javascripts/**"/>
</beans>
springmvc.xml配置文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
</head>
<body>
执行成功
</body>
</html>
success.jsp测试页面
<?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"
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>day02_springmvc5_01response</display-name>
<!-- 配置springmvc核心控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 请求过滤器,把请求交给springmvc处理 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置springmvc提供的编码过滤器 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml配置文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>springmvc文件上传</title>
</head>
<body>
<!-- 跨服务器文件上传表单 -->
<form action="springmvc/testFileUpload3" method="post" enctype="multipart/form-data">
图片名称:<input type="text" name="imageName"/><br/>
图片:<input type="file" name="uploadFile"/><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
index.jsp测试页面
除了原本的tomcat负责接受上传文件请求
还要再另外配置一个tomcat,用于保存上传的文件,该tomcat端口改为9090,/WEB-INF/page/uoploads,和新建一个工程项目day02_springmvc5_02image