import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HttpUtil {
/*
* binnary strut is: 1. [seperater] + 0x0D0A + 2. [header info,i.e.
* Disposition,name,filename...seperate is ;] + 0x0D0A+ 3.
* [Content-Type:xxx] + 0x0D0A + 0x0D0A 4. [Data bytes] + 0x0D0A + >> this
* may be binnary,can not change to string 5. [seperater] + 0x0D0A + 2,3,4
* repeated 6. [seperater] + 0x0D0A
*
*/
/**
* new line key
*/
public static byte[] CRLF = new byte[]{0x0d, 0x0a};
/**
* content key
*/
public static byte[] CRLF2 = new byte[]{0x0d, 0x0a, 0x0d, 0x0a};
/**
* multipart form keywords
*/
public static String MULTI_TYPE = "multipart/form-data";
/**
* normal form keywords
*/
public static String NORMAL_TYPE = "application";
private String[] contentDisposition;
private String[] contentType;
private byte[][] data;
private String[] fieldString;
private String[] filename;
private boolean isMultipartForm = false;
private String[] name;
private int paramCount = 0;
private HttpServletRequest request;
private HttpServletResponse response;
private String[] value;
// read the request and set all fields
/**
* <pre>
* http tools
* </pre>
* @param request request
* @param response response
* @throws ServletException ServletException
* @throws IOException
*/
public HttpUtil(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ct = request.getContentType();
this.request = request;
this.response = response;
if (ct == null) {
this.isMultipartForm = false;
} else {
if (ct.indexOf(HttpUtil.MULTI_TYPE) < 0) {
this.isMultipartForm = false;
} else {
this.initMultipartForm(request, response);
this.isMultipartForm = true;
}
}
}
private byte[] copyBytes(byte[] src, int start, int end) {
byte[] ret = new byte[end - start + 1]; // include the end
for (int i = start; i <= end; i++)
ret[i - start] = src[i];
return ret;
}
private int countMatchs(byte[] src, byte[] find) {
int count = 0;
int pos = 0;
for (int i = 0; i < src.length