4G的文件 以web的方式怎么上传。。。
------解决方案--------------------
- Java code
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(); } }
------解决方案--------------------
- Java code
public synchronized ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { NpvidoForm npvidoForm = (NpvidoForm) form;// TODO Auto-generated method Timestamp timestamp = new Timestamp(System.currentTimeMillis()); npvidoForm.getNpvido().setPostTime(timestamp); // 文件大小 // ------------------------------------------------// PersonInfo personInfo1 = AuserinfoUtil.getPersonInfo(request, response); try { npvidoForm.getNpvido().setZtname((int)npvidoForm.getFile().getFileSize()/1024+""); uploadId = personInfo1.getId(); allCount = npvidoForm.getFile().getFileSize(); // 限制文件大小为100M以内 if (npvidoForm.getFile().getFileSize() < 102400000) { String fileName = npvidoForm.getFile().getFileName(); if (fileName.substring(fileName.lastIndexOf(".") + 1,fileName.length()).equals("flv")) { String systemFileName = System.currentTimeMillis()+ fileName.substring(fileName.lastIndexOf("."),fileName.length()); DataInputStream inputStream = new DataInputStream(npvidoForm.getFile().getInputStream()); // 服务器的位置 String path = request.getContextPath(); String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; String pathString = request.getRealPath("/")+ "UserFiles\\video\\"; File file = new File(pathString); if (!file.exists()) file.mkdirs(); // 文件实际上传地址 String uploadPath = pathString + systemFileName; // 存储位置 npvidoForm.getNpvido().setPicUrl(basePath+"UserFiles\\video\\" + systemFileName); System.out.println("上传的路径:"+uploadPath); System.out.println("数据库保存的路径:"+basePath+"UserFiles\\video\\" + systemFileName); // 上传 DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(uploadPath))); dataOutputStream.flush(); int b = -1; while ((b = inputStream.read()) != -1) { dataOutputStream.write(b); //为了做简易的进度条采用的获取当前已上传的文件的大小 File file2 = new File(uploadPath); nowCount = (int) file2.length(); } inputStream.close(); dataOutputStream.close(); } else { return new ActionForward("findAll","/papersite/channel/paper/addNpvido.jsp", false); } } else { request.setAttribute("uploadSize", "0"); return new ActionForward("findAll","/papersite/channel/paper/addNpvido.jsp", false); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // ------------------------------------------------// npvidoForm.getNpvido().setComeFrom(personInfo1.getAccount()); npvidoService.save(npvidoForm.getNpvido()); nowCount = 0; allCount = 0; averageCount = 0; uploadId = 0; // log PersonInfo personInfo = AuserinfoUtil.getPersonInfo(request, response); npvidoService.logAdd(personInfo.getId(), "视频管理", "添加", "添加ID:" + npvidoForm.getNpvido().getId(), request); return new ActionForward("findAll", "/npvido.do?action=findAll", false); }