从当前包中获取文件

发布于 2024-12-11 17:44:10 字数 2608 浏览 0 评论 0原文

该代码在我的本地运行良好,但当我们使用竹子构建时给出文件未找到异常。有什么想法/解决方法吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-12-18 17:44:10

没有可靠的方法来引用相对于项目根文件夹的文件。

您需要将此文件作为资源引用。据我所知,您使用 Maven。如果是这样,您需要将此文件放入 /src/test/resources 而不是 /src/test/java (也许您也可以配置 Maven 从 /src/test/resources 获取资源code>/src/test/java,但这会违反 Maven 目录布局约定)。

之后,您可以加载此文件,

InputStream fis = getClass()
    .getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");

或者,如果当前类位于 com.statement 包中,则为

InputStream fis = getClass().getResourceAsStream("SamplePDFStatementFile.txt");

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

InputStream fis = getClass()
    .getResourceAsStream("/com/statement/SamplePDFStatementFile.txt");

Or, if the current class is in the com.statement package, as

InputStream fis = getClass().getResourceAsStream("SamplePDFStatementFile.txt");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文