从当前包中获取文件
该代码在我的本地运行良好,但当我们使用竹子构建时给出文件未找到异常。有什么想法/解决方法吗?
final static String FILE_NAME ="/src/test/java/com/statement/SamplePDFStatementFile.txt";
file = new File(FILE_NAME);
FileInputStream fis = new FileInputStream(file);
我基本上想测试一个读取文件的类。这是完整的代码。
//Main Class
public class PdfRenderer {
public void render(PdfFile pdfFile) throws IOException {
FileInputStream fis = new FileInputStream(pdfFile.getFile());
final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.setHeader("Content-Disposition", " attachment; filename=" + pdfFile.getAttachmentName());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = fis.read(buff, 0, buff.length))) {
response.getOutputStream().write(buff, 0, bytesRead);
}
response.getOutputStream().flush();
}
}
//Test
public class PdfRendererTest{
PdfFile pdfFile;
File file;
@Test
public void test_DetailStatementsByAccountNumber() throws Exception {
new AbstractSeamTest.ComponentTest() {
PdfRenderer actionBean = new PdfRenderer();
URL url = this.getClass().getResource("/SamplePDFStatementFile.txt");
final String FILE_NAME = url.getFile();
protected void testComponents() throws Exception {
file = new File(FILE_NAME);
context.checking(new Expectations() {{
one(pdfFile).getFile();
will(returnValue(file));
one(pdfFile).getAttachmentName();
will(returnValue(file.getName()));
}});
actionBean.render(pdfFile);
}
}.run();
}
}
我想设置 pdfFile.getFile() 返回 SamplePDFStatementFile.txt 的期望。如果我使用 getResourceAsStream,我不确定如何将其转换为文件对象。
好的..所以现在我正在使用。看起来这就是答案:)
public void inputStreamToFile() throws Exception{
InputStream inputStream = this.getClass().getResourceAsStream("/SamplePDFStatementFile.txt");
file = File.createTempFile("abc","def");
OutputStream out = new FileOutputStream(file);
int length = 0;
byte[] bytes = new byte[1024];
while ((length = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, length);
}
inputStream.close();
out.flush();
out.close();
}
This code runs well in my locally but gives file not found exception when we build using bamboo. Any idea/workaround?
final static String FILE_NAME ="/src/test/java/com/statement/SamplePDFStatementFile.txt";
file = new File(FILE_NAME);
FileInputStream fis = new FileInputStream(file);
I basically want to test a class which reads a file. Here is the Complete Code.
//Main Class
public class PdfRenderer {
public void render(PdfFile pdfFile) throws IOException {
FileInputStream fis = new FileInputStream(pdfFile.getFile());
final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.setHeader("Content-Disposition", " attachment; filename=" + pdfFile.getAttachmentName());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = fis.read(buff, 0, buff.length))) {
response.getOutputStream().write(buff, 0, bytesRead);
}
response.getOutputStream().flush();
}
}
//Test
public class PdfRendererTest{
PdfFile pdfFile;
File file;
@Test
public void test_DetailStatementsByAccountNumber() throws Exception {
new AbstractSeamTest.ComponentTest() {
PdfRenderer actionBean = new PdfRenderer();
URL url = this.getClass().getResource("/SamplePDFStatementFile.txt");
final String FILE_NAME = url.getFile();
protected void testComponents() throws Exception {
file = new File(FILE_NAME);
context.checking(new Expectations() {{
one(pdfFile).getFile();
will(returnValue(file));
one(pdfFile).getAttachmentName();
will(returnValue(file.getName()));
}});
actionBean.render(pdfFile);
}
}.run();
}
}
I want to set an expectation that pdfFile.getFile() returns SamplePDFStatementFile.txt. If I use getResourceAsStream, am not sure how to convert it to a File Object.
OK.. so now I am using. Looks like thats the answer :)
public void inputStreamToFile() throws Exception{
InputStream inputStream = this.getClass().getResourceAsStream("/SamplePDFStatementFile.txt");
file = File.createTempFile("abc","def");
OutputStream out = new FileOutputStream(file);
int length = 0;
byte[] bytes = new byte[1024];
while ((length = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, length);
}
inputStream.close();
out.flush();
out.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有可靠的方法来引用相对于项目根文件夹的文件。
您需要将此文件作为资源引用。据我所知,您使用 Maven。如果是这样,您需要将此文件放入
/src/test/resources
而不是/src/test/java
(也许您也可以配置 Maven 从/src/test/resources
获取资源code>/src/test/java,但这会违反 Maven 目录布局约定)。之后,您可以加载此文件,
或者,如果当前类位于
com.statement
包中,则为There are no reliable ways to reference files relative to the project root folder.
You need to reference this file as a resource instead. As far as I see, you use Maven. If so, you need to put this file into
/src/test/resources
rather than/src/test/java
(perhaps you can also configure Maven to get resources from/src/test/java
, but it would be a violation of Maven directory layout convention).After that you can load this file as
Or, if the current class is in the
com.statement
package, as