![](/img/trans.png)
[英]Eclipse Not Finding Classes added to the build Path (Java Dynamic Web Project)
[英]No file path in dynamic web project in java eclipse
我在Eclipse中有一个动态的Web项目。 当我只执行WordFreq.java时,它可以工作。 如果要在MyServlet.java> doGet方法中执行相同的代码,则会出现以下错误:HTTP状态500-text.txt(系统找不到指定的文件)。 我到处都复制了文件(text.txt):
仍然出现此错误。
注意:我的工作区在D:/ ....中,而我的Apache服务器在C:/ ...中,我试图更改为完整路径,但仍然无法正常工作。
WordFreq.java
package cpd;
import java.io.*;
import java.util.Scanner;
public class WordFreq {
public static void main(String[] args) throws IOException {
File file1 = new File("text.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
String content = new Scanner(file1).useDelimiter("\\Z").next();
CezarBun cezar = new CezarBun();
System.out.println(cezar.criptare(content, 3));
System.out.println("Frecventa literelor");
System.out.println();
int nextChar;
char ch;
double[] count = new double[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') {
count[ch - 'a']++;
}
}
for (int i = 0; i < 26; i++) {
System.out.printf("%c %d", i + 'a', (int) count[i]);
System.out.println("Frecventa: " + count[i] / file1.length() * 100 + "%");
}
in.close();
try {
/*create a buffered reader that connects to the console, we use it so we can
read lines*/
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in2.readLine();
//create an print writer for writing to a file
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
//output to the file a line
out.println(lineFromInput);
//close the stream
out.close();
} catch (IOException e1) {
System.out.println("Error during reading/writing");
}
}
}
MyServlet.java
package cpd;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("A pornit servlet-ul" + "<br />");
//inceput wordfreq
File file1 = new File("text.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
String content = new Scanner(file1).useDelimiter("\\Z").next();
CezarBun cezar = new CezarBun();
System.out.println(cezar.criptare(content, 3));
System.out.println("Frecventa literelor");
System.out.println();
int nextChar;
char ch;
double[] count = new double[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') {
count[ch - 'a']++;
}
}
for (int i = 0; i < 26; i++) {
System.out.printf("%c %d", i + 'a', (int) count[i]);
System.out.println(" Frecventa: " + count[i] / file1.length() * 100 + "%");
}
in.close();
try {
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in2.readLine();
//create an print writer for writing to a file
PrintStream out2 = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out2);
//output to the file a line
out2.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out2.close();
} catch (IOException e1) {
System.out.println("Error during reading/writing");
}
//sfarsit wordfreq
// freq = new WordFreqBun();
//freq.wf();
out.print("<br />");
out.print("<a href='http://localhost:8080/CpdApplication/'>Inapoi la meniu</a>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
您可以使用ServletContext.getResourceAsStream()作为资源来访问文件,该文件可以位于Web应用程序中的任何位置。 如果您不希望浏览器访问它,建议将其保存在/ WEB-INF目录下。
ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/file.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
ServletContext.getResource() :返回映射到给定路径的资源的URL。
ServletContext.getResourceAsStream() :以InputStream对象的形式返回位于指定路径处的资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.