传统的上传文件方式:
坐标引用:
<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>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </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> </dependencies>
index.jsp:
<%-- Created by IntelliJ IDEA. User: administrator Date: 2020/3/5 Time: 19:12 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>文件上传</h3> <form action="user/fileupload1" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="upload" /><br/> <input type="submit" value="上传" /> </form> </body> </html>
UserController.java:
@RequestMapping("/fileupload1") public String fileupload1(HttpServletRequest request) throws Exception { System.out.println("文件上传..."); //使用fileupload组件完成上传文件 //上传位置 String path = request.getSession().getServletContext().getRealPath("/uploads/"); //判断,该路径是否存在 File file = new File(path); if(!file.exists()){ //创建文件夹 file.mkdirs(); } //解析request对象,获取上传文件项 DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); //解析request List<FileItem> items = upload.parseRequest(request); //遍历 for(FileItem item : items){ //进行判断,当前item对象是否是上传文件项 if (item.isFormField()){ //说明是普通表单项 }else { //说明是上传文件项 //获取上传文件名称 String filename = item.getName(); //把文件的名称设置成为唯一值 String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid+"_"+filename; //完成上传 item.write(new File(path,filename)); //删除临时文件 item.delete(); } } return "success"; }
点击选择文件,找文件上传图片在你的Idea工程下的target中
springMVC的文件上传方式
springMVC框架文件上传的原理分析
坐标用上面的,配置一下springmvc.xml:
<?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"> <!--开启注解扫描--> <context:component-scan base-package="cn"/> <!--视图解析器--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!--前端控制器,哪些静态资源不拦截--> <mvc:resources location="/js/" mapping="/js/**"/> <mvc:resources location="/css/" mapping="/css/**"/> <mvc:resources location="/images/" mapping="/images/**"/> <!--配置文件解析器--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--最大10M--> <property name="maxUploadSize" value="10485760" /> </bean> <!--开启springMVC框架注解的支持--> <mvc:annotation-driven /> </beans>
index.jsp:
<%-- Created by IntelliJ IDEA. User: administrator Date: 2020/3/5 Time: 19:12 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>传统文件上传</h3> <form action="user/fileupload1" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="upload" /><br/> <input type="submit" value="上传" /> </form> <h3>springMVC文件上传</h3> <form action="user/fileupload2" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="upload" /><br/> <input type="submit" value="上传" /> </form> </body> </html>
UserController.java:
@RequestMapping("/fileupload2") public String fileupload2(HttpServletRequest request, MultipartFile upload) throws Exception { System.out.println("springMVC文件上传..."); //使用fileupload组件完成上传文件 //上传位置 String path = request.getSession().getServletContext().getRealPath("/uploads/"); //判断,该路径是否存在 File file = new File(path); if(!file.exists()){ //创建文件夹 file.mkdirs(); } //说明是上传文件项 //获取上传文件名称 String filename = upload.getOriginalFilename(); //把文件的名称设置成为唯一值 String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid+"_"+filename; //完成上传 upload.transferTo(new File(path,filename)); return "success"; }
点击选择文件,找文件上传图片在你的Idea工程下的target中
跨服务器上传文件
我们在自己电脑再搭建一个tomcat为图片服务器,模仿跨服务器调用图片服务器
再设置一个tomcat
new一个moudle,再建一个工程fileupdateserver
启动一下tomcat
新加个坐标地址
<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>
UserController.java:
@RequestMapping("/fileupload3") public String fileupload3(MultipartFile upload) throws Exception { System.out.println("跨服务器文件上传..."); //定义上传文件服务器路径 String path = "http://localhost:9090/uploads/"; //说明是上传文件项 //获取上传文件名称 String filename = upload.getOriginalFilename(); //把文件的名称设置成为唯一值 String uuid = UUID.randomUUID().toString().replace("-", ""); filename = uuid+"_"+filename; //完成上传,跨服务器 //创建客户端的对象 Client client = Client.create(); //和图片服务器进行连接 WebResource webResource = client.resource(path + filename); //上传文件 webResource.put(upload.getBytes()); return "success"; }
- 版权声明:文章来源于网络采集,版权归原创者所有,均已注明来源,如未注明可能来源未知,如有侵权请联系管理员删除。