问题
I have tried coding a servlet which creates a file based on the inputs given to it.The file gets created along with appropriate text but I am unable to give the file 777 permissions and I am also unable to run ProcessBuilder later. I thought it is inter related because the command fired in ProcessBuilder would require the file to have appropriate permissions but when I try giving it permissions using chmod it doesn't work either.
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){}
}
}
It would be really great if anyone could help.
For Convenience : File Permissions
File logfile=new File("/var/lib/tomcat7/webapps/ROOT/log.txt");
logfile.setReadable(true);
logfile.setWritable(true);
logfile.setExecutable(true);
ProcessBuilder :
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();
回答1:
Sorry About unintentionally misguiding earlier. The problem actually lied in hadoop's storage system (hdfs) and I thought it was definitely a problem with tomcat or Servlets. I thought of deleting the question as soon as I found this out but then I thought that It would benefit someone else , as I could not find anything on it for 2 days.
I found this to help me finally Permission denied at hdfs.
-copyFromLocal or -put needs to have some permissions I suppose and just adding the following in hdfs-site.xml and then running it worked.
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
Also I tried the same in php and it is much less of a hassle than jsp or servlet.
This is what I did finally in 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;
?>
来源:https://stackoverflow.com/questions/24883364/giving-file-permissions-and-running-processbuilder-in-java-servlets