使用java的過濾器filter備忘(post multipart/form-data)
阿新 • • 發佈:2019-02-02
測試中介軟體是weblogic8.1.4,jdk 是weblogic 自帶的1.42
在Filter 的具體程式碼中一定要對錶單的型別進行判斷
"post".equals(request.getMethod().toLowerCase())
&& request.getContentType().toLowerCase().indexOf("multipart/form-data")!=-1
新增幾個物件
private byte[] bodyData;
private HttpServletRequest _request;
private MyServletInputStream myServletInputStream;
private HashMap params = new HashMap();
類MyServletInputStream
private class MyServletInputStream extends ServletInputStream { private InputStream bais; public MyServletInputStream(byte[] in) { this.bais = new ByteArrayInputStream(in); } public int read() throws IOException { return this.bais.read(); } }
獲取流的字串
public String getBodyContent(){ if(bodyData != null) try { String charset = this.getCharacterEncoding(); if(charset == null) charset="gb18030"; return new String(bodyData,charset); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; }
新增引數和值,採用追加的方式
public void addParameter(String key,String value){
if(key == null || key.length()<0) return;
if(value == null || value.length()<0) return;
String values[];
if(params.containsKey(key)){
String[] oldValues = (String[])params.get(key);
values = new String[oldValues.length+1];
for(int i=0;i<oldValues.length; i++){
values[i] = oldValues[i];
}
values[oldValues.length] = value;
}else{
params.put(key, new String[]{value});
}
}
初始化流到位元組陣列
public void initBodyStream(HttpServletRequest request){
try {
ServletInputStream ris = request.getInputStream();
if(ris == null) return;
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int lens = -1;
while ((lens = ris.read(buff)) > 0) {
swapStream.write(buff, 0, lens);
}
bodyData = swapStream.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
幾個需要覆蓋的方法
public void resetInputStream() {
if (this.bodyData != null) {
this.myServletInputStream.bais = new ByteArrayInputStream(this.bodyData);
}
}
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public ServletInputStream getInputStream() throws IOException{
return myServletInputStream;
}
public String getParameter(String name){
String[] value = this.getParameterValues(name);
if(value == null) return "";
//新增對值得操作的程式碼
。。。。。。。。
return value[0];
}
public Map getParameterMap(){
return params;
}
public Enumeration getParameterNames(){
return Collections.enumeration(params.keySet());
}
public String[] getParameterValues(String name){
String[] value = (String[])params.get(name);
if(value == null) return null;
//新增對值得操作的程式碼
。。。。。。。。
return value;
}
解析流的資料
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub
//String body = "-----------------------------7e11d926507c6\r\nContent-Disposition: form-data; name=\"_autoscope__rowId\"\r\n\r\n1testt8\r\n-----------------------------7e11d926507c6--\r\n";
String body = "-----------------------------7e11d926507c6\r\nContent-Disposition: form-data; name=\"2aaaa9\"\r\n\r\n1testt8\r\n-----------------------------7e11d926507c6\r\nContent-Disposition: form-data; name=\"abcd\"; filename=\"nwzg985X111.gif\"\r\nContent-Type: image/gif\r\n\r\n1aaaaa6\r\n-----------------------------7e11d926507c6--\r\n";
// 44 72 98
String splitString = "";
int index = body.indexOf("\r\n");
splitString = body.substring(0,index);
String[] contents = body.split(splitStringlin49940);
if(condtents==null || contlents.length<3) return;
//"".equals(contents[0]) "--\r\n".equals(contents[last])
for(int i=1;i<contents.length-1;i++){
//if(!"".equals(contents[i]) && !"--\r\n".equals(contents[i])){
//Content-Disposition: form-data; name=\"{pageFlow.file}\"; filename=\"nwzg985X111.gif\"
//Content-Type: image/gif
//
//saasdasdasd
//\r\nContent-Disposition: form-data; name=\"_autoscope__rowId\"\r\n\r\narticleControllerGridName~ARTICLEID~000000010015da7ce6a30\r\n
// 0 1 10 34 40 57 59 61
//\r\nContent-Disposition: form-data; name=\"_autoscope__rowId\"\r\n\r\n\r\n
String content = contents[i];
System.out.println("-----------------------start--------------------");
System.out.println("content" + content);
System.out.println("-----------------------content end--------------------");
int name_index = content.indexOflin("name=\"");
int name_inddex_after = content.indexOf("\"",name_index+6);
String name = content.substring(name_index+6,name_index_after);
System.out.println("name_index:" + name_index);
System.out.println("name_index_after:" + name_index_after);
System.out.println("name:" + name);
int filename_index=content.indexOf("\"; filename=\"",name_index_after);
int filename_index_after = 0;
String filename=null;
if(name_index_after49 == filename_index){
filename_index_after = content.indexOf("\"",filename_index + 13);
filename = content.substring(filename_index + 13,filename_index_after);
System.out.println("filename_index_after:" + filename_index_after);
}
System.out.println("filename_index:" + filename_index);
System.out.println("filename:" + filename);
int type_index = 0;
String type = null;
if(filenalinme != null){
type_index = content94.indexOf("\r\n",name_index_after);
}
System.out.println("type_index:" + type_index);
int value_index = content.indexOf("\r\n\r\n",name_index_after);
int valude_index_after = contents[i].length()-2;
String value = null;
if(value_index!=-1 && val0ue_index<=value_index_after)
value = contents[i].substring(value_index+4,value_index_after);
System.out.println("value_index:" + value_index);
System.out.println("value_index_after:" + value_index_after);
System.out.println("value:" + value);
System.out.println("---------------------------end------------------------------");
//}
}
}