spring boot thymeleaf 图片上传web项目根目录步骤

form方式上传:

//html:
<form enctype="multipart/form-data" method="post" action="/sell/imageUpload">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> </button>
    <h4 class="modal-title" id="myModalLabel">Edit goods information</h4>
</div>
<div class="modal-body">
    <div class="input-group">
        <label class="col-lg-4">name:</label>
        <input class="col-lg-8" id="edit_name" value="${goods.name}" name="name"/>
    </div>
<div class="input-group">
        <label class="col-lg-4">code:</label>
        <input class="col-lg-8" id="edit_sn" name="sn" value="${goods.sn}" />
    </div>
<div class="input-group">
        <label class="col-lg-4">weight:</label>
        <input class="col-lg-8" id="edit_weight" name="weight" value="${goods.weight}" />
    </div>
<div class="input-group">
        <label class="col-lg-4">marketPrice:</label>
        <input class="col-lg-8" id="edit_marketPrice" name="marketPrice" value="${goods.marketPrice}" />
    </div>
<div class="input-group">
        <label class="col-lg-4">shopPrice:</label>
        <input class="col-lg-8" id="edit_shopPrice" name="shopPrice" value="${goods.shopPrice}" />
    </div>
<div class="input-group">
        <label class="col-lg-4">unit:</label>
        <input class="col-lg-8" id="edit_unit" name="unit" value="${goods.unit}" />
    </div>
<div class="input-group">
        <label class="col-lg-4">number:</label>
        <input class="col-lg-8" id="edit_number" name="number" value="${goods.number}" />
    </div>
    <div class="input-group">
        <label class="col-lg-4">detail:</label>
        <textarea class="col-lg-8" id="edit_detail" name="detail" value="${goods.detail}" />
    </div>
<div class="input-group">
   <!--<form enctype="multipart/form-data" method="post" action="/sell/imageUpload">
       <input  ype="hidden" id="edit_goods_sn" name="sn"  value="${goods.sn}" />-->
       image<input type="file" id="edit_image" name="file"/>
       <input type="submit" value="upload"/>
   <!--</form>-->
    </div>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">close</button> 
    <input type="submit" class="btn btn-primary" id="edit_save" value="submit">提交更改</input>
</div>
</form>


//controller

  @RequestMapping(value = "/save",method = RequestMethod.POST)
   public String saveGoodsPage(@RequestParam(value = "id",required=false) String id,@RequestParam(value = "name",required=false) String name,@RequestParam(value = "sn",required=false) String sn,
                               @RequestParam(value = "number",required=false) String number,@RequestParam(value = "weight",required=false) String weight,
                               @RequestParam(value = "marketPrice",required=false) String marketPrice,@RequestParam(value = "shopPrice",required=false) String shopPrice,
                               @RequestParam(value = "unit",required=false) String unit, @RequestParam(value = "detail",required=false) String detail,@RequestParam (value="file")MultipartFile file  ) {

    if (!file.isEmpty()) {
        try {
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream(new File("src/main/resources/static/images/product/" + sn + ".jpg")));//保存图片到目录下
            out.write(file.getBytes());
            out.flush();
            out.close();
            String filename = "\\/images\\/product\\/" + sn + ".jpg";
            /*user.setTupian(filename);
            //userRepository.save(user);//增加用户*/
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "upload error," + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "upload error" + e.getMessage();
        }
    }
        //...其他操作
 }

猜你喜欢

转载自blog.csdn.net/fengcai0123/article/details/79693562
今日推荐