Java - spring mvc 上传图片模块实现
2016-09-08 16:19
309 查看
问题导读:
在spring mvc 框架中实现文件上传功能(抽取主要代码)
解决方案:
在spring mvc 框架中实现文件上传功能(抽取主要代码)
解决方案:
需要的jar :
https://yunpan.cn/cMCb4F5vYkJb9 访问密码 b84eV层实现:
<form role="form" id="addform" method="post" action="/websiteDemo/admin/addGoods.do" class="form-horizontal" style="padding-top:50px;padding-bottom:50px" enctype="multipart/form-data" > <div class="form-group"> <label class="control-label col-sm-2" for="name">商品名称:</label> <div class="col-sm-4"><input type="text" class="form-control" id="name" name="name"></div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="unit">计量单位:</label> <div class="col-sm-4"><input type="text" class="form-control" id="unit" name="unit"></div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="price">单价:</label> <div class="col-sm-4"><input type="text" class="form-control" id="price" name="price"></div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="main_gd_ctg">大分类:</label> <div class="col-sm-4"><input type="text" class="form-control" id="main_gd_ctg" name="main_gd_ctg"></div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="sub_gd_ctg">小分类:</label> <div class="col-sm-4"><input type="text" class="form-control" id="sub_gd_ctg" name="sub_gd_ctg"></div> </div> <div class="form-group"> <label class="control-label col-sm-2 "for="total">库存数量:</label> <div class="col-sm-4"><input type="text" class="form-control" id="total" name="total"></div> </div> <div class="form-group"> <label class="control-label col-sm-2 "for="description">商品描述:</label> <div class="col-sm-4"><textarea class="form-control" rows="3" id="description" name="description"></textarea></div> </div> <div class="form-group"> <label class="control-label col-sm-2 "for="file">上传文件:</label> <div class="col-sm-4"><input type="file" id="file" name="file"></div> </div> <div style="height:50px;"></div> <div class="col-md-offset-2"> <input type="reset" class="btn btn-primary col-md-1" value="取消"> <input type="submit" id="addGoods" class="btn btn-primary col-md-offset-1 col-md-1" value="添加"> </div> </form>
C层实现:
package com.sau.ec.controller; import java.io.File; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import com.sau.ec.bean.GoodsVo; import com.sau.ec.service.GoodsService; @Controller @RequestMapping("/admin") public class GoodsController { @Autowired private GoodsService gService; @RequestMapping(value="/addGoods.do",method=RequestMethod.POST) private String fildUpload(GoodsVo goods ,@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request)throws Exception{ //获得物理路径webapp所在路径 String pathRoot = request.getSession().getServletContext().getRealPath(""); String path=""; if(!file.isEmpty()){ //生成uuid作为文件名称 String uuid = UUID.randomUUID().toString().replaceAll("-",""); goods.setImages(uuid); //将uuid 存到数据库中,代表图片的名字 //goods.setImages(uuid); //获得文件类型(可以判断如果不是图片,禁止上传) String contentType=file.getContentType(); //获得文件后缀名称 String imageName=contentType.substring(contentType.indexOf("/")+1); path="/pic/"+uuid+"."+imageName; file.transferTo(new File(pathRoot+path)); } /* * 将商品添加到数据库 */ gService.addGoods(goods); return "admin/common"; } }
截图:
mysql> select * from goods; +----+------------------------+-----------------------------------------------------------------------------------+------+-------+-------------+------------+-------+----------------------------------+ | id | name | description | unit | price | main_gd_ctg | sub_gd_ctg | total | images | +----+------------------------+-----------------------------------------------------------------------------------+------+-------+-------------+------------+-------+----------------------------------+ | 1 | HAN时尚防紫外线太阳镜 | HAN时尚防紫外线太阳镜HD59303-S01 银框渐进黑灰片 | 支 | 59 | 男款 | 太阳镜 | 315 | e7111215aacc4fa19be3a91722f981b5 | | 2 | 吸盘式双杆不锈钢毛巾架 | 【买就送20只垃圾袋】【免钉免钻,掉落包赔】吸盘式双杆不锈钢毛巾架(每个ID限购5件) | 支 | 16 | 室内 | 居家 | 156 | c85b6d4b31dd4554a05a264781081423 | +----+------------------------+-----------------------------------------------------------------------------------+------+-------+-------------+------------+-------+----------------------------------+ 2 rows in set
相关文章推荐
- JAVA技术实现上传图片水印效果
- 图片实现上传的java类
- java struts中实现上传多张图片代码
- 11级_Java_曹建波 9.13 Tcp实现上传文件和图片
- 在Java中实现TCP协议编程续之实现文本和图片的上传代码
- JAVA实现图片下载至本地,处理后,再上传FTP
- JAVA实现图片下载至缓存处理后,再上传FTP
- Spring MVC上传图片,Java二进制图片写入数据库,生成略缩图
- Spring MVC 实现图片上传功能
- java图片上传后台实现以及删除时删除目录
- java实现图片上传
- java实现图片上传
- java web 实现图片上传前的预览
- 强大的java编程实现邮件发送功能,支持内容,上传多个图片和多个附件
- JAVA实现图片上传自定义等比尺寸
- java 实现 图片上传
- java模拟post方式实现图片上传
- java模拟post方式提交表单实现图片上传
- xheditor上传图片的java实现
- java实现上传图片进行切割