文件上传之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配置 <!-- 注册国际化信息,必 ...
随机推荐
- hexo的next主题个性化教程:打造炫酷网站
看到有些next主题的网站很炫酷,那么是怎么配置的呢?接下来我会讲一讲如何实现一些炫酷的效果 主要有以下32种: 在右上角或者左上角实现fork me on github 添加RSS 添加动态背景 实 ...
- angular2.0---服务Service,使用服务进行数据处理
1.创建服务 打开命令窗口,cd到项目目录下,输入 ng g service myData1 回车 创建服务,如下图所示: 这样就成功创建了服务,此时,可以在项目的app文件夹下生成了两个serv ...
- RPC簡介
RPC 技术原理 RPC ( Remote Procedure Call Protocol,远程过程调用协议 ): 客户端在不知道调用细节的情况下,调用存在于远程计算机上的某个对象,就像调 ...
- Android网络请求库RetrofitUtils
RetrofitUtils GitHub地址,帮忙给个Star 项目介绍 Retrofit+Okhttp辅助类的简单封装,vesion 1.0.X 实现了Get,Post-Form.Post-Json ...
- 润乾报表如何使用Echarts
1. 润乾报表中使用Echarts统计图的步骤 2. 报表中添加echarts2统计图 选中需要设为统计图的单元格,点击 报表-第三方图形 菜单项,或者右键菜单-第三方图形,在图形编 ...
- android 每个块半径不同的扇形图,自定义view
1.首先看效果图 2.自定义PieChartView,继承自View,下边为PieChartView代码 package com.yingjinbao.im.peach.customview; imp ...
- Linux pyenv环境安装
python工作环境管理 pyenv安装: git clone https://github.com/pyenv/pyenv ~/.pyenv echo 'export PYENV_ROOT=&quo ...
- Oracle EBS PO采购订单更新
DECLARE l_result NUMBER; l_progress NUMBER; l_errors PO_API_ERRORS_REC_TYPE; l_chg PO_CHANGES_REC_TY ...
- C#实现ADH815通讯
最近在做自提柜项目,考虑到ADH815电路板在自助售卖行业的通用性.把通讯代码贴出来了. 下载地址
- Windows自带强大的入侵检测工具——Netstat 命令 查询是否中木马
Netstat命令可以帮助我们了解网络的整体使用情况.根据Netstat后面参数的不同,它可以显示不同的网络连接信息.Netstat的参数如图,下面对其中一些参数进行说明.如何检测本机是否有被中木马, ...