• 上传文件,获取表单数据和文件流


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");
      WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
      SessionParams sessionParams = (SessionParams) VSessionUtils.getSessionParams(request);
      UserLogin userLogin = sessionParams.getUserInfo();
      UserConfig userConfig = sessionParams.getCustoms();

      FileInfo fileInfo = new FileInfo();
      String fileName = "";
      String orgFileName = "";
      String bucket="";
      if(null!=userLogin && null!=userLogin.getWebFile()){
        bucket= userLogin.getWebFile().getBucketName();
      }
      String fileUrl = "";
      String vId = "";
      String wId = "";
      WebCastServiceImpl webcastService=BeanUtils.getBean(WebCastServiceImpl.class);
      // 创建缓冲区
      DiskFileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);

      FileItemIterator fileItem = null;
      InputStream in=null;
      try{
      List<FileItem> items = upload.parseRequest(request);
      Iterator iter = items.iterator();

      while (iter.hasNext()) {
      FileItem item = (FileItem) iter.next();
      // String name = item.getFieldName();
      if(item.isFormField()){//判断是否是文件流

        String name = item.getFieldName();
        String value = item.getString();
        if(name.equals("wId")){
          wId=item.getString();
        }
      }
      if(!item.isFormField() && item.getName().length() > 0){
        fileName = item.getName();//会将完整路径名传过来
        /*int start = value.lastIndexOf("\");
        fileName = value.substring(start+1);*/
        if(fileName.indexOf("\") > 0){
        String[] tempArr = fileName.split("\\");
        fileName = tempArr[tempArr.length -1];
      }
      fileInfo.setFilename(fileName);
      fileInfo.setFiletype(this.getExtension(fileName));
      fileInfo.setFilename(getFileUrl(fileName,fileInfo.getFiletype()));
      orgFileName = fileName;
      if(!fileInfo.getFiletype().equals("mp4")){
        response.sendRedirect("/worklite/webcast/open/uploadTemp?success=type&vId=&fileName=");
        return;
      }
      if(null!=fileName && !fileName.equals("")){
        UUID uuid = UUID.randomUUID();
        fileName = uuid.toString()+"."+fileInfo.getFiletype();
        fileInfo.setFilename(fileName);
      }
      in = item.getInputStream();
      //item.write(new File(realPath,fileName));

      }
      }
      WebCast_baseinfo base=webcastService.getWebCast(wId);
      LocalFileManager fileMgr = new LocalFileManager();
      fileUrl = fileMgr.recvFromStream(FileConstants.TYPE_MEDIA, FileConstants.SUBTYPE_VIDEO, fileInfo.getFilename(), in);
      vId=UploadVideo.upload(fileMgr.getServerFileURL(FileConstants.TYPE_MEDIA, FileConstants.SUBTYPE_VIDEO,fileName), base.getCompanyId()+"_"+base.getwName()+"_"+orgFileName);

      WebCast_upvideo upvideo=webcastService.getWebcastUploadVideo(wId);
      try{
        if(null!=upvideo){
          webcastService.uptWebcastUploadVideo(wId,vId);
        }else{
          webcastService.addWebcastUploadVideo(wId,vId,new Date());
        }
      }catch(Exception e){
        Log.error(e.getMessage());
        response.sendRedirect("/worklite/webcast/open/uploadTemp?success=false&vId=&fileName=");
      }
      response.sendRedirect("/worklite/webcast/open/uploadTemp?success=true&vId="+vId+"&fileName="+orgFileName);
      }catch(Exception e){
      Log.error(e.getMessage());
      response.sendRedirect("/worklite/webcast/open/uploadTemp?success=false&vId=&fileName=");
    }

    }

  • 相关阅读:
    java获取客户端用户真实ip
    electron制作上位机软件篇(三)启动项目并进行打包
    electron制作上位机软件篇(二)使用serialport进行串口通信
    electron制作上位机软件篇(一):编译安装serialport
    STM32学习篇-蜂鸣器
    STM32学习篇-跑马灯
    日志级别的选择:Debug、Info、Warn、Error还是Fatal
    常用的正则验证
    kindEditor富文本编辑器
    表单验证jq.validate.js
  • 原文地址:https://www.cnblogs.com/momo1210/p/6283864.html
Copyright © 2020-2023  润新知