日期:2014-05-17 浏览次数:20787 次
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wk.util.StringUtil; @SuppressWarnings("serial") public class FileUploadServlet extends HttpServlet { public static final byte CR = 0x0D; public static final byte LF = 0x0A; public static final byte DASH = 0x2D; protected static final int REMAIN_SIZE = 64; protected static final int DEFAULT_BUFSIZE = 4096; protected static final int READ_BUFSIZE = DEFAULT_BUFSIZE - REMAIN_SIZE; protected static final byte[] HEADER_SEPARATOR = {CR, LF, CR, LF}; protected static final byte[] FIELD_SEPARATOR = {CR, LF}; protected static final byte[] STREAM_TERMINATOR = {DASH, DASH}; protected static final byte[] BOUNDARY_PREFIX = {CR, LF, DASH, DASH}; private static String upload_base_path; protected boolean equals(byte[] b1, int start1, byte[] b2, int start2, int length) { if (b1.length - start1 < length) { return false; } if (b2.length - start2 < length) { return false; } for (int i = 0; i < length; i++) { if (b1[start1 + i] != b2[start2 + i]) { return false; } } return true; } protected int search(byte[] b, int start, String str) { return search(b, start, str.getBytes()); } protected int search(byte[] b, int start, byte[] s) { int length = s.length; byte first = s[0]; for (int i = start, total = b.length - length; i <= total; i++) { if (b[i] == first && equals(b, i, s, 0, length)) { return i; } } return -1; } protected byte[] merge(byte[]... bs) { if (bs == null) { return null; } int total = 0; for (byte[] b : bs) { total += b.length; } byte[] newb = new byte[total]; int offset = 0; for (byte[] b : bs) { System.arraycopy(b, 0, newb, offset, b.length); offset += b.length; } return newb; } protected int readFully(InputStream in, byte[] buff, int offset, int length) { int total = 0; while (true) { try { int readlen = in.read(buff, offset, length); if (readlen == -1) { return 0; } total += readlen; if (readlen == length) { return total; } offset += readlen; length -= readlen; } catch (IOException ioe) { return -1; } } } protected void afterUploadSuccess(int index, String name, String fileName) { } protected void afterUploadFail(String errcode, String errmsg) { afterUploadFail(0, errcode, errmsg); } protected void afterUploadFail(int index, String errcode, String errmsg) { if (index == 0) { System.out.println(errcode + ":" + errmsg); } else {