springmvc上传多张图片

简介: 在pom.xml中加入      commons-fileupload      commons-fileupload      1.

在pom.xml中加入

  1. <dependency>  
  2.     <groupId>commons-fileupload</groupId>  
  3.     <artifactId>commons-fileupload</artifactId>  
  4.     <version>1.3.1</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>commons-io</groupId>  
  8.     <artifactId>commons-io</artifactId>  
  9.     <version>2.4</version>  
  10. </dependency>  
2.在spring的配置文件中加入

  1. <!-- 上传文件 -->  
  2. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.     <property name="defaultEncoding" value="utf-8"/>  
  4.     <!-- 最大内存大小 -->  
  5.     <property name="maxInMemorySize" value="10240"/>  
  6.     <!-- 最大文件大小,-1为不限制大小 -->  
  7.     <property name="maxUploadSize" value="-1"/>  
  8. </bean>  
3.前台

  1. <body>  
  2. <form action="${basePath}file/upload" method="post" enctype="multipart/form-data">  
  3.     <label>用户名:</label><input type="text" name="name"/><br/>  
  4.     <label>密 码:</label><input type="password" name="password"/><br/>  
  5.     <label>头 像1</label><input type="file" name="file"/><br/>  
  6.     <label>头 像2</label><input type="file" name="file"/><br/>  
  7.     <input type="submit" value="提  交"/>  
  8. </form>  
  9. </body>  
4.后台

@RequestMapping("addSchoolHonor")
public String addSchoolHonor(Model model, @RequestParam(value = "file", required = false) MultipartFile[] file,
TbResource tbResource) throws IllegalStateException, IOException {
TbCategory item = categoryService.selectCaName(tbResource.getCaName());
if (!item.equals(null)) {
TbResource tb = new TbResource();
tb.setCaId(item.getCaId());
tb.setCaName(tbResource.getCaName());
if(!tbResource.getCaName().equals("校园风光")) {
tb.setReContent(tbResource.getReContent());
}
for (MultipartFile mf : file) {
if (!mf.isEmpty()) {
String path = session.getServletContext().getRealPath("/static/uploadimg");//‘’/static/uploadimg‘’是自己webContent下的包
String fileName = mf.getOriginalFilename();
fileName = UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);// uuid+文件扩展名避免重名,中文名等问题
File uploadFile = new File(path, fileName);
mf.transferTo(uploadFile);
tb.setReTitle(fileName);
resourceService.insert(tb);
}
}
model.addAttribute("message", "添加成功");
} else {
model.addAttribute("message", "不存在该类别,添加失败");
}
return "admin/general/addschoolhonor";
}

5.返回前台显示时

<tr th:each="general,generalStart:${pagemsg.lists}">
                    <td><img alt="无图片" width="100px;" height="80px;" th:src="@{/static/uploadimg/{picture}(picture=${general.reTitle})}" /></td>
                    <td th:if="${sign}=='1'"><a th:href="@{/general/deleteScenery?(id=${general.reId})}" onclick="return confirm('确定要删除吗')">Delete</a></td>
                </tr>

相关文章
|
26天前
|
SQL Java 应用服务中间件
使用Servlet上传多张图片——访问提示
使用Servlet上传多张图片——访问提示
10 0
|
26天前
使用Servlet上传多张图片——前台页面层(Index.jsp)
使用Servlet上传多张图片——前台页面层(Index.jsp)
12 0
|
2月前
element上传多张图片
element上传多张图片
|
5月前
|
JavaScript
jQuery动态拼接多张图片并且获取每张图片名称
jQuery动态拼接多张图片并且获取每张图片名称
26 1
uniapp上传多张图片-带删除按钮查看大图效果demo(整理)
uniapp上传多张图片-带删除按钮查看大图效果demo(整理)
|
9月前
|
Java Linux API
SpringBoot 实现 PDF 添加水印有哪些方案
PDF(Portable Document Format,便携式文档格式)是一种流行的文件格式,它可以在多个操作系统和应用程序中进行查看和打印。在某些情况下,我们需要对 PDF 文件添加水印,以使其更具有辨识度或者保护其版权。本文将介绍如何使用 Spring Boot 来实现 PDF 添加水印的方式。
178 0
|
11月前
|
小程序 前端开发 JavaScript
小程序上传多张图片到springboot后台,返回可供访问的图片链接
小程序上传多张图片到springboot后台,返回可供访问的图片链接
11626 0
实现手动上传表单数据+图片文件
在很多项目中都会有上传数据+图片的需求,我最近在项目中负责活动发布的板块,需要几个表单数据加两个图片和一个图片数组,我看到产品需求后头就很大,我之前没有做过相关的业务,所以这几天一直在尝试,看到接口文档我内心已经崩了。
116 1
实现手动上传表单数据+图片文件
|
JavaScript
php-单张图片、多张图片、视频上传
php-单张图片、多张图片、视频上传
200 0
php-单张图片、多张图片、视频上传
|
Python
【图片操作】批量生成缩略图
在我们日常生活中,缩略图很大程度减少了我们内存的使用。如果我们看一张图片就必须加载完成后才能看,那么我们就会发现很多应用都变慢了很多,而且流量也消耗的很快。今天我们就来看看Python生成缩略图的操作。
276 0