Python 官方文档:入门教程 => 点击学习
一个简单的springMVC文件上传例子 所需的依赖 只需要这个就好了。在idea的依赖关系图中,commons-fileupload包含了commons-io依赖 <d
一个简单的springMVC文件上传例子
所需的依赖
只需要这个就好了。在idea的依赖关系图中,commons-fileupload包含了commons-io依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
一个简单的文件上传的页面
<fORM action="Http://localhost:8080/springmvc_WEB_exploded/fff" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="filer_input" multiple="multiple">
<input type="submit" value="Submit">
</form>
在spring的配置文件中注入一个文件上传的bean
spring.xml
<!-- 文件上传的bean-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--上传文件的最大大小,单位为字节 -->
<property name="maxUploadSize" value="17367648787"></property>
<!-- 上传文件的编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
实例代码
@PostMapping("/fff")
public String file(@PathVariable("file")MultipartFile file, httpservletRequest req) throws IOException {
//判断文件不存在
if (file.isEmpty()){
return "faile.html";
}
//我想放到这个地方文件的路径
String realPath = req.getServletContext().getRealPath("/WEB-INF/file");
//返回客户端文件系统中的原始文件名
String filename = file.getOriginalFilename();
File myFile = new File(realPath, filename);
if (!myFile.getParentFile().exists()){
myFile.getParentFile().mkdir();
System.out.println("文件夹被创建了");
}
//将前端传过来的文件,传到自己new的File对象中
file.transferTo(myFile);
return "success.html";
}
注意事项:
1、文件上传请求必须用post请求。
2、注意路径问题
3、前端的form表单必须添加 enctype="multipart/form-data" 这个属性
--结束END--
本文标题: springmvc实现文件上传功能
本文链接: https://www.lsjlt.com/news/121799.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0