• SpringBoot图片上传(二)


    需求简介:做新增的时候,需要上传图片。(⊙o⊙)…这需求描述也太简单了吧,限制文件大小60*60 512kb ,第一次做,记录一下嗷,废话就不啰嗦了 上代码

    代码:

     

    //html代码
    <div class="col-md-5">
    <input type="file" value="上传图标" id="fileupload" th:onchange="|fileUpload()|" style="display:none"/>
      //此src是 上传的小图标图片
    <img src="D:/img/shangchuan.png" alt="" th:onclick="|doUpload()|" id="imageId" style="margin-top: 10px;"/>
    <input type="hidden" id="serviceIcon" name="serviceIcon" class="form-control"/>
    </div>
    //js代码 <script type="text/javascript" th:inline="javascript" xmlns:th="http://www.w3.org/1999/xhtml">
    function doUpload(){
    $("#fileupload").trigger("click");
    }
       //文件上传
    function fileUpload(file){
    debugger;
    var myform = new FormData();
    myform.append('file', $("#fileupload")[0].files[0]);
    var max_size = 512;// 300k
    var tmpFile = $("#fileupload").val();
    var imgtype = tmpFile.toLowerCase().split('.');
    if ((imgtype[1]) != "jpg") {
    layer.msg("图片只支持jpg格式");
    tmpFile.value = "";
    return false;
    } else {
    $.ajax({
    type:"POST",
    url: rootPath + "/serv/service/updatefile",
    data:myform,
    cache: false,
    contentType: false,
    processData: false,
    dataType: "json",//返回的数据格式
    async: false,
    success: function (data) {
    debugger;
    if(data==="1" ){
    layer.msg("上传失败,图片内存过大");
    }else if(data=="2"){
    layer.msg("上传失败,图片内存过大");
    }else{
    var path="/oh-manager/"+data;
                     //取代原来的上传图片的小图标 展示上传后的图片
    $("#imageId").attr("src",path);
    $("#serviceIcon").val(path);
    }
    },
    error: function (jqXHR, textStatus, errorThrown) {

    }
    });
    }
         return false;
    }
    </script>

    //后台java代码: @RequestMapping(value="/updatefile")
    @ResponseBody
    public String updatefile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
    String contentType = file.getContentType();
    String fileName = file.getOriginalFilename();
       try {
    File targetFile = new File("D:/");
    if(!targetFile.exists()){
    targetFile.mkdirs();
    }
    FileOutputStream out = new FileOutputStream("D:/temp/"+fileName);
    out.write(file.getBytes());
    File fileRoute = new File("D:/temp/"+fileName);
    BufferedImage sourceImg =ImageIO.read(new FileInputStream(fileRoute));
    Long size = file.getSize() / 1024; // 图片大小
    int wid=sourceImg.getWidth(); // 源图宽度
    int hei=sourceImg.getHeight(); // 源图高度
    if(size>512){
    fileName="1";
    }
    if(wid>60 || hei>60){
    fileName="2";
    }
    out.flush();
    out.close();
    } catch (Exception e) {
    // TODO: handle exception
    }
    long length=file.getSize();
    //返回json
    return fileName; //fileName就是上边ajax的data
    } //配置文件constant.properties配置文件路径 web.upload-path=D:/temp/
    spring.mvc.static-path-pattern=/**
    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,
    classpath:/static/,classpath:/public/,file:${web.upload-path}
  • 相关阅读:
    docker2核 elasticsearch卡死
    spring cloud config
    App网关Zuul
    spring Ribon
    spring Feign声明式服务消费(Ribbon Hystrix )
    spring Hystrix
    spring cloud 整体介绍
    springbean 生命周期
    xml六种解析方式
    jdk8中的forEach使用return执行下一次遍历
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/9778858.html
Copyright © 2020-2023  润新知