[英]Permission denied issue while writing file using servlets
I'm trying to write a file in dopost of servlet. 我正在尝试在servlet的dopost中写入文件。 I have a jsp file that send two variables to servlet in doPost method. 我有一个jsp文件,该文件将两个变量发送到doPost方法中的servlet。 and the servlet writes these variables in the file as follows: servlet将这些变量写入文件,如下所示:
<form action="Client" method="POST"> name <input type="text" size="20px" name="name1" ><br/> name2 <input type="text" size="20px" name="name2"><br/> <input type="submit" value="submit"> </form>
My doPost is 我的doPost是
String name1 = request.getParameter("name1");
String name2 = request.getParameter("name2");
BufferedWriter output = null;
try {
File doc = new File("/home/username/Desktop/file.txt");
output = new BufferedWriter(new FileWriter(doc));
output.write(name1+" "+name2);
response.getWriter().append("File saved!");
} catch ( IOException e ) {
response.getWriter().append("Exception"+e.getMessage());
} finally {
if ( output != null ) output.close();
}
I created a war
file and i put this war in my /var/lib/tomcat7/webapps/
after execution i got an exception 我创建了一个war
文件,执行此操作后,将其放置在我的/var/lib/tomcat7/webapps/
/home/username/Desktop/file.txt (Permission denied)
Why Permission denied issue is occuring ? 为什么会出现“权限被拒绝”问题?
Try this: 尝试这个:
String filename = getServletContext().getRealPath("/") + "filename.txt";
File file = new File(filename);
This will create a file names filename.txt in your webapps.context. 这将在您的webapps.context中创建一个文件名filename.txt。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.