从request中获取文件流的两种方式

方法一: 从HttpServletRequest中获取文件信息

参考文档:https://blog.csdn.net/java_newstar/article/details/78053891
Collection<Part> parts = req.getParts();
for (Iterator<Part> iterator = parts.iterator(); iterator.hasNext();) {
    Part part = iterator.next();  
    System.out.println("-----类型名称------->"+part.getName());
    System.out.println("-----类型------->"+part.getContentType());
    System.out.println("-----提交的类型名称------->"+part.getSubmittedFileName());
    System.out.println("----流-------->"+part.getInputStream());
}

方法二: 从MultipartHttpServletRequest中获取文件信息


//将当前上下文初始化给  CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                    request.getSession().getServletContext());
// 判断是否是多数据段提交格式
 if (multipartResolver.isMultipart(request)) {
         MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
         Iterator<String> iter = multiRequest.getFileNames();
         logger.info("iter.hasNext(): "+iter.hasNext());
         Integer fileCount = 0;
         while (iter.hasNext()) {
                MultipartFile multipartFile = multiRequest.getFile(iter.next());
                String fileName = multipartFile.getOriginalFilename();
                logger.info("upload filename: " + fileName );
                if(fileName == null || fileName.trim().equals("")){
                      continue;
                }
                //20170207 针对IE环境下filename是整个文件路径的情况而做以下处理
                Integer index = fileName.lastIndexOf("\\");
                String newStr = "";
                 if(index>-1){
                       newStr = fileName.substring(index+1);
                  }else{
                       newStr = fileName;
                  }
                 if(!newStr.equals("")){
                        fileName = newStr;
                  }
                  logger.info("new filename: " + fileName );

                  if (multipartFile != null) {
                        HashMap<String,Object> result =  DispatchInterfaceUtil.uploadInputStream(multipartFile.getInputStream(),multipartFile.getSize(),fileName);
                  }
        }
}






阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