文件上传之MultipartFile使用
一、单/多文件上传使用例子:
工程路径如下
-src
|--main.java
--controller
--service
|--config
--ImageStorageProperties.java
--WebAppConfig.java
|--resource
|--config
--image-storage.properties
1.controller层
@CrossOrigin("*")
@RequestMapping(value = "/putImages",method = RequestMethod.POST, headers = "Accept=application/json")
public String putImages(@RequestBody MultipartFile[] files, HttpServletRequest request) {
return putImgService.putImages(files,request);
}
2.service层
/**
* 图片单/多张上传
* @param files 文件
* @return
*/
public String putImages(MultipartFile[] files, HttpServletRequest request) {
String imgName = "";
if (files == null || files.length == 0) {
return "图片为空";
}
//遍历并保存文件
for (MultipartFile file : files) {
// 图片传到服务器
imgName=imgUpload(file);
if ("".equals(imgName)) {
return "图片上传失败";
}
}
return "图片上传成功";
} /**
* 图片传到服务器
* @param file
* @return 图片路径
*/
public String imgUpload(MultipartFile file) {
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
fileName = UUID.randomUUID() + suffixName;
// 图片存储地址,例如"E:/imagesServer/"
String parent = imageStorageProperties.getStoreUrl();
String imgName = "";
try {
File targetFile = new File(parent, fileName);
// 创建文件夹
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
// 将上传文件存储到服务器中
file.transferTo(targetFile);
// 背景图片地址
imgName = targetFile.getName(); // 图片显示地址,例如"http://localhost:8080/imgFiles/" + imgName
imgName = imageStorageProperties.getHttpUrl() + imgName;
System.out.println(imgName);
} catch (IOException e) {
e.printStackTrace();
}
return imgName;
}
3.文件存储路径配置
1)image-storage.properties配置文件如下
image.httpUrl=http://localhost:8080/imgFiles/
image.storeUrl=E:/imagesServer/
2)ImageStorageProperties.java配置类如下
@ConfigurationProperties(prefix = "image", ignoreUnknownFields = false)
@PropertySource(value = {"classpath:config/image-storage.properties"},encoding="utf-8")
public class ImageStorageProperties { private String httpUrl;
private String storeUrl; public String getHttpUrl() {
return httpUrl;
} public void setHttpUrl(String httpUrl) {
this.httpUrl = httpUrl;
} public String getStoreUrl() {
return storeUrl;
} public void setStoreUrl(String storeUrl) {
this.storeUrl = storeUrl;
}
}
4.拦截器配置
WebAppConfig.java配置如下
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter { @Autowired
private ImageStorageProperties imageStorageProperties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/imgFiles/**").addResourceLocations("file:"+imageStorageProperties.getStoreUrl());
super.addResourceHandlers(registry);
}
}
文件上传之MultipartFile使用的更多相关文章
- 文件上传之 MultipartFile
利用MultipartFile(组件)实现文件上传 在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的Mult ...
- 文件上传api——MultipartFile
MultipartFile 方法总结 byte[] getBytes() 返回文件的内容作为一个字节数组. String getContentType() 返回文件的内容类型. InputStr ...
- MultipartFile 多文件上传的应用
公司的项目很多地方要用到文件上传,以前的上传主要是用apache的fileupload ,使用的感受并不太好.今天试了试spring的MultipartFile,感觉还不错,封装的比较简洁. 当然,中 ...
- springmvc图片文件上传接口
springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller; import java.awt.image.Buffer ...
- SpringMVC文件上传下载
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...
- Spring MVC 文件上传 & 文件下载
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: pom.xml WebConfig.java index.jsp upload.jsp FileUploadCon ...
- Springboot文件上传代码笔记
1.在src下创建filter包,包内Class名UploadFilter package com.gd.filter; import org.apache.catalina.servlet4prev ...
- javaweb简单的实现文件上传
java代码: // @RequestMapping(value = "/upload.do", method = RequestMethod.POST) @RequestMapp ...
- SpringMVC国际化与文件上传
点击阅读上一章 其实SpringMVC中的页面国际化与上一章的验证国际化基本一致. 1.对页面进行国际化 1)首先我们对Spring配置文件中添加国际化bean配置 <!-- 注册国际化信息,必 ...
随机推荐
- 使用AutoFac组织多项目应用程序
较复杂的应用程序都是由多个项目组织成的,项目可以划分成程序集(Assemblies)和宿主(Hosts),也就是应用程序的入口. Assemblies 通常是常见的类库项目,包括可以重用的功 ...
- vs2017调试源代码
最近刚入职 ,带我得导师发给我一堆项目,什么云端和医院端,各种wcf服务.window服务和一些公共类库来回调用.搞得是迷迷糊糊,晕头转向.反正是一脸大萌比... 不过经过几个日日夜夜得不停奋战,大致 ...
- css 相对单位rem详解
CSS3新增了一个相对单位rem(root em,根em),这个单位引起了广泛关注.这个单位与em有什么区别呢?区别在于使用rem为元素设定字体大小时,仍然是相对大小,但相对的只是HTML根元素. ...
- libevent2笔记(linux、windows、android的编译)
0. 前言 我使用的版本是libevent-2.0.21-stable.高级的应用还是得看官网文档http://www.wangafu.net/~nickm/libevent-2.0/doxygen/ ...
- exim CVE-2017-16943 uaf漏洞分析
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 这是最近爆出来的 exim 的一个 uaf 漏洞,可以进行远程代码 ...
- Pwn with File结构体(一)
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 利用 FILE 结构体进行攻击,在现在的 ctf 比赛中也经常出现 ...
- onInterceptTouchEvent与onTouchEvent默认返回值
其中Layout里的onInterceptTouchEvent默认返回值是false,这样touch事件会传递到View控件,Layout里的onTouch默认返回值是false, View里的onT ...
- windows 删除删除不掉的文件
DEL /F /A /Q \\?\%1RD /S /Q \\?\%1 windows下删除删除不掉的文件: 1.打开记事本,把上面的命令复制进去 2.保存,后缀名改为.bat,ok 3.把想要删除的文 ...
- justreq测试接口配置服务
特性 自动缓存每一次接口请求,当测试服务器宕机时,依然可以从容开发 接口替身服务,当后台GG们还没开发好接口时,可以用json.txt等替代 独有jrs脚本,仿php,可以定制更灵活的接口替身,甚至可 ...
- 10.Spring——框架的AOP
1.Spring 框架的 AOP 2.Spring 中基于 AOP 的 XML架构 3.Spring 中基于 AOP 的 @AspectJ 1.Spring 框架的 AOP Spring 框架的一个关 ...