SpringMVC上传文件的两种方法
作者:残缺的孤独 发布时间:2023-11-17 14:27:20
标签:SpringMVC,上传
在该示例中,阐述了SpringMVC如何上传文件。
1、上传页面upload.jsp
<body>
<form action="/TestSpringMVC3/data/uploadfile" enctype="multipart/form-data" method="post">
file:<input type="file" name="file"><br>
<input type="submit" value="upload file">
</form>
</body>
2、controller配置文件
<?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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
使Spring支持自动检测组件,如注解的Controller
-->
<context:component-scan base-package="cn.com.yy.controller"/>
<!-- 开启注解配置 -->
<mvc:annotation-driven/>
<!-- 支持JSP JSTL的解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxUploadSize" value="10485760000"/>
<property name="maxInMemorySize" value="40960"/>
</bean>
</beans>
主要是添加了文件上传的解析器,配置了默认编码,最大的上传大小以及缓存大小等参数。
3、Controller
package cn.com.yy.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
@RequestMapping("/data")
public class FileUploadController {
/**
* method1:通过参数CommonsMultipartFile进行解析
* @RequestParam("file")中的file对应于upload.jsp中的file类型的name对应的名称
* @param file
* @return
* @throws IOException
*/
@RequestMapping(value="/uploadfile")
public String upload1(@RequestParam("file") CommonsMultipartFile file) throws IOException{
//获取文件名称
String fileName = file.getOriginalFilename();
//写入本地磁盘
InputStream is = file.getInputStream();
byte[] bs = new byte[1024];
int len;
OutputStream os = new FileOutputStream(new File("D:/temp/" + fileName));
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
is.close();
return "upload_success";
}
@RequestMapping("/upload")
public String toPage(){
return "upload";
}
}
4、返回页面upload_success.jsp
<body>
upload file success!!
</body>
5、测试
访问 http://localhost:8080/TestSpringMVC3/data/upload 跳转到上传页面
选择文件上传
点击上传会跳转到上传成功页面。
上述方法只是简单的讲解了SpringMVC如何上传文件。
第二种方法:使用SpringMVC封装的方法进行文件上传
/**
* 使用SpringMVC封装好的方法进行文件上传
* @param request
* @param response
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping("/uploadfile2")
public void upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
//获取解析器
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断是否是文件
if(resolver.isMultipart(request)){
//进行转换
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
//获取所有文件名称
Iterator<String> it = multiRequest.getFileNames();
while(it.hasNext()){
//根据文件名称取文件
MultipartFile file = multiRequest.getFile(it.next());
String fileName = file.getOriginalFilename();
String localPath = "D:/temp/" + fileName;
File newFile = new File(localPath);
//上传的文件写入到指定的文件中
file.transferTo(newFile);
}
}
}
该方法上传文件效率更优。
来源:http://blog.csdn.net/yyywyr/article/details/44571353
0
投稿
猜你喜欢
- 我们知道,进入百度图片后,输入一个关键字后,首先看到的是很多缩略图,当我们点击某张缩略图时,我们就可以进入到大图显示页面,在大图显示页面,中
- 一、什么是热部署?热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用。二、什么是SpringBoot热部署?SpringBoot
- IoC的概念介绍控制反转(IOC)模式(又称DI:Dependency Injection)就是Inversion of Control,控
- 今天没有继续学习新知识点,画出了一个随鼠标画出的 图形,知识点全都是之前学的,话不多说,先上图:代码部分: // ope
- 质数又称素数。一个大于1的自然数,如果除了1和它自身外,不能被其他自然数整除的数;否则称为合数。根据算术基本定理,每一个比1大的整数,要么本
- 1.设置url-pattern为*.do(最为常见的方式)只要你的请求url中包含配置的url-pattern,该url就可以到达Dispa
- 问题描述:String preStr = "a.b.c"; // 这里要把该字符串按小圆点进行分割,成"a&q
- 如果不知道,类的静态变量存储在那? 方法的局部变量存储在那? 赶快收藏Java内存区域主要可以分为共享内存,堆、方法区和线程私有内存,虚拟机
- Map 中ConcurrentHashMap是线程安全的,但不是所有操作都是,例如get()之后再put()就不是了,这时使用merge()
- 这里使用的是dynamic-datasource-spring-boot-starter ,它是一个基于springboot的快速集成多数据
- 一、JDBC简介JDBC是连接java应用程序和数据库之间的桥梁。什么是JDBC?Java语言访问数据库的一种规范,是一套API。JDBC
- [LeetCode] 3. Longest Substring Without Repeating Characters 最长无重复字符的子
- 前言最近看插件库上少有的取色器大都是圆形的或者奇奇怪的的亚子,所以今天做两个矩形的颜色取色器提示:以下是本篇文章正文内容,下面案例可供参考一
- 前言假如有人问你这么几个问题,看能不能答上来Mybatis Mapper 接口没有实现类,怎么实现的 * JDK * 为什么不能对类进
- 这篇文章主要介绍了JAVA如何定义构造函数过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 一、API简介Thread.sleep()是Thread类的一个静态方法,使当前线程休眠,进入阻塞状态(暂停执行),如果线程在睡眠状态被中断
- 英文设置加粗可以在xml里面设置: <SPAN style="FONT-SIZE: 18px">androi
- 项目结构项目路径可以自己定义,只要路径映射正确就可以pom.xml <properties> <spring.versio
- Java获取环境变量Java 获取环境变量的方式很简单: System.getEnv() 得到所有的环境变量Syste
- ObjectMapper 忽略字段大小写核心代码:ObjectMapper mapper = new ObjectMapper();mapp