搞WEB开发,上传下载这个功能是必不可少的,jsp自身没有带这个组件,只能靠我们自己写了,不过毕竟对于新手来讲,能力有限,只好借助别人了,现在网上关于上传的组件也不少,在这里我选择的是apache的COMMON-FILEUPLOAD1.1,与 COMMON-FILEUPLOAD1.0相比,使用方法略有不同,网上很多教程是关于1.0的,如果你在用1.0的方法使用1.1的版本,可能会出现些错误,我本人就是一个典型,呵呵.
长话短说,搞我们这个的都不喜欢看文字的了,要不然我还想开个文学专栏呢,呵呵!
配置方法一(按1.0的使用方法,本人不推荐,但还是拿出来写写):
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import ="java.util.regex.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%
//(要配合org.apache.commons.io使用,记得去下这个包,不太方便,所以本人不推荐使用)
//如果不下这个包,在程序执行到下面的 List fileItems = fu.parseRequest(request); 时会出错
DiskFileUpload fu = new DiskFileUpload();
// 设置允许用户上传文件大小,单位:字节
fu.setSizeMax(1024*1024*100);
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath(application.getRealPath("//") + "temp//");
//开始读取上传信息
List fileItems = fu.parseRequest(request);
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
//正则匹配,过滤路径取文件名
String regExp=".+(.+)$";
//过滤掉的文件类型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;temp<errorType.length;temp++){
if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{
item.write(new File("E://" + m.group(1)));
out.print(name+" "+size+"<br>");
}
catch(Exception e){
out.println(e);
}
}
else
{
throw new IOException("fail to upload");
}
}
}
%>
配置方法二(推荐):
//以下代码是参考javadoc写的,本人测试运行通过,推荐使用该方法,不然用1.1就没意义了
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置内存区块大小
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(new File(application.getRealPath("//") + "temp//"));
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1024*1024*100);
List fileItems = upload.parseRequest(request);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
while(i.hasNext()){
// String comment = ((FileItem)i.next()).getString();//存数据库用
FileItem fi = (FileItem)i.next();
// filename on the client
String fileName = fi.getName();
File tempFile = new File(fileName);
fileName = tempFile.getName();
// save comment and filename to database//存入数据库
// write the file
fi.write(new File(application.getRealPath("//") + "temp//", fileName));
}
%>
其他废话我不说了,记得下相应的包,然后放入对应的classpath下