📄 multipartfilter.java
字号:
if(b>=0&&b<byteBoundary.length&&c==byteBoundary[b]) b++; else { // this is not a boundary if(cr) out.write(13); if(lf) out.write(10); cr=lf=false; if(b>0) out.write(byteBoundary,0,b); b=-1; out.write(c); } } // check partial boundary if((b>0&&b<byteBoundary.length-2)||(b==byteBoundary.length-1)) { if(cr) out.write(13); if(lf) out.write(10); cr=lf=false; out.write(byteBoundary,0,b); b=-1; } // boundary match if(b>0||c==-1) { if(b==byteBoundary.length) lastPart=true; if(state==10) state=-2; break; } // handle CR LF if(cr) out.write(13); if(lf) out.write(10); cr=(c==13); lf=(c==10||state==10); if(state==10) state=-2; } } finally { out.close(); } if (file==null) { bytes = ((ByteArrayOutputStream)out).toByteArray(); params.add(name,bytes); } } // handle request chain.doFilter(new Wrapper(srequest,params),response); } finally { deleteFiles(request); } } private void deleteFiles(ServletRequest request) { ArrayList files = (ArrayList)request.getAttribute(FILES); if (files!=null) { Iterator iter = files.iterator(); while (iter.hasNext()) { File file=(File)iter.next(); try { file.delete(); } catch(Exception e) { _context.log("failed to delete "+file,e); } } } } /* ------------------------------------------------------------ */ private String value(String nameEqualsValue) { String value=nameEqualsValue.substring(nameEqualsValue.indexOf('=')+1).trim(); int i=value.indexOf(';'); if(i>0) value=value.substring(0,i); if(value.startsWith("\"")) { value=value.substring(1,value.indexOf('"',1)); } else { i=value.indexOf(' '); if(i>0) value=value.substring(0,i); } return value; } /* ------------------------------------------------------------------------------- */ /** * @see javax.servlet.Filter#destroy() */ public void destroy() { } private static class Wrapper extends HttpServletRequestWrapper { String encoding="UTF-8"; MultiMap map; /* ------------------------------------------------------------------------------- */ /** Constructor. * @param request */ public Wrapper(HttpServletRequest request, MultiMap map) { super(request); this.map=map; } /* ------------------------------------------------------------------------------- */ /** * @see javax.servlet.ServletRequest#getContentLength() */ public int getContentLength() { return 0; } /* ------------------------------------------------------------------------------- */ /** * @see javax.servlet.ServletRequest#getParameter(java.lang.String) */ public String getParameter(String name) { Object o=map.get(name); if (o instanceof byte[]) { try { String s=new String((byte[])o,encoding); return s; } catch(Exception e) { e.printStackTrace(); } } else if (o instanceof String) return (String)o; else if (o instanceof String[]) { String[] s = (String[])o; return s.length>0 ? s[0] : null; } return null; } /* ------------------------------------------------------------------------------- */ /** * @see javax.servlet.ServletRequest#getParameterMap() */ public Map getParameterMap() { return map; } /* ------------------------------------------------------------------------------- */ /** * @see javax.servlet.ServletRequest#getParameterNames() */ public Enumeration getParameterNames() { return Collections.enumeration(map.keySet()); } /* ------------------------------------------------------------------------------- */ /** * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String) */ public String[] getParameterValues(String name) { List l=map.getValues(name); if (l==null || l.size()==0) return new String[0]; String[] v = new String[l.size()]; for (int i=0;i<l.size();i++) { Object o=l.get(i); if (o instanceof byte[]) { try { v[i]=new String((byte[])o,encoding); } catch(Exception e) { e.printStackTrace(); } } else if (o instanceof String) v[i]=(String)o; } return v; } /* ------------------------------------------------------------------------------- */ /** * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String) */ public void setCharacterEncoding(String enc) throws UnsupportedEncodingException { encoding=enc; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -