如何在2 @RequestMapping之间传递数据

时间:2016-08-04 14:57:44

标签: java spring

我有2个@RequestMapping;第一个将视频上传到我的服务器,第二个执行转码linux命令。

每个@RequestMapping都在工作,但是我从一周开始尝试从第一个@RequestMapping获取上传文件的文件名并将其发送到第二个。

请帮助我!

这是第一个:

@RequestMapping(value = "/uploadajax", method = RequestMethod.POST)
@ResponseBody
public List<ObjectNode> uploadMultipleFiles(Model model, MultipartHttpServletRequest request, HttpServletResponse response,
        Principal principal) throws IOException {



    CommonsMultipartFile multipartFile = null;
    Iterator<String> iterator = request.getFileNames();


    ObjectMapper objectMapper = new ObjectMapper();
    FileMeta fileMeta = null;

    while (iterator.hasNext()) {

        String key = iterator.next();

        multipartFile = (CommonsMultipartFile) request.getFile(key);
        String uploadedFileName = multipartFile.getOriginalFilename();             


        try {
            List<ObjectNode> listFileNode = new ArrayList<ObjectNode>();
            byte[] bytes = multipartFile.getBytes();
            String phyPath = request.getSession().getServletContext().getRealPath("/");
            String repoUserPath=phyPath+"resources/"+user.getId();
            System.out.println("-------------"+user.getId()+"------------------");
            System.out.println("####"+repoUserPath);
            File repoUSer = new File(repoUserPath);

            if (!repoUSer.exists()) {
                repoUSer.mkdir();

                String filepath =repoUserPath+"/"+multipartFile.getOriginalFilename();
                System.out.println("####"+filepath);
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(filepath));
                stream.write(bytes);
                stream.close();

                fileMeta = new FileMeta();
                fileMeta.setFileName(multipartFile.getOriginalFilename());
                fileMeta.setFileSize((multipartFile.getSize()/1024)/1024+" Mb");
                fileMeta.setFileType(multipartFile.getContentType());
                fileMeta.setPathfile("http://89.40.113.84:8080/ProjectTranscode/resources/"+user.getId()+"/"+multipartFile.getOriginalFilename()); 
                fileMeta.setLinkdelete("/var/lib/tomcat7/webapps/ProjectTranscode/resources/"+user.getId()+"/"+multipartFile.getOriginalFilename());
                serviceMangerTranscode.save(fileMeta);
                fileMeta.setUser(user);
                serviceMangerTranscode.updateFile(fileMeta);
                file= fileMeta;
                model.addAttribute("Nomf", file.getFileName());



                System.out.println("created!!");
                String[] cmdArray = {"ffprobe","-show_streams","-i",filepath  };
                model.addAttribute("filepath",filepath);
                System.out.println("ffprobe2........!!");
                 //String[] cmdArray = {"ping","www.google.com" };
                Process process = Runtime.getRuntime().exec(cmdArray);
                InputStream processInputStream =process.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(processInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


                String line = null;
                System.out.println("<OUTPUT>");

                while ( (line = bufferedReader.readLine()) != null){
                     ObjectNode node = objectMapper.createObjectNode();

                    System.out.println(line);
                    node.put("line",line );

                    listFileNode.add(node);

                }

                line ="aaa";
                ObjectNode node  = objectMapper.createObjectNode();

                node.put("line",line );
                System.out.println("</OUTPUT>");

                return listFileNode;
            }else {
                 long size = FileUtils.sizeOfDirectory(repoUSer);
                 System.out.println("size 1"+(size/1024)/1024);
                 if ((size/1024)/1024 >100) {
                     ObjectNode node = objectMapper.createObjectNode();                         
                    node.put("error","Size Not Supported for Your repository !");
                    listFileNode.add(node);
                }else{
                System.out.println("choix2!!++");
                String filepath =repoUserPath+"//"+multipartFile.getOriginalFilename();
                System.out.println("####"+filepath);
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(filepath));
                stream.write(bytes);
                stream.close();

            //  fileMeta = new FileMeta();
                fileMeta.setFileName(multipartFile.getOriginalFilename());
                fileMeta.setFileSize((multipartFile.getSize()/1024)/1024+" Mb");
                fileMeta.setFileType(multipartFile.getContentType());
                fileMeta.setPathfile(filepath); 
                fileMeta.setLinkdelete("/var/lib/tomcat7/webapps/ProjectTranscode/resources/"+user.getId()+"/"+multipartFile.getOriginalFilename());
                serviceMangerTranscode.save(fileMeta);
                fileMeta.setUser(user);
                serviceMangerTranscode.updateFile(fileMeta);
                file= fileMeta;
                model.addAttribute("filedetails", fileMeta);
                System.out.println("fichier créé!!");
                 String[] cmdArray = {"ffprobe","-show_streams","-i",filepath  };
                 System.out.println("ffprobe executee........!!");
                 Process process = Runtime.getRuntime().exec(cmdArray);
                  InputStream processInputStream =process.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(processInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                String line = null;
                System.out.println("<OUTPUT>");

                while ( (line = bufferedReader.readLine()) != null){
                     ObjectNode node = objectMapper.createObjectNode();
                    // if (line.contains("codec_type") || line.contains("duration_ts")){
                    System.out.println(line);
                    node.put("line",line );
                    listFileNode.add(node);

                }//}
                line ="bbb";
                ObjectNode node  = objectMapper.createObjectNode();

                node.put("line",line );
                System.out.println("</OUTPUT>");
                System.out.println("size 2"+(size/1024)/1024);
                return listFileNode;

            }

            }
        } catch (Exception e) {
            return null;
        }
    }

    return null;
}

