java获取form表单里的字段和文件流

        Map map=new HashMap();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            // 文件上传
            // 用stream api处理,提高性能
            // System.err.println("file upload request...");
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator fileItemIterator = upload.getItemIterator(request);
            for (; fileItemIterator.hasNext();) {
                FileItemStream item = fileItemIterator.next();
                String name = item.getFieldName();
                System.out.println(name);
                if (item.isFormField()) {
                    InputStream stream = item.openStream();
                    // 普通域
                    String value = Streams.asString(stream, "UTF-8");
                    if (map.containsKey(name)) {
                        String valueto = (String) map.get(name);
                        valueto += "," + value;
                        map.put(name, valueto);
                    } else {
                        map.put(name, value);
                    }
                    stream.close();
                } else {
                    // 文件域
                    InputStream in = item.openStream();// 获取文件流
                }
            }
        }

 

posted @ 2018-04-26 09:43  东山磐石西山流云  阅读(4266)  评论(0编辑  收藏  举报