欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java多种文件上传方式总结

程序员文章站 2024-02-19 11:31:46
...

请求类型

Java多种文件上传方式总结

单文件上传

Java多种文件上传方式总结

    //上传单个文件
    @ResponseBody
    @RequestMapping(value = "uploadfile", produces = "multipart/form-data;charset=UTF-8")
    public String uploadfiles(HttpServletRequest request){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartRequest.getFile("file");
            if (multipartFile == null) {
                System.out.println("multipartFile == null");
            }
            String filename = multipartRequest.getFile("file").getOriginalFilename();

            String tmpFile = request.getSession().getServletContext().getRealPath("/") + UUID.randomUUID().toString().replace("-", "") + ".apk";
            File targetFile = new File(tmpFile);
            AppEntity appEntity = new AppEntity();
            if (null != multipartFile) {
                //保存文件
                try {
                    multipartFile.transferTo(targetFile);
                } catch (IllegalStateException e) {
                    logger.error("multipartFile.transferTo, IllegalStateException", e);
                } catch (IOException e) {
                    logger.error("multipartFile.transferTo, IOException", e);
                }
            }
        }

        return "";
    }

多文件上传

多文件单字段上传

Java多种文件上传方式总结

    @ResponseBody
    @RequestMapping(value = "uploadfiles", produces = "multipart/form-data;charset=UTF-8")
    public String uploadfile(MultipartHttpServletRequest request){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            //上传多个文件,一个字段中包含多个文件
            String projectPath = request.getSession().getServletContext().getRealPath("/");
            MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
            List<MultipartFile> fileSet = new LinkedList<>();
            for(Map.Entry<String, List<MultipartFile>> temp : multiFileMap.entrySet()){
                fileSet = temp.getValue();
            }

            for(MultipartFile temp : fileSet){
                File file2 = new File(projectPath+temp.getOriginalFilename());
                try {
                    temp.transferTo(file2);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return "";
    }

多文件多字段上传

Java多种文件上传方式总结

    @ResponseBody
    @RequestMapping(value = "uploadfiles", produces = "multipart/form-data;charset=UTF-8")
    public String uploadfile(MultipartHttpServletRequest request){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        if (multipartResolver.isMultipart(request)) {
            //上传多个文件,每个字段一个文件
            Iterator<String> fileNames = request.getFileNames();
            while (fileNames.hasNext()){
                // 取得上传文件
                String uploadName = fileNames.next();
                MultipartFile file = request.getFile(uploadName);
                if (file != null) {
                    String projectPath = request.getSession().getServletContext().getRealPath("/");
                    String originalFilename = file.getOriginalFilename();
                    String tmpFile = projectPath + System.currentTimeMillis() + "_" + originalFilename;
                    File targetFile = new File(tmpFile);
                    try {
                        file.transferTo(targetFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return "";
    }

over!