spring mvc 文件上传工具类
虽然文件上传在框架中,已经不是什么困难的事情了,但自己还是开发了一个文件上传工具类,是基于springmvc文件上传的。
工具类只需要传入需要的两个参数,就可以上传到任何想要上传的路径:
参数1:HttpServletRequest request
参数2:String storePath //文件存储相对路径 ,例如:"/upload","/image","/local/file"
返回值:上传到服务器的相对路径
一:代码实现
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.UUID; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; public class FileUploadUtils {
/**
*
* @param request
* @param storePath 文件存储相对路径 ,例如:"/upload","/image","/local/file"
* @return
*/
public static String tranferFile(HttpServletRequest request,String storePath){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//先判断request中是否包涵multipart类型的数据,
if(multipartResolver.isMultipart(request)){
//再将request中的数据转化成multipart类型的数据
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator iter = multiRequest.getFileNames();
while(iter.hasNext()){
MultipartFile file = multiRequest.getFile((String)iter.next());
if(file != null){
String originalFileName = file.getOriginalFilename(); String path =request.getSession().getServletContext().getRealPath(storePath);
//得到存储到本地的文件名
String localFileName=UUID.randomUUID().toString()+getFileSuffix(originalFileName);
//新建本地文件
File localFile = new File(path,localFileName);
//创建目录
File fileDir=new File(path);
if (!fileDir.isDirectory()) {
// 如果目录不存在,则创建目录
fileDir.mkdirs();
} String src=storePath+"/"+localFileName;
//写文件到本地
try {
file.transferTo(localFile);
return src;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
/**
* 获取文件后缀
* @param originalFileName
* @return
*/
public static String getFileSuffix(String originalFileName){
int dot=originalFileName.lastIndexOf('.');
if((dot>-1)&&(dot<originalFileName.length())){
return originalFileName.substring(dot);
}
return originalFileName;
}
}
二:controller 调用
@Controller
@RequestMapping("/file")
public class FileUploadController { @RequestMapping("save")
@ResponseBody
public String save(String name,String password,HttpServletRequest request,HttpServletResponse response){
System.out.println("name-----------------------"+name);
System.out.println("password-----------------------"+password);return FileUploadUtils.tranferFile(request, "/upload");
}
}
三:jsp页面代码
<h2>文件上传</h2>
<form name="upload" action="/file/save" enctype="multipart/form-data" method="post">
姓名:<input name="name" type="text">
密码:<input name="password" type="text">
文件:<input type="file" name="thefile" />
<input type="submit" value="上传文件" />
</form>
四:springmvc配置文件中, 配置文件上传视图支持
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="20000000"></property>
</bean>
至此,一个完整的springmvc文件上传工具完毕。
spring mvc 文件上传工具类的更多相关文章
- spring boot 文件上传工具类(bug 已修改)
以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...
- Spring MVC 笔记 —— Spring MVC 文件上传
文件上传 配置MultipartResolver <bean id="multipartResolver" class="org.springframework.w ...
- 【Java Web开发学习】Spring MVC文件上传
[Java Web开发学习]Spring MVC文件上传 转载:https://www.cnblogs.com/yangchongxing/p/9290489.html 文件上传有两种实现方式,都比较 ...
- 文件上传工具类 UploadUtil.java
package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- FastDFS 文件上传工具类
FastDFS文件上传工具类 import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; imp ...
- Spring MVC文件上传教程 commons-io/commons-uploadfile
Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...
- Spring mvc文件上传实现
Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...
- Spring mvc 文件上传到文件夹(转载+心得)
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- spring mvc 文件上传 ajax 异步上传
异常代码: 1.the request doesn't contain a multipart/form-data or multipart/mixed stream, content type he ...
随机推荐
- mac navicat premium 使用技巧
快捷键 CMD-I:对象信息 CMD-L:查询日志 CMD-Y:新建查询 SHIFT-CMD-T:数据传输 SHIFT-CMD-C:命令列界面
- jquey中json字符串与json的转换(转)
<!doctype html> <html> <head> <meta charset="utf-8"> <script sr ...
- vmware磁盘空间扩展
往vmware虚拟机中导入数据库或者文件以后经常出现磁盘空间不够用.这个时候就需要扩展一下磁盘的大小. 笔者本来60G,现在想扩展到100G 命令如下 D:\Program Files (x86)\V ...
- 移动端IOS和androi及浏览器js判断[转载]
转载自:http://www.niutifa.com/?p=561 移动端IOS和androi及浏览器js判断: <script type="text/javascript" ...
- CORBA简介
使用.NET开发corba应用 一. 什么是IIOP.NET IIOP.NET 是通过使用基于corba的IIOP支持.NET.javaEE和corba组件实现无缝互操作的技术.如图1.1所示,这种解 ...
- Linux yum源完全配置
一.简介 yum主要功能是更方便的添加/删除/更新RPM包,自动解决包的依赖性问题,便于管理大量系统的更新问题,其理念是使用一个中心仓库(repository)管理一部分甚至一个distributio ...
- Array Division 808D
D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- python基础之Day5
一.基本概念 为什么要有数据: 计算机能够像人一样识别现实生活中的状态是因为计算机事先将数据存到了记忆中 为什么要分类型: 满足现实世界不同状态的需要 二.数据类型(研究定义,作用,常见操作) 1.整 ...
- 无法创建.gitignore文件,提示必须输入文件名称
If you're using Windows it will not let you create a file without a filename in Windows Explorer. It ...
- bootstrap 坑
1. 表格内存出不来,也不报错 .. 值是对的.. 原因是 table 中必须有属性 data-toggle="table" <table id="My ...