这是第二个:

@RequestMapping(value ="/Transcodage", method = RequestMethod.POST)
public String Trascoood(Model model, HttpServletRequest request, HttpServletResponse response

         )   throws IOException {



//Recuperation des valeurs utilisateur: 
 String userIDD =request.getParameter("useridd");   
 String newFileName =request.getParameter("newFileName");
 String encolist=request.getParameter("encolist");
 String codeclist=request.getParameter("codeclist");
 String bitrate=request.getParameter("bitratevideo");
String Nomf= request.getParameter("mapping1Form");

System.out.println("NOMFFFFFFFFFFF: " + Nomf);

 int userIDDD =  Integer.parseInt(userIDD); 
User user = userv.getUserById(userIDDD);

ObjectMapper objectMapper = new ObjectMapper();
org.codehaus.jackson.node.ObjectNode node = objectMapper.createObjectNode();

//file= serviceMangerTranscode.getFileById(idfile);

    ModelAndView modelUserHome = new ModelAndView("userhome");

    //System.out.println("file path : " + file.getPathfile() +"*************************************************");
    System.out.println("userIDD: " + userIDD + "...............................................");
    System.out.println("newFileName: " + newFileName + "...............................................");
    System.out.println("encolist: " + encolist + "...............................................");
    String newFileN = new StringBuilder().append(".").append(newFileName).append(".").append(encolist).toString(); 

    System.out.println("FILE NAME + EXTENTION: " + newFileN + "...............................................");
    //node.put("success", "true");
    String[] cmdArray = {"sudo", "ssh", "-tt", "root@89.40.113.84", "/root/dved", "-l", "89.40.112.120,89.40.112.248", "you.mp4", "-s",newFileN};
    //String[] cmdArray = {"sudo", "/root/dve", "-l", "89.40.112.120,89.40.112.248", "/root/greece.mkv", "-s",".teeeeeeeeeeeeest.avi" };
    //String[] cmdArray = {"sudo","/root/dve","-s",".encoded.avi","-l","89.40.112.248","/root/greece.mkv" };

    List<ObjectNode> listFileNode = new ArrayList<ObjectNode>(); 
        try{
            Runtime rt = Runtime.getRuntime();

             ProcessBuilder pb = new ProcessBuilder(cmdArray);
             Process proc = pb.start();
             //Process proc = rt.exec(cmdArray);
              int rc =0;
              rc = proc.waitFor();       // Wait for the process to finish.

              System.out.printf("Script executed successfully in ", rc);

                InputStream stderr = proc.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);

                String line = null;
                System.out.println("<ERROR___EXEC>");


                while ( (line = br.readLine()) != null){

                    System.out.println(line);
                    node.put("line",line );
                    listFileNode.add(node);
                  }
                System.out.println("</ERROR___EXEC>");
                int exitVal = proc.waitFor();
                System.out.println("Process exitValue: " + exitVal);


                InputStream processInputStream =proc.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(processInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                System.out.println("<RESULTAT___EXEC>"); 
                while ( (line = bufferedReader.readLine()) != null){

                    System.out.println(line);
                    node.put("lineR",line );
                    listFileNode.add(node);
                                  } 
                System.out.println("</RESULTAT___EXEC>");  

        }catch (Throwable t)
          {
            t.printStackTrace();
          }
                model.addAttribute("usersize",user.getUserfiles().size());
                model.addAttribute("listfiles",user.getUserfiles());
                model.addAttribute("user",user);
                model.addAttribute("MsgTranscode","Votre video a bien ete encodee, vous pouvez desormais la telecharger de votre portfolio!");
                return "userhome";

}

1 个答案:

答案 0 :(得分:0)

感谢您的回复,我认为无法完成,所以我将数据放入数据库,而不是在第二次RequestMapping中恢复