- <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">導(dǎo)入的EXCEL文件(導(dǎo)入的明細(xì)會復(fù)蓋原有明細(xì)數(shù)據(jù)):</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="導(dǎo)入" 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>
<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">導(dǎo)入的EXCEL文件(導(dǎo)入的明細(xì)會復(fù)蓋原有明細(xì)數(shù)據(jù)):</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="導(dǎo)入" 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;// 設(shè)置上傳文件最大為 3M
- String u_name="";
- // 允許上傳的文件格式的列表
- final String[] allowedExt = new String[] { "xls", "jpeg", "gif", "txt",
- "doc", "docx", "mp3", "wma" };
- response.setContentType("text/html");
- // 設(shè)置字符編碼為UTF-8, 這樣支持漢字顯示
- response.setCharacterEncoding("GBK");
- //實(shí)例化RequestContext對象
- RequestContext requestContext = new ServletRequestContext(request);
- if(FileUpload.isMultipartContent(requestContext)){}
- // 實(shí)例化一個(gè)硬盤文件工廠,用來配置上傳組件ServletFileUpload
- DiskFileItemFactory dfif = new DiskFileItemFactory();
- //上傳文件胡原始路徑
- String realpath = this.getServletContext().getRealPath("/")+"ImagesUploadTemp" ;
- // 設(shè)置存放臨時(shí)文件的目錄
- dfif.setRepository(new File(realpath));
- dfif.setSizeThreshold(4096);// 設(shè)置上傳文件時(shí)用于臨時(shí)存放文件的內(nèi)存大小,這里是4K.多于的部分將臨時(shí)存在硬盤
- // 用以上工廠實(shí)例化上傳組件
- ServletFileUpload sfu = new ServletFileUpload(dfif);
- System.err.println(" reapath="+this.getServletContext().getRealPath("/")+"ImagesUploadTemp");
- // 設(shè)置最大上傳尺寸
- 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("文件尺寸超過規(guī)定大小:" + MAX_SIZE + "字節(jié)<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();
- // 循環(huán)處理所有文件
- while (fileItr.hasNext()) {
- FileItem fileItem = null;
- String path = null;
- long size = 0;
- // 得到當(dāng)前文件
- 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) {
- out.println("請選擇上傳文件<p />");
- out.println("<a href=\"excelInsert.action\" target=\"_top\">返回</a>");
- return;
- }
-
- // 得到去除路徑的文件名
- String t_name = path.substring(path.lastIndexOf("\\") + 1);
- // 得到文件的擴(kuò)展名(無擴(kuò)展名時(shí)將得到全名)
- String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
- // 拒絕接受規(guī)定文件?