日期:2014-05-16 浏览次数:20834 次
<script>
function callUpload() {
var fileValue = document.FileUpload.file1.value;
if (fileValue == "") {
alert("请选择所需上传的文件!");
return false;
}
showLayer.style.display = "inline";
//count()
FileUpload.submit();
}
</script>
<body>
<form name="FileUpload" method="post" action="uploads" enctype="multipart/form-data" >
<table border="0">
<tr>
<td nowrap>
<div align="left">导入的EXCEL文件(导入的明细会复盖原有明细数据):</div>
</td>
</tr>
<tr>
<td nowrap>
<input type="file" class="mybutton" name="file1" size="50" style="border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px">
<input type="submit" class="mybutton" value="导入" name="shangchuan" onClick="return callUpload()" style="border-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px" >
</td>
</tr>
</table>
<table width="100%">
<tr>
<td>
<hr width="100%">
</td>
</tr>
</table>
</form>
</body>
?
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final long MAX_SIZE = 3 * 1024 * 1024;// 设置上传文件最大为 3M
String u_name="";
// 允许上传的文件格式的列表
final String[] allowedExt = new String[] { "xls", "jpeg", "gif", "txt",
"doc", "docx", "mp3", "wma" };
response.setContentType("text/html");
// 设置字符编码为UTF-8, 这样支持汉字显示
response.setCharacterEncoding("GBK");
//实例化RequestContext对象
RequestContext requestContext = new ServletRequestContext(request);
if(FileUpload.isMultipartContent(requestContext)){}
// 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory();
//上传文件胡原始路径
String realpath = this.getServletContext().getRealPath("/")+"ImagesUploadTemp" ;
// 设置存放临时文件的目录
dfif.setRepository(new File(realpath));
dfif.setSizeThreshold(4096);// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘
// 用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
System.err.println(" reapath="+this.getServletContext().getRealPath("/")+"ImagesUploadTemp");
// 设置最大上传尺寸
sfu.setSizeMax(MAX_SIZE);
PrintWriter out = response.getWriter();
// 从request得到 所有 上传域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {// 处理文件尺寸过大异常
e.printStackTrace();
if (e instanceof SizeLimitExceededException) {
out.println("文件尺寸超过规定大小:" + MAX_SIZE + "字节<p />");
out.println("<a href=\"excelInsert.action\" target=\"_top\">返回</a>");
return;
}
//e.printStackTrace();
}
// 没有文件上传
if (fileList == null || fileList.size() == 0) {
out.println("文件大小不能为空,请选择上传文件<p />");
out.println("<a href=\"excelInsert.action\" target=\"_top\">返回</a>");
return;
}
// 得到所有上传的文件
Iterator fileItr = fileList.iterator();
// 循环处理所有文件
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
// 得到当前文件
fileItem = (FileItem) fileItr.next();
// 忽略简单form字段而不是上传域的文件域(<input type="text" />等)
if (fileItem == null || fileItem.isFormField()) {
continue;
}
// 得到文件的完整路径
path = fileItem.getName();
path = new String(path.getBytes("ISO-8859-1"),"UTF-8");
System.out.println("完整路径="+path);
// 得到文件的大小
size = fileItem.getSize();
if ("".equals(path) || size == 0) {