论坛首页 Java企业应用论坛

Servlet中使用File类出现问题..

浏览 1194 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2009-10-12  
OO
我在servlet的doGet方法中创建文件,为何建立不了呢?谢谢..

doGet方法的代码如下:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
File file=new File("ere","abcd.txt");
out.print("abc:");
out.println(file.getAbsolutePath());
//out.println(file.getName().toString());

file.createNewFile();

out.flush();
out.close();
}
错误提示如下:
java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:850)
at com.servlet.ListFold.doGet(ListFold.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:595)
   发表时间:2009-10-12  
主要是因为创建文件时,ere目录没有建立。

修改如下:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 直接获取目录,则将会到web服务器默认的目录下去找父目录。比如这里的ere目录,如果使用的是
// tomcat服务器,则会到tomcat/bin/目录下去找ere目录,如果找不到则报错。

// 一般在web应用中,都是获得应用的根然后再,考虑建立文件存储目录
String webHome = request.getSession().getServletContext().getRealPath(
"/");
String realPath = webHome + "ere";

File dir = new File(realPath);
if (dir.isDirectory()) {
createFile(out,realPath,"abcd.txt");
}else{
dir.mkdir();
createFile(out,realPath,"abcd.txt");
}
out.flush();
out.close();
}
---


/**
* 功能:建立文件
* @param out
* @param realPath
* @param fileName
*/
private void createFile(PrintWriter out,String realPath,String fileName){

File file = new File(realPath, fileName);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print("abc:");
out.println(file.getAbsolutePath());


}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics