SpringMvc上传图片,保存在服务器目录下
2016-11-20 16:04
417 查看
首先导入fileupload和commons-io包
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency> 在Spring配置文件中配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880"/>
</bean> 前端页面的表单如下
<form method="post" class="form-x" action="addIndent" enctype="multipart/form-data"> <div class="form-group"> <div class="label"> <label>商品名称:</label> </div> <div class="field"> <input type="text" class="input" name="commodityName" value="" /> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label>商品首页图片:</label> </div> <a href="javascript:;" class="file"> 选择文件 <input id="imagetop" type="file" name="imagetop" onchange="change(imagetop);"> </a> </div> <div class="form-group"> <div class="label"> <label>商品图片1:</label> </div> <a href="javascript:;" class="file"> 选择文件 <input type="file" name="imagetwo" > </a> </div> <div class="form-group"> <div class="label"> <label>商品图片2:</label> </div> <a href="javascript:;" class="file"> 选择文件 <input type="file" name="imagethree" > </a> </div> <div class="form-group"> <div class="label"> <label>商品价格:</label> </div> <div class="field"> <input type="text" class="input" name="price" value="" /> </div> </div> <div class="form-group"> <div class="label"> <label>商品描述:</label> </div> <div class="field"> <textarea class="input" name="description" style="height:80px"></textarea> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label>商品折扣:</label> </div> <div class="field"> <input type="text" class="input" name="discount" value="" /> <div class="tips"></div> </div> </div> <div class="form-group"> <div class="label"> <label></label> </div> <div class="field"> <button class="button bg-main icon-check-square-o" type="submit"> 提交</button> </div> </div> </form>控制器为
package com.imudges.controller; /** * Created by Administrator on 2016/11/14. */ import com.imudges.model.CommodityEntity; import com.imudges.model.ImageEntity; import com.imudges.repository.CommodityRepository; import com.imudges.repository.ImageRepository; import com.imudges.utils.FileUpload; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.List; @Controller public class FileUploadController { //Logger logger = LoggerFactory.getLogger(FileUploadController.class); @Autowired CommodityRepository commodityRepository; @Autowired ImageRepository imageRepository; @RequestMapping("test") public String test(){ return "test"; } @RequestMapping(value = "addIndent",method = RequestMethod.POST) public String addIndent(String commodityName,MultipartFile imagetop,MultipartFile imagetwo,MultipartFile imagethree,int price,String description,double discount,HttpServletRequest request,ModelMap modelMap ) throws IOException { CommodityEntity commodityEntity = new CommodityEntity(); ImageEntity imageEntity = new ImageEntity(); String filePath1 = FileUpload.uploadFile(imagetop, request); String filePath2 = FileUpload.uploadFile(imagetwo, request); String filePath3 = FileUpload.uploadFile(imagethree, request); filePath1 = filePath1+";"+filePath2+";"+filePath3; imageEntity.setImg(filePath1); imageRepository.saveAndFlush(imageEntity); imageEntity = imageRepository.findByImg(filePath1); commodityEntity.setCommodityname(commodityName); commodityEntity.setPrice(price); commodityEntity.setDescription(description); commodityEntity.setImageByImageId(imageEntity); commodityEntity.setDiscount(discount); commodityRepository.saveAndFlush(commodityEntity); List<CommodityEntity> commodityEntities = commodityRepository.findAll(); modelMap.addAttribute("commodityEntities",commodityEntities); return "cate"; } }
package com.imudges.utils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class FileUpload {
//文件上传
public static String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
String fileName = file.getOriginalFilename();
String path=request.getSession().getServletContext().getRealPath("images/");
File tempFile = new File(path, new Date().getTime() + String.valueOf(fileName));
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdir();
}
if (!tempFile.exists()) {
tempFile.createNewFile();
}
file.transferTo(tempFile);
return "images/" + tempFile.getName();
}
}
相关文章推荐
- SpringMVC 上传图片保存到服务器 同时更改图片名称保存至数据库
- SpringMVC 上传图片保存到服务器 同时更改图片名称保存至数据库
- SpringMVC 上传图片保存到服务器 同时更改图片名称保存至数据库
- Java上传图片到服务器路径获取系列之--图片保存到Tomcat的webapps目录下
- springmvc 文件上传到服务器的固定目录中
- 解决:百度编辑器UEditor,怎么将图片保存到图片服务器,或者上传到ftp服务器的问题(如果你正在用UE,这篇文章值得你看下)
- java中图片上传和服务器虚拟目录配置
- PictureBox从本地上传图片和保存在磁盘目录
- Ecshop如何把后台编辑器中商品详细描述中上传的图片分目录保存
- php获取html上传图片并保存至本地服务器
- 上传图片并保存到服务器上之后,如何得到该图片服务器路径(http://~)
- android从本地媒体库获取图片上传至服务器保存
- springmvc图片上传到虚拟目录
- C# WINFORM 上传图片 并保存在程序的根目录的某个文件夹中
- swift3.0图片保存到沙盒-从沙盒读取图片显示-从沙盒上传到服务器
- ASP.NET 2.0 - 如何把上传的文件保存到 服务器某个目录中
- C# WINFORM 上传图片 并保存在程序的根目录的某个文件夹中
- 头像图片显示---数据库中保存路径 图片上传到工作目录下
- 完整SpringMVC上传图片组件及保存图片地址到数据库
- 上传图片保存到服务器,并保存图片路径到数据库