日期:2014-05-17 浏览次数:20872 次
boolean flag = true; int length = getMyfileFileName().split("\\.").length; String docType = getMyfileFileName().split("\\.")[length - 1];// 文件类型 try{//以下判断文件大小的 FileInputStream fiss = new FileInputStream(myfile); String filesz = String.valueOf(fiss.available()/1000) ;//2000000k===2G if(Long.parseLong(filesz)>2000000l) { try { response.getWriter().println("<script>alert('上传文件不能超过2G!');window.parent.location.reload();</script>"); } catch (IOException e) { e.printStackTrace(); } return null ; } }catch(Exception e1){ System.out.println("我的文档上传出错!"); } // 以下才是正常的文件上传 if (flag) { String currentTime = (new SimpleDateFormat("yyyyMMdd")) .format(new Date());// 把当前时间当成文件夹名字 File dirFile = new File(ServletActionContext.getServletContext() .getRealPath("/") + "upload\\document\\" + currentTime); if (!dirFile.exists()) { dirFile.mkdir(); } String docTitle = request.getParameter("docTitle");// 文件名 String docDescription = request.getParameter("docDescription");// 文件描述 String docPath = "upload\\document\\" + currentTime + "\\" + getMyfileFileName();// 文件路径 // 以服务器的文件保存地址和原文件名建立上传文件输出流 File imageFile = new File(ServletActionContext .getServletContext().getRealPath("/") + "upload\\document\\" + currentTime + "\\" + getMyfileFileName()); copy(myfile, imageFile); FsDocument fd = new FsDocument(); fd.setUserId(userId);// 用户ID-----现在是死的··以后还得动态得到当前用户 fd.setDocType(docType);// 文档类型 fd.setDocTitle(docTitle);// 文档标题 fd.setDocPath(docPath);// 文档路径 fd.setDocId(0l);// 文档ID fd.setDocDescription(docDescription);// 文档描述 fd.setCreTime(new Date());// 创建时间 documentService.saveData(fd); } try { response .getWriter() .println( "<script>alert('文件上传成功!');window.parent.location.reload();</script>"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private void copy(File src, File dst) { try { InputStream in = null ; OutputStream out = null ; try { in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE); byte [] buffer = new byte [BUFFER_SIZE]; while (in.read(buffer) > 0 ) { out.write(buffer); } } finally { if ( null != in) { in.close(); } if ( null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } }
------解决方案--------------------
public synchronized ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServle