1.下载相应的组件的最新版本
Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载
附加的Commons IO 可以在http://jakarta.apache.org/commons/io/下载
2.将commons-fileupload-1.2.1.jar commons-io-1.4.jar拷贝到$TOMCAT\common\lib目录下
3.编写相应的代码如下
UploadExample.jsp
<%@ page contentType='text/html;charset=gb2312'%>
<html>
<title><%= application.getServerInfo() %></title>
<body>
上传文件程序应用示例
<form action='doUpload.jsp' method='post' enctype='multipart/form-data'>
<%-- 类型enctype用multipart/form-data,这样可以把文件中的数据作为流式数据上传,不管是什么文件类型,均可上传。--%>
请选择要上传的文件<input type='file' name='upfile' size='50'>
<input type='submit' value='提交'>
</form>
</body>
</html>
doUpload.jsp
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<% response.setContentType("text/html");
// 图片上传路径
String uploadPath =request.getSession().getServletContext().getRealPath("/")+"upload/images/";
// 图片临时上传路径
String tempPath = request.getSession().getServletContext().getRealPath("/")+"upload/images/temp/";
// 图片网络相对路径
String imagePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
// 文件夹不存在就自动创建:
if(!new File(uploadPath).isDirectory())
new File(uploadPath).mkdirs();
if(!new File(tempPath).isDirectory())
new File(tempPath).mkdirs();
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while(i.hasNext()) {
FileItem file = (FileItem)i.next();
// 获得文件名,这个文件名是用户上传时用户的绝对路径:
String sourcefileName = file.getName();
if(sourcefileName!=null&&(sourcefileName.endsWith(".jpg")||sourcefileName.endsWith(".gif"))) {
// 在这里可以记录用户和文件信息,生成上传后的文件名
String destinationfileName=null;
Random rd = new Random();
Calendar time = Calendar.getInstance();
if(sourcefileName.endsWith(".jpg")){
destinationfileName=String.valueOf(time.get(Calendar.YEAR))
+ String.valueOf(time.get(Calendar.MONTH))
+ String.valueOf(time.get(Calendar.DAY_OF_MONTH))
+ String.valueOf(time.get(Calendar.HOUR_OF_DAY))
+ String.valueOf(time.get(Calendar.MINUTE))
+ String.valueOf(time.get(Calendar.SECOND))
+ String.valueOf(rd.nextInt(100)) + ".jpg";
}else if(sourcefileName.endsWith(".gif")){
destinationfileName=String.valueOf(time.get(Calendar.YEAR))
+ String.valueOf(time.get(Calendar.MONTH))
+ String.valueOf(time.get(Calendar.DAY_OF_MONTH))
+ String.valueOf(time.get(Calendar.HOUR_OF_DAY))
+ String.valueOf(time.get(Calendar.MINUTE))
+ String.valueOf(time.get(Calendar.SECOND))
+ String.valueOf(rd.nextInt(100)) + ".gif";
}
File f1=new File(uploadPath+ destinationfileName);
file.write(f1);
out.print(sourcefileName+"成功上传!") ;
out.print("<img src="+imagePath+"upload/images/"+destinationfileName+">");
}else{
out.println("上传文件出错,只能上传 *.jpg , *.gif");
}
}
// 跳转到上传成功提示页面
}
catch(Exception e) {
// 可以跳转出错页面
}
out.flush();
out.close();
%>