import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
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.FileUploadException;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public
class
FileUploadServlet
extends
HttpServlet
{
private
static
final
long serialVersionUID = 1L;
public
FileUploadServlet()
{
super();
}
protected
void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
protected
void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
final
long MAX_SIZE = 2048 * 1024 * 1024;
final
String[] allowedExt =
new
String[]
{
"exe"
,
"jpg"
,
"DT"
};
response.setContentType(
"text/html"
);
response.setCharacterEncoding(
"UTF-8"
);
DiskFileItemFactory dfif =
new
DiskFileItemFactory();
ServletFileUpload sfu =
new
ServletFileUpload(dfif);
sfu.setSizeMax(MAX_SIZE);
PrintWriter out = response.getWriter();
List fileList = null;
try
{
fileList = sfu.parseRequest(request);
}
catch
(FileUploadException e)
{
if
(e
instanceof
SizeLimitExceededException)
{
out.println(
"文件尺寸超过规定大小:"
+ MAX_SIZE +
"字节<p />"
);
out.println(
"<a href=\"FileUpload.html\" target=\"_top\">返回</a>"
);
return
;
}
e.printStackTrace();
}
if
(fileList == null || fileList.size() == 0)
{
out.println(
"请选择上传文件<p />"
);
out.println(
"<a href=\"FileUpload.html\" target=\"_top\">返回</a>"
);
return
;
}
DecimalFormat digit=
new
DecimalFormat(
"0.00"
);
Iterator fileItr = fileList.iterator();
while
(fileItr.hasNext())
{
FileItem fileItem = null;
String path = null;
double size = 0;
fileItem = (FileItem) fileItr.next();
if
(fileItem == null || fileItem.isFormField())
{
continue
;
}
size = (double)fileItem.getSize()/1024;
if
(
""
.equals(path) || size == 0)
{
out.println(
"<html><head><title>上传处理界面</title></head>"
);
out.println(
"请选择上传文件<p />"
);
out.println(
"<a href=\"FileUpload.html\" target=\"_top\">返回</a>"
);
out.println(
"</html>"
);
return
;
}
path = fileItem.getName();
String t_name = path.substring(path.lastIndexOf(
"\\"
) + 1);
String t_ext = t_name.substring(t_name.lastIndexOf(
"."
) + 1);
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for
(; allowFlag < allowedExtCount; allowFlag++)
{
if
(allowedExt[allowFlag].equals(t_ext))
break
;
}
if
(allowFlag == allowedExtCount)
{
out.println(
"<html><head><title>上传处理界面</title></head>"
);
out.println(
"请上传以下类型的文件<p />"
);
for
(allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out.println(
"*."
+ allowedExt[allowFlag]
+
" "
);
out.println(
"<p /><a href=\"FileUpload.html\" target=\"_top\">返回</a>"
);
out.println(
"</html>"
);
return
;
}
try
{
fileItem.write(
new
File(
"\\"
+t_name));
System.out.println(t_name);
out.println(
"<html><head><title>上传处理界面</title></head>"
);
out.println(
"文件名称为:"
+ path +
"<br>"
);
out.println(
"文件上传成功, 已保存为: "
+ t_name
+
"<br>"
+
" 文件大小: "
+ digit.format(size) +
"K <p />"
);
out.println(
"<a href=\"FileUpload.html\" target=\"_top\">继续上传</a>"
);
out.println(
"</html>"
);
}
catch
(Exception e)
{
e.printStackTrace();
}
}
}
}