在这里看到很多讨论文件上传的文章,觉得各有利敝,有些只限于上传文件,而不能同时取得文本字段值,尤其是上传多个文件比较少,现本人做这个上传文件的类最多可支持上传255个文件,同时可取得文本字段值,请各位高手指正.
文件上传类:moquploadbean.java
package net.jspcn.tool;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* title: 文件上传类
* description: 既能对文件进行上传,又能取得输入框的值,最多可同时上传255个文件
* copyright: copyright (c) 2002
* company: tekson
* @author 莫琼
* @version 1.0
*/
public class uploadbean {
private string[] sourcefile = new string[255]; //源文件名
private string[] suffix = new string[255]; //文件后缀名
private string cansuffix = ".gif.jpg.jpeg.png"; //可上传的文件后缀名
private string objectpath = "c:/"; //目标文件目录
private string[] objectfilename = new string[255]; //目标文件名
private servletinputstream sis = null; //输入流
private string[] description = new string[255]; //描述状态
private long size = 100 * 1024; //限制大小
private int count = 0; //已传输文件数目
private byte[] b = new byte[4096]; //字节流存放数组
private boolean successful = true;
private hashtable fields = new hashtable();
public uploadbean() {
}
//设置上传文件的后缀名
public void setsuffix(string cansuffix) {
this.cansuffix = cansuffix;
}
//设置文件保存路径
public void setobjectpath(string objectpath) {
this.objectpath = objectpath;
}
//设置文件保存路径
public void setsize(long maxsize) {
this.size = maxsize;
}
//文件上传处理程序
public void setsourcefile(httpservletrequest request) throws ioexception {
sis = request.getinputstream();
int a = 0;
int k = 0;
string s = "";
while ( (a = sis.readline(b, 0, b.length)) != -1) {
s = new string(b, 0, a);
if ( (k = s.indexof("filename=\"")) != -1) {
// 取得文件数据
s = s.substring(k + 10);
k = s.indexof("\"");
s = s.substring(0, k);
sourcefile[count] = s;
k = s.lastindexof(".");
suffix[count] = s.substring(k + 1);
if (cantransfer(count)) {
transferfile(count);
}
++count;
} else if ( (k = s.indexof("name=\"")) != -1) {
// 普通表单输入元素,获取输入元素名字
string fieldname = s.substring(k+6, s.length()-3);
sis.readline(b, 0, b.length);
stringbuffer fieldvalue = new stringbuffer(b.length);
while ( (a = sis.readline(b, 0, b.length)) != -1) {
s = new string(b, 0, a-2);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
} else {
fieldvalue.append(s);
}
}
fields.put(fieldname, fieldvalue.tostring());
}
if (!successful)
break;
}
}
//取得表单元素值
public string getfieldvalue(string fieldname) {
if (fields == null || fieldname == null) {
return null;
}
return (string) fields.get(fieldname);
}
//取得上传文件数
public int getcount() {
return count;
}
//取得目标路径
public string getobjectpath() {
return objectpath;
}
//取得源文件名
public string[] getsourcefile() {
return sourcefile;
}
//取得目标文件名
public string[] getobjectfilename() {
return objectfilename;
}
//取得上传状态描述
public string[] getdescription() {
return description;
}
//判断上传文件的类型
private boolean cantransfer(int i) {
suffix[i] = suffix[i].tolowercase();
//这个是用来传图片的,各位可以把后缀名改掉或者不要这个条件
if (sourcefile[i].equals("") || (!(cansuffix.indexof("."+suffix[i])>=0))) {
description[i] = "err: file suffix is wrong.";
return false;
}
else {
return true;
}
}
//上传文件转换
private void transferfile(int i) {
string x = long.tostring(new java.util.date().gettime());
try {
objectfilename[i] = x + "." + suffix[i];
fileoutputstream out = new fileoutputstream(objectpath + objectfilename[i]);
int a = 0;
int k = 0;
long hastransfered = 0; //标示已经传输的字节数
string s = "";
while ( (a = sis.readline(b, 0, b.length)) != -1) {
s = new string(b, 0, a);
if ( (k = s.indexof("content-type:")) != -1) {
break;
}
}
sis.readline(b, 0, b.length);
while ( (a = sis.readline(b, 0, b.length)) != -1) {
s = new string(b, 0, a);
if ( (b[0] == 45) && (b[1] == 45) && (b[2] == 45) && (b[3] == 45) && (b[4] == 45)) {
break;
}
out.write(b, 0, a);
hastransfered += a;
if (hastransfered >= size) {
description[count] = "err: the file " + sourcefile[count] +
" is too large to transfer. the whole process is interrupted.";
successful = false;
break;
}
}
if (successful) {
description[count] = "right: the file " + sourcefile[count] +
" has been transfered successfully.";
}
out.close();
if (!successful) {
sis.close();
file tmp = new file(objectpath + objectfilename[count]);
tmp.delete();
}
}
catch (ioexception ioe) {
description[i] = ioe.tostring();
}
}
public static void main(string[] args) {
system.out.println("test ok");
}
}
文件上传调用:moqupload.jsp
〈%@ page contenttype="text/html; charset=gb2312" %>
〈html>
〈head>
〈title>文件上载〈/title>
〈/head>
〈body>
〈form action="moquploadsubmit.jsp" enctype="multipart/form-data" method="post">
作者姓名:〈input type="text" name="author" />
〈br />
公司名称:〈input type="text" name="company" />
〈br />
文件描述:〈input type="text" name="comment" />
〈br />
选择文件1:〈input type="file" name="filename1" />
〈br />
选择文件2:〈input type="file" name="filename2" />
〈br />
选择文件3:〈input type="file" name="filename3" />
〈br />
选择文件4:〈input type="file" name="filename4" />
〈br />
〈input type="submit" value="上载" />
〈/form>
〈/body>
〈/html>
文件上传提交:moquploadsubmit.jsp
〈%@ page contenttype="text/html;charset=gb2312"%>
〈jsp:usebean id="filebean" scope="page" class="net.moq.www.moquploadbean" />
〈%
filebean.setobjectpath("d:\\temp\\");
filebean.setsize(10000*1024);
filebean.setsuffix(".gif.jpg.png.jpge.html.htm");
filebean.setsourcefile(request);
string [] sasourcefile = filebean.getsourcefile();
string [] saobjectfile = filebean.getobjectfilename();
string [] sadescription = filebean.getdescription();
int icount = filebean.getcount();
string sobjectpath = filebean.getobjectpath();
for(int i=0;i〈icount;i++) {
out.println("〈br>源始文件:");
out.println(sasourcefile[i]);
out.println("〈br>目标文件:");
out.println(sobjectpath+saobjectfile[i]);
out.println("〈br>上传说明:");
out.println(sadescription[i]);
out.println("〈br>");
}
out.println("〈br>作者:" + filebean.getfieldvalue("author"));
out.println("〈br>公司:" + filebean.getfieldvalue("company"));
out.println("〈br>说明:" + filebean.getfieldvalue("comment"));
%>
