• 多文件上传demo


    @ApiOperation(value = "批量上传", notes = "批量上传", httpMethod = "POST")
        @PostMapping(value = "/upload")
        public void upload(HttpServletRequest request) {
            MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest)request;
            String savePath="E:/test/";
    
            //保证目录存在
            File dir = new File(savePath);
            if (!dir.isDirectory()) {
                dir.mkdirs();
            }
    
            Iterator<String> it = multipartRequest.getFileNames();
            while (it.hasNext()) {
                MultipartFile multipartFile = multipartRequest.getFile(it.next());
                if (multipartFile != null) {
                    String originName = multipartFile.getOriginalFilename();
                    int subIdx = originName.lastIndexOf(".");
                    String suffix = originName.substring(subIdx);//文件后缀
                    File file;
                    String showName;
                    while (true) {
                        showName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;//文件名称
                        file = new File(savePath + showName);
                        if (!file.exists()) {
                            break;
                        }
                    }
                    byte[] buffer = new byte[1024];
                    try (OutputStream os = new FileOutputStream(file);
                         InputStream is = multipartFile.getInputStream()){
                        while (is.read(buffer) != -1) {
                            os.write(buffer);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
  • 相关阅读:
    java中的“指针”
    UEditor1.4.3.3编辑器漏洞
    csrf攻击实例
    shiro java 反序列漏洞复现
    渗透面试问题
    了解 OWASP TOP 10
    网络基础知识回顾
    cs(cobalt strike)的使用
    解决docker-valhub漏洞环境下载慢的问题
    Vulhub漏洞CVE-2017-10271复现
  • 原文地址:https://www.cnblogs.com/tinyj/p/9798712.html
Copyright © 2020-2023  润新知