springboot在提交表单的时候同时提交图片等文件,并且在页面显示图片时访问物理磁盘上的图片.(tomcat映射虚拟路径给本地磁盘)

在提交表单的时候同时提交图片等文件,这里以图片为例

表单大家都很熟悉了,这里提一下要注意的点
enctype="multipart/form-data"

<form role="form" method="post" action="/seller/product/save" enctype="multipart/form-data">
                                                <div class="form-group">
                                                        <label>名称</label><input type="text" class="form-control" value="${(productInfo.productName)!''}" name="productName" />
                                                </div>
                                                <div class="form-group">
                                                        <label>价格</label><input type="text" class="form-control" value="${(productInfo.productPrice)!''}" name="productPrice"/>
                                                </div>
                                                <div class="form-group">
                                                        <label>库存</label><input type="number" class="form-control" value="${(productInfo.productStock)!''}" name="productStock"/>
                                                </div>
                                                <div class="form-group">
                                                        <label>描述</label><input type="text" class="form-control" value="${(productInfo.productDescription)!''}" name="productDescription"/>
                                                </div>

                                                <div class="form-group">
                                                        <label>图片</label>
                                                        <br>
                                                        <#if productInfo?? && productInfo.productIcon??>
                                                                <img height="100" width="100" src="${(productInfo.productIcon)!''}" alt="图片加载失败"/>
                                                        </#if>
                                                        <input type="file" name="newProductIcon" />
                                                </div>
                                                <div class="form-group">
                                                        <label>商品类目</label>
                                                        <select name="categoryType" class="form-control">
                                                                <#list categoryList as category>
                                                                        <#if (productInfo.categoryType)?? && productInfo.getCategoryType() == category.getCategoryType()>
                                                                                <option value="${category.getCategoryType()}" selected="selected">${category.getCategoryName()}</option>
                                                                        <#else>
                                                                                <option value="${category.getCategoryType()}">${category.getCategoryName()}</option>
                                                                        </#if>

                                                                </#list>
                                                        </select>
                                                </div>
                                                <div class="form-group">
                                                        <label>商品状态</label>
                                                        <select class="form-control" name="productStatus">
                                                                <option value="0" selected="selected">上架</option>
                                                                <option value="1">下架</option>
                                                        </select>
                                                </div>
                                                <input hidden type="text" name="productId" value="${(productInfo.productId)!''}">

                                                <button type="submit" class="btn btn-default">提交</button>
                                        </form>

后端接收的时候
在这里插入图片描述

保存图片,这里要说一下MultipartFile这个类保存图片非常方便,transferTo(目标路径的File)

 @Override
    public String saveIcon(MultipartFile newIcon) {

        String UUIDPictureName = UUID.randomUUID().toString();
        String originalFileName = newIcon.getOriginalFilename();
        //后缀
        String ext = originalFileName.substring(originalFileName.lastIndexOf('.'));
        //保存的时候还是要保存到真实路径的,这里的这个路径有file前缀
        String fileName = UUIDPictureName + ext;
        String iconRealPath = iconPath.substring(iconPath.indexOf(':') + 1) + fileName;
        File icon = new File(iconRealPath);
        try {
            //存入目标路径
            newIcon.transferTo(icon);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //在webConfi中映射了虚拟路径到项目中
        return "/icon/" + fileName;
    }

在页面显示图片时访问物理磁盘上的图片

这里要添加一个虚拟路径映射,路径我放在yml中了,方便后面修改
iconPath = file:D:/icon/
前面的这个file前缀很重要

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{

    @Value("${product.iconPath}")
    private String iconPath;


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/icon/**").addResourceLocations(iconPath);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值