【问题标题】:Giving File permissions and running ProcessBuilder in Java Servlets在 Java Servlet 中授予文件权限并运行 ProcessBuilder
【发布时间】:2014-09-13 00:52:44
【问题描述】:

我尝试编写一个 servlet,它根据给定的输入创建一个文件。该文件与适当的文本一起创建,但我无法授予文件 777 权限,并且以后也无法运行 ProcessBuilder。 我认为这是相互关联的,因为在 ProcessBuilder 中触发的命令需要文件具有适当的权限,但是当我尝试使用 chmod 授予它权限时,它也不起作用。

import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import java.io.*;
import java.util.*;
public class Serv extends HttpServlet
{ 
    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
    String jobId=req.getParameter("jobId");
    String jobStatus=req.getParameter("jobStatus");
    String displayName=req.getParameter("displayName");
    String name=req.getParameter("name");
    String description=req.getParameter("description");
    String frequency=req.getParameter("frequency");
    String lastModifiedAt=req.getParameter("lastModifiedAt");
    String createdAt=req.getParameter("createdAt"); 
    String createdBy=req.getParameter("createdBy");     
    String opPath=req.getParameter("opPath");       
    String env=req.getParameter("env");     
    res.setContentType("text/html");
    PrintWriter pw=res.getWriter();
    pw.println("JobId: \n"+jobId);
    pw.println("JobStatus: \n"+jobStatus);
    String path = getServletContext().getRealPath("/");
    File logfile=new File("/var/lib/tomcat7/webapps/ROOT/log.txt");
    logfile.setReadable(true);
    logfile.setWritable(true);
    logfile.setExecutable(true);    
    PrintWriter writer = new PrintWriter(path+"log.txt", "UTF-8");
    writer.println("Job ID : "+jobId);
    writer.println("Job Status : "+jobStatus);
    writer.println("Rule Name : "+name);
    writer.println("Rule Display Name : "+displayName);
    writer.println("Rule Description : "+description);
    writer.println("Rule env : "+env);
    writer.println("Rule frequency : "+frequency);
    writer.println("Rule last modified at : "+lastModifiedAt);
    writer.println("Rule created at : "+createdAt);
    writer.println("Rule created by : "+createdBy);
    writer.println("Notification Path : "+opPath);
    writer.close();
    pw.close();
    try
        {
        // ProcessBuilder pb1=new ProcessBuilder("sudo","/bin/chmod","777","/var/lib/tomcat7/webapps/ROOT/log.txt");
        // pb1.redirectErrorStream(true);
        // Process p1=pb1.start();
        // InputStreamReader isr1 = new  InputStreamReader(p1.getInputStream());
        // BufferedReader br1 = new BufferedReader(isr1);
        // String lineRead1;
        // while ((lineRead1 = br1.readLine()) != null) 
        //     {
        //  System.out.println(lineRead1);
        //     }
        // p1.waitFor();
        ProcessBuilder pb2=new ProcessBuilder("/usr/local/hadoop/bin/hadoop", "fs", "-copyFromLocal", "/var/lib/tomcat7/webapps/ROOT/log.txt",opPath);
        pb2.redirectErrorStream(true);
    //  pb2.directory(new File("/var/lib/tomcat7/webapps/ROOT/"));
        Process p2=pb2.start();
        InputStreamReader isr2 = new  InputStreamReader(p2.getInputStream());
        BufferedReader br2 = new BufferedReader(isr2);
        String lineRead2;
        while ((lineRead2 = br2.readLine()) != null) 
            {
            System.out.println(lineRead2);
            }
        p2.waitFor();
        }
    catch(Exception e){}

    }
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
    String jobId=req.getParameter("jobId");
    String jobStatus=req.getParameter("jobStatus");
    String displayName=req.getParameter("displayName");
    String name=req.getParameter("name");
    String description=req.getParameter("description");
    String frequency=req.getParameter("frequency");
    String lastModifiedAt=req.getParameter("lastModifiedAt");
    String createdAt=req.getParameter("createdAt"); 
    String createdBy=req.getParameter("createdBy");     
    String env=req.getParameter("env");     
    String opPath=req.getParameter("opPath");       
    res.setContentType("text/html");
    PrintWriter pw=res.getWriter();
    pw.println("JobId: \n"+jobId);
    pw.println("JobStatus: \n"+jobStatus);
    String path = getServletContext().getRealPath("/");
    File logfile=new File("/var/lib/tomcat7/webapps/ROOT/log.txt");
    logfile.setReadable(true);
    logfile.setWritable(true);
    logfile.setExecutable(true);    
    PrintWriter writer = new PrintWriter(path+"log.txt", "UTF-8");
    writer.println("Job ID : "+jobId);
    writer.println("Job Status : "+jobStatus);
    writer.println("Rule Name : "+name);
    writer.println("Rule Display Name : "+displayName);
    writer.println("Rule Description : "+description);
    writer.println("Rule env : "+env);
    writer.println("Rule frequency : "+frequency);
    writer.println("Rule last modified at : "+lastModifiedAt);
    writer.println("Rule created at : "+createdAt);
    writer.println("Rule created by : "+createdBy);
    writer.println("Notification Path : "+opPath);
    writer.close();
        pw.close();
        try
        {
        // ProcessBuilder pb1=new ProcessBuilder("sudo", "/bin/chmod","777", "-R", "/var/lib/tomcat7/webapps/ROOT/log.txt");
        // pb1.redirectErrorStream(true);
        // Process p1=pb1.start();
        // InputStreamReader isr1 = new  InputStreamReader(p1.getInputStream());
        // BufferedReader br1 = new BufferedReader(isr1);
        // String lineRead1;
        // while ((lineRead1 = br1.readLine()) != null) 
        //     {
        //  System.out.println(lineRead1);
        //     }
        // p1.waitFor();
        ProcessBuilder pb2=new ProcessBuilder("/usr/local/hadoop/bin/hadoop", "fs", "-copyFromLocal", "/var/lib/tomcat7/webapps/ROOT/log.txt",opPath);
        pb2.redirectErrorStream(true);
    //  pb2.directory(new File("/var/lib/tomcat7/webapps/ROOT/"));
        Process p2=pb2.start();
        InputStreamReader isr2 = new  InputStreamReader(p2.getInputStream());
        BufferedReader br2 = new BufferedReader(isr2);
        String lineRead2;
        while ((lineRead2 = br2.readLine()) != null) 
            {
            System.out.println(lineRead2);
            }
        p2.waitFor();
        }
    catch(Exception e){}
    }
}

