通过servlet用以下的类,可以实现,记住html页面的form的enctype="multipart/form-data",否则不行。
class FileUpload {
public FileUpload() {
}
//文件上传部分
final int NONE = 0; //状态码,表示没有特殊操作
final int DATAHEADER = 1; //表示下一行要读到包头信息
final int FIELDDATA = 2; //表示下面要读到表单域的文本
final int FILEDATA = 3; //表示下面要读的是上传文件和二进制数据
String filepath = "";
String fieldname = "";
String fieldvalue = "";
String filename = "";
String boundary = "";
String lastboundary = "";
long fileSize = 0; //文件长度
//容纳表单域的名称/值得哈西表
Hashtable formfields = new Hashtable();
public String getFilename() {
return filename;
}
public long getFileSize() {
return fileSize;
}
public String getFieldValue(String Fieldname) {
if (formfields == null || Fieldname == null) {
return null;
}
return (String) formfields.get(Fieldname);
}
public void setFilePath(String FilePath) {
if (FilePath.substring(FilePath.length() - 1) != "\\") {
FilePath = FilePath + "\\";
}
filepath = FilePath;
}
public String getFilePath() {
return filepath;
}
public void doUpload(HttpServletRequest request) throws IOException {
//请求消息实体的总长度(请求消息中除消息头之外的数据长度)
int totalbytes = request.getContentLength();
//容纳请求消息实体的字节数组
byte b[] = new byte[totalbytes];
//请求消息类型
String contentType = request.getContentType();
//在消息头类型中找到分界符的定义
int pos = contentType.indexOf("boundary=");
if (pos != -1) {
pos += "boundary=".length();
boundary = "--" + contentType.substring(pos);
lastboundary = boundary + "--";
}
int state = NONE; //起始状态为NONE
//得到请求消息的数据输入流
DataInputStream in = new DataInputStream(request.getInputStream());
DataOutputStream fileout = null;
in.readFully(b); //根据长度将消息实体的内容读入字节数组中
in.close();
String reqcontent = new String(b); //从字节数组中得到表示实体的字符串
//从字符串中得到输出缓冲流
BufferedReader reqbuf = new BufferedReader(new StringReader(reqcontent));
//设置循环标志
boolean flag1 = true;
int i = 0;
while (flag1 == true) {
String s = reqbuf.readLine();
if (s == lastboundary || s == null) {
break;
}
switch(state)
{
case NONE:
if (s.startsWith(boundary)) {
//如果读到分界符,则表示下一行一个头信息
state = DATAHEADER;
i += 1;
}
break;
case DATAHEADER:
pos = s.indexOf("filename=");
//县判断这是一个文本表单域的头信息,还是一个上传文件的头信息
if (pos == -1) {
//如果是文本表单域的头信息,解析出表单域的名称
pos = s.indexOf("name=");
pos += "name=".length() + 1; //1表示后面的“占位
s = s.substring(pos);
int l = s.length();
s = s.substring(0, l - 1);
fieldname = s; //表单域的名称放入
state = FIELDDATA; //设置状态,准备读取表单域的值
}
else {
//如果是文件数据的头,先存储这一行,用于在字节数组中定位
String temp = s;
//先解析出文件名
pos = s.indexOf("filename=");
pos += "filename=".length() + 1; ////1表示后面的“占位
s = s.substring(pos);
int l = s.length();
s = s.substring(0, l - 1);
pos = s.lastIndexOf("\\");
s = s.substring(pos + 1);
filename = s; //文件名存入
//下面这部分从字节数组中取出文件的数据
pos = byteIndexOf(b, temp, 0); //定位行
//定位下一行,2表示一个回车和一个换行占2个字节
b = subBytes(b, pos + temp.getBytes().length + 2, b.length);
//再读一行信息,使这一部分数据的content-type
s = reqbuf.readLine();
//设置文件输入流,准备写文件
//filename="d:/jspserver/"+filename;
File f = new File(filepath + filename);
fileout = new DataOutputStream(new
FileOutputStream(f));
//字节数组再往下一行,4表示两个回车换行占4个字节,本行的回车
//换行2个字节,content-type得下一行拾回车换行表示的空行,占2个字节
//得到文件数据的起始位置
b = subBytes(b, s.getBytes().length + 4, b.length);
pos = byteIndexOf(b, boundary, 0); //定位文件数据的结尾
if (pos != -1) {
b = subBytes(b, 0, pos - 1); //取得文件数据
fileout.write(b, 0, b.length - 1); //将文件数据存盘
fileSize = b.length - 1; //文件长度存入
}
else {
fileout.write(b);
fileSize = fileSize + b.length;
}
state = FILEDATA;
//fileout.close();
}
break;
case FIELDDATA:
//读出表单域的值
s = reqbuf.readLine();
fieldvalue = s;
formfields.put(fieldname, fieldvalue);
state = NONE;
break;
case FILEDATA:
//如果是文件数据不进行分析,直接读取
while ( (!s.startsWith(boundary)) && (!s.startsWith(lastboundary))) {
s = reqbuf.readLine();
if (s.startsWith(boundary)) {
state = DATAHEADER;
}
else
break;
}
break;
}
}
//in.close();
fileout.close();
}
//文件上传部分
//字节数组中的Indexof寒暑,与String类中的indexof类似
//b要搜索的字节数组,s要找的字符串,start搜索的起始位置
//如果找到,返回s的第一个字节在buffer中的下标,没有则返回-1
private static int byteIndexOf(byte[] b, String s, int start) {
return byteIndexOf(b, s.getBytes(), start);
}
//字节数组中的Indexof寒暑,与String类中的indexof类似
//b要搜索的字节数组,s要找的字符串,start搜索的起始位置
//如果找到,返回s的第一个字节在buffer中的下标,没有则返回-1
private static int byteIndexOf(byte[] b, byte[] s, int start) {
int i;
if (s.length == 0) {
return 0;
}
int max = b.length - s.length;
if (max < 0) {
return -1;
}
if (start > max) {
return -1;
}
if (start < 0) {
start = 0;
//在b中找到s的第一元素
}
search:
for (i = start; i <= max; i++) {
if (b[i] == s[0]) {
int k = 1;
while (k < s.length) {
if (b[k + i] != s[k]) {
continue search;
}
k++;
}
return i;
}
}
return -1;
}
//用于从一个字节数组中提取一个字节数组,类似于String累得substring()
private static byte[] subBytes(byte[] b, int from, int end) {
byte[] result = new byte[end - from];
System.arraycopy(b, from, result, 0, end - from);
return result;
}
//用于从一个字节数组中提取一个字节数组,类似于String累得substring()
private static String subByteString(byte[] b, int from, int end) {
return new String(subBytes(b, from, end));
}
}