`
johnny15963
  • 浏览: 16256 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

multipart-form-data遍历

阅读更多
map = new HashMap();		
Enumeration en = getMultipartRequest().getFileNames();
while(en.hasMoreElements()){
	String fileName = (String) en.nextElement();
	File file = multiRequest.getFile(fileName);
	map.put(fileName, file);
}	

en = getMultipartRequest().getParameterNames();
while(en.hasMoreElements()){
	String paraName = (String) en.nextElement();
	String[] values = multiRequest.getParameterValues(paraName);
				
	if(values == null || values.length == 0){
		map.put(paraName, null);
	}else if(values.length == 1){
		map.put(paraName, values[0]);
	}else {
		map.put(paraName, values);
	}
				
}


File f1 = getMultipartRequest().getFile("file1"); // name attribute in
													// the form
String filename1 = null;
byte contents1[] = null;
if (f1 != null) {
	filename1 = f1.getName();

	InputStream is1 = null;
	try {
		is1 = new FileInputStream(f1.getCanonicalPath());
		contents1 = IOHelper.getBytes(is1);
	} finally {
		if (is1 != null) {
			is1.close();
		}
	}
}


IOHelper.class
 public static byte[] getBytes(InputStream is)
        throws IOException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c;
        while((c = is.read()) != -1) 
            baos.write(c);
        return baos.toByteArray();
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics