[jsp]代码库
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
public class UploadServlet extends HttpServlet {
private final String uploadPath = "C:/file/"; // 用于存放上传文件的目录
private final String tempPath = "C:/tempFile"; // 用于存放临时文件的目录
private final int flushSize = 1024 * 512; // 缓冲区大小
private final long fileSize = 10 * 1024 * 1024; // 文件最大大小
public void init() throws ServletException {
File file = new File (uploadPath);
if (!file.exists() ) {
file.mkdir();
}
file = new File (tempPath);
if (!file.exists() ) {
file.mkdir();
}
}
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 判断from表单的enctype属性值是否设置为multipart/form-data
boolean isMultipart = FileUpload.isMultipartContent (request);
if (!isMultipart) {
return;
}
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置缓冲区大小
factory.setSizeThreshold (flushSize);
factory.setRepository (new File (tempPath) );
// 创建FileUpload对象
FileUpload upload = new FileUpload (factory);
// 设置文件最大大小
upload.setSizeMax (fileSize);
List items = upload.parseRequest (request);
Iterator iter = items.iterator();
while (iter.hasNext() ) {
FileItem item = (FileItem) iter.next();
// 判断是否是file表单元素
if (!item.isFormField() ) {
String name = item.getName();//获得文件路径及名称
name = name.substring (name.lastIndexOf (File.separator) + 1,
name.length() ); //获得文件名
String fileType = name.substring (name.indexOf (".") ); //获得文件后缀名
String filename = getFileName();//随机产生一个文件名防止文件名重复
long size = item.getSize();
//如果文件名不存在 则不处理
if ( (name == null || name.equals ("") ) && size == 0) {
continue;
}
item.write (new File (uploadPath + filename + fileType) );
} else {
//获得文件描述
String desc = item.getString ("UTF-8");
System.out.println (desc);
}
}
response.sendRedirect ("upload.jsp?code=001");
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect ("upload.jsp?code=002");
}
}
private String getFileName() {
//根据时间产生文件名
SimpleDateFormat df = new SimpleDateFormat ("yyyyMMddhhmmssms");
return df.format (new Date() );
}
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet (request, response);
}
}
/****************************************/
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<c:if test="${param.code eq '001'}">
<div style="color:red">上传成功!</div>
</c:if>
<c:if test="${param.code eq '002'}">
<div style="color:red">上传失败!</div>
</c:if>
<form action="uploadServlet" method="post" enctype="multipart/form-data">
照片描述:<input type="text" name="desc"/><br/>
<input type="file" name="file1"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>