zoukankan      html  css  js  c++  java
  • MultipartFile+nio上传文件

    
    

    import java.io.File;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;


    public
    ResponseData upload(@RequestParam(value="file",required=true) MultipartFile file, HttpServletRequest request) throws Exception{ String filePath = null; String fileName = file.getOriginalFilename(); // 获取文件的后缀名 String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1); //检查文件格式是否合法 if(file.getContentType().toLowerCase().contains("image/") && !StringUtils.isBlank(suffixName) && (suffixName.equalsIgnoreCase("BMP") || suffixName.equalsIgnoreCase("JPG") || suffixName.equalsIgnoreCase("JPEG") || suffixName.equalsIgnoreCase("PNG"))) { // 获取登录用户信息 Integer userId = 1; // 设置文件存储路径 filePath = request.getSession().getServletContext().getRealPath("/") + File.separator + userId + "_" + System.currentTimeMillis() + "." + suffixName; byte[] bytes = file.getBytes(); Path path = Paths.get(filePath); //保存在本地 Files.write(path, bytes); //上传到远程文件服务器 //TODO return ResponseData.success(filePath ); } else { return ResponseData.error(-1, "图片格式错误"); } }
  • 相关阅读:
    opencv图片压缩视频并读取
    python常见模块统计
    MySQL索引及优化
    web开发框架之 Tornado
    Tornado项目基本架构
    python闭包以及装饰器
    python语法糖
    python os模块
    TCP中的3次握手和4次挥手
    Python常见的数据类型方法
  • 原文地址:https://www.cnblogs.com/shihaiming/p/9437261.html
Copyright © 2011-2022 走看看