问题导读:
在spring mvc 框架中实现文件上传功能(抽取主要代码)
解决方案:
需要的jar :
- https://yunpan.cn/cMCb4F5vYkJb9 访问密码 b84e
V层实现:
<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