如果有人能帮忙就太好了。

为了方便: 文件权限

File logfile=new File("/var/lib/tomcat7/webapps/ROOT/log.txt");
logfile.setReadable(true);
logfile.setWritable(true);
logfile.setExecutable(true);    

流程构建器:

ProcessBuilder pb2=new ProcessBuilder("/usr/local/hadoop/bin/hadoop", "fs", "-copyFromLocal", "/var/lib/tomcat7/webapps/ROOT/log.txt",opPath);
        pb2.redirectErrorStream(true);
    //  pb2.directory(new File("/var/lib/tomcat7/webapps/ROOT/"));
        Process p2=pb2.start();
        InputStreamReader isr2 = new  InputStreamReader(p2.getInputStream());
        BufferedReader br2 = new BufferedReader(isr2);
        String lineRead2;
        while ((lineRead2 = br2.readLine()) != null) 
            {
            System.out.println(lineRead2);
            }
        p2.waitFor();

【问题讨论】:

  • @hellospk 但它不适用于 Servlet。在 servlet 的情况下是否需要做任何特别的事情,例如使用 String path = getServletContext().getRealPath("/");并在 log.txt 之前添加它作为路径,只是因为我需要它才能写入 log.txt 文件。但这在用于设置权限时也不起作用。
  • jdk-7 可能会解决你的问题journaldev.com/855/…
  • 试过了,权限部分有效,但它仍然没有执行它的 ProcessBuilder 部分。

标签: java servlets file-permissions processbuilder


【解决方案1】:

抱歉,之前无意误导。问题实际上在于hadoop的存储系统(hdfs),我认为这绝对是tomcat或Servlet的问题。我一发现这个问题就想删除这个问题,但后来我认为这会让其他人受益,因为我在 2 天内找不到任何东西。

我终于发现这对我有帮助 Permission denied at hdfs.

-copyFromLocal 或 -put 我想需要有一些权限,只需在 hdfs-site.xml 中添加以下内容,然后运行它就可以了。

 <property>
    <name>dfs.permissions</name>
    <value>false</value>
  </property>

我也在php中尝试过,它比jsp或servlet要麻烦得多。

这就是我最终在php中所做的

<?php
  if( $_GET["jobId"] || $_GET["jobStatus"] )
  {
     echo "Job Id ". $_GET['jobId']. "<br />";
     echo "Job Status ". $_GET['jobStatus']. "";
  }
  $jobId=$_GET["jobId"];
  $jobStatus=$_GET["jobStatus"];
  $displayName=$_GET["displayName"];
  $name=$_GET["name"];
  $description=$_GET["description"];
  $frequency=$_GET["frequestuency"];
  $lastModifiedAt=$_GET["lastModifiedAt"];
  $createdAt=$_GET["createdAt"];    
  $createdBy=$_GET["createdBy"];        
  $opPath=$_GET["opPath"];      
  $env=$_GET["env"];
  $file = fopen("log.txt","w");
  echo fwrite($file,"Job ID : ".$jobId."\n"."Job Status : ".$jobStatus."\n"."Display Name : ".$displayName."\n"."Name : "."\n"."Description : ".$description."\n"."Frequency : ".$frequency."\n"."Last Modified At".$lastModifiedAt."\n"."Created At".$createdAt."\n"."Created By : ".$createdBy);
  fclose($file);
  chmod("log.txt", 0777);
  $last_line = system('/usr/local/hadoop/bin/hadoop fs -put /var/www/html/log.txt /user/hduser/Alert/', $retval);
  $last_line = system('/usr/local/hadoop/bin/hadoop fs -ls /user/hduser/Alert/', $retval);
  echo '<pre>';
  $output2 = exec('hadoop fs -copyFromLocal /var/www/html/log.txt /user/hduser/Alert/Notify');
  echo '
    </pre>
    <hr />' . $last_line . '
    <hr />' . $retval;
        '<hr />' . $output1;

?>

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 2013-06-07
    • 2019-06-18
    • 2015-09-14
    • 2023-04-02
    • 2011-09-01
    • 1970-01-01
    • 2012-02-03
    • 2023-03-08
    相关资源
    最近更新 更多