【发布时间】:2015-02-16 18:25:00
【问题描述】:
好的。我需要修改我的问题。我之前的问题太模糊,无法显示我的问题。
上面的屏幕截图显示了我的动态 Web 项目。它有几个 Java 代码,可以独立运行。
另外,我有 'index.jsp' 文件,在里面:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PFBankingSystem</title>
</head>
<body>
<%
PFSystemTemplate PF = new PFSystemTemplate();
PF.init();
PF.filterConnection();
PF.filterExecution();
%>
PF...blahblah 代码与最初在 main 函数中的代码相同。因此,单击“在服务器上运行”意味着仅使用 .jsp 运行我原来的独立程序。 (只是使用jsp作为控制台类似,以后会添加更多接口。)
然后,我的原始 java 程序尝试使用 Java 文件 I/O 读取“BankingDataExample.dat”。 (你可以在上面的截图中找到这个文件。我只是把它放在项目根文件夹和 WebContent 文件夹中。)
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(Constant.BANKING_DATA_FILE)));
...
public static final String BANKING_DATA_FILE = "BankingDataExample.dat";
然后,我的程序一直说找不到“BankingDataExample.dat”。
我不知道如何使用servlet,但如果使用servlet 是唯一的解决方案,我会尝试。但此时,我只想找到可以放置输入文件的合适位置。
========================为了提供更多信息,我放了两个完整的源码=============
SourceFilter.java
public class SourceFilter extends PFGeneralFilter {
public SourceFilter() {
super();
}
@Override
public void compute() throws EndOfStreamException {
try {
BufferedReader brForBankingData = new BufferedReader(new FileReader(new File(Constant.BANKING_DATA_FILE)));
String bankingDatum = "";
while((bankingDatum = brForBankingData.readLine()) != null) {
this.outPorts[Constant.DEFAULT_PORT].write(Utility.convertStringToByteArray(bankingDatum));
}
brForBankingData.close();
throw new EndOfStreamException();
} catch (IOException e) {
throw new EndOfStreamException();
}
}
}
PFGeneralFilter.java
public abstract class PFGeneralFilter implements Runnable, PFFilterInterface {
protected PipedInputStream[] inPorts;
protected PipedOutputStream[] outPorts;
public PFGeneralFilter() {
this.inPorts = new PipedInputStream[]{new PipedInputStream()};
this.outPorts = new PipedOutputStream[]{new PipedOutputStream()};
}
public PFGeneralFilter(int numberOfInputPorts, int numberOfOutputPorts) {
this.inPorts = new PipedInputStream[numberOfInputPorts];
for(int i = 0; i < numberOfInputPorts; i++)
this.inPorts[i] = new PipedInputStream();
this.outPorts = new PipedOutputStream[numberOfOutputPorts];
for(int i = 0; i < numberOfOutputPorts; i++)
this.outPorts[i] = new PipedOutputStream();
}
private void closePorts() {
try {
for(int i = 0; this.inPorts != null && i < this.inPorts.length; i++)
this.inPorts[i].close();
for(int i = 0; this.outPorts != null && i < this.outPorts.length; i++)
this.outPorts[i].close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true) {
try {
compute();
} catch (Exception e) {
if (e instanceof EndOfStreamException)
return;
}
}
}
@Override
public void connect(int indexForOutputPortInThisFilter, PipedInputStream connectedInputPortInNextFilter) throws IOException {
this.outPorts[indexForOutputPortInThisFilter].connect(connectedInputPortInNextFilter);
}
@Override
public PipedInputStream getInputPort(int index) {
return this.inPorts[index];
}
@Override
public PipedOutputStream getOutputPort(int index) {
return this.outPorts[index];
}
protected class EndOfStreamException extends Exception {
private static final long serialVersionUID = 1L;
public EndOfStreamException() {
closePorts();
}
}
abstract public void compute() throws EndOfStreamException;
}
【问题讨论】:
-
请记住 WEB-INF 是完全私有的,不能用于访问除 Web 资源以外的任何内容,在您的情况下,它是一个简单的 java 文件,您不能从中访问 WEB-INF。
-
所以要么使用 servlet,要么将要访问的文件放在类路径或 java 文件的同一个包中
标签: java eclipse jsp jakarta-ee tomcat7