日期:2014-05-20 浏览次数:20874 次
<html:form action="uploadAction.do?method=upFile" enctype="multipart/form-data"> <c:choose> <c:when test="${zn == 'pilot'}"> <input type="radio" name="name" value="pilot" checked="checked" onclick="getZoneDate('pilot');"/> <font color="red">PRFQ1</font> </c:when> <c:otherwise> <input type="radio" name="name" value="pilot" onclick="getZoneDate('pilot');"/> <font color="red">PRFQ1</font> </c:otherwise> </c:choose> <c:choose> <c:when test="${zn == 'week'}"> <input type="radio" name="name" value="week" checked="checked" onclick="getZoneDate('week');"/> <font color="red">PRFQ2</font> </c:when> <c:otherwise> <input type="radio" name="name" value="week" onclick="getZoneDate('week');"/> <font color="red">PRFQ2</font> </c:otherwise> </c:choose> <br/> Select Excel File:<html:file property="theFile" /> <html:submit value="upload"/> <br/> FeedBack File:<html:file property="theFile" /> <html:submit value="upload" onclick="this.form.action='uploadAction.do?method=upFBFile'"/> </html:form>
public ActionForward upFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String filePathName = ""; if (form instanceof UploadForm) { String encoding = request.getCharacterEncoding(); if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) { response.setContentType("text/html; charset=gb2312"); } UploadForm theForm = (UploadForm) form; FormFile file = theForm.getTheFile(); String contentType = file.getContentType(); String zoneName = theForm.getName(); String size = (file.getFileSize() + " bytes"); String fileName = file.getFileName(); // System.out.println("fileName="+fileName); //这里打印为"fileName=",明显没有值 String timeStr = df.getTime("yyyyMMddhhmmss"); try { InputStream stream = file.getInputStream(); String filePath = request.getRealPath("/"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream bos = null; filePathName = filePath+ "files" + "\\"+ timeStr+file.getFileName(); String pathName = request.getSession().getServletContext().getRealPath("/") + "files"; File files= new File(pathName); if(files.exists()&&files.isDirectory()){ bos = new FileOutputStream(filePath + "files"+ "\\" + timeStr+file.getFileName()); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } }else{ boolean flag = files.mkdir(); if(flag){ bos = new FileOutputStream(filePath + "files"+ "\\" + timeStr+file.getFileName()); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } } } bos.close(); stream.close(); } catch (Exception e) { } request.setAttribute("contentType", contentType); request.setAttribute("size", size); request.setAttribute("fileName", fileName); request.setAttribute("filePathName", filePathName); request.setAttribute("zoneName", zoneName); return mapping.findForward("display"); } return null; }