zoukankan      html  css  js  c++  java
  • springboot文件上传步骤,详细,ajax文件上传,formdata封装

    话不多说上代码

    jsp页面:

    <form id="addForm" enctype="multipart/form-data">
        <input type="hidden" name="uId">
        <div class="form-group">
            <label class="control-label">商品名称:</label>
            <input type="text" name="pName" class="form-control">
        </div>
        <div class="form-group">
            <label class="control-label">库存:</label>
            <input type="text" name="pStock" class="form-control">
        </div>
        <div class="form-group">
            <label class="control-label">成本价:</label>
            <input type="text" name="pCost" class="form-control">
        </div>
        <div class="form-group">
            <label class="control-label">商品价格:</label>
            <input type="text" name="pPrice" class="form-control">
        </div>
        <div class="form-group">
            <label class="control-label">商品描述:</label>
            <input type="text" name="pDescription" class="form-control">
        </div>
        <div class="form-group">
            <label class="control-label">商品图片:</label>
            <img width="250" height="150" src='<c:url value="/static/probimg/222.png"></c:url>'>
            <input type="file" name="pPicture" class="form-control">
        </div>
    </form>

    js代码:

    $("#confirmAdd").click(function () {
        var formdata = new FormData($("#addForm")[0]);
        $.ajax({
            type:"POST",
            dataType:"json",
            url:"addProduct",
            data:formdata,
            async:false,
            cache:false,
            contentType:false,
            processData:false,
            success:function(msg){
                if(msg){
                    alert("文件上传")
                }
            }
        })
    })

    控制层代码:思路(将上传的文件存储在本地的盘符,但是当要将本地文件如何映射到jsp页面呢??)

    @RequestMapping("addProduct")
    @ResponseBody
    public Boolean addProduct(HttpServletRequest request, String pName, Integer pStock,
                              BigDecimal pCost, BigDecimal pPrice, String pDescription,@RequestParam("pPicture") MultipartFile pPicture) throws IOException {
        String fileName = UUID.randomUUID()+ pPicture.getOriginalFilename();
        if(!pPicture.isEmpty()){
            byte [] bytes = pPicture.getBytes();
            BufferedOutputStream bufferedOutputStream = new
                    BufferedOutputStream(new FileOutputStream(new File("E:\upload\"+fileName)));
            bufferedOutputStream.write(bytes);
            bufferedOutputStream.close();
        }
        Product product = new Product( null,  pName,  pStock,  pCost,  pPrice,  pDescription,  fileName);
        return productService.add(product);
    }

    但是当要将本地文件如何映射到jsp页面呢??

    package com.mall.han.utils;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    //springboot 5.0 addResourceLocations 类已经弃用 ,可以使用WebMvcConfigurationSupport
    public class WebAppConfigurer extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //其实这里addResourceHandler是映射的一个请求,完全可以不用映射在static目录下, 可以理解为一个@RequestMapping,只不过这个请求是映射到本地的静态资源而已    
        registry.addResourceHandler("/static/probimg/**").addResourceLocations("file:E://upload/");
    }
    }

     这个文件不需要放东西,然后重启服务器,在浏览器下输入该目录会显示本地的文件

            

  • 相关阅读:
    1008 Elevator
    mysql---时间类型详解
    mysql导入导出
    mysql不能启动报error2013错误的解决办法
    mysql总结
    mysql安装图解
    Access连接数据源配置(新手必知)
    eclipse建包的一些细节
    数据库操作(存着用来复制省的每次写)
    (工具类)MD5算法|时间格式转换|字符串转数字
  • 原文地址:https://www.cnblogs.com/han-guang-xue/p/9988157.html
Copyright © 2011-2022 走看看