이 코드는 로컬에서 잘 실행되지만 bamboo를 사용하여 빌드 할 때 파일을 찾을 수 없습니다. 예외가 발생합니다. 어떤 아이디어/해결 방법?현재 패키지에서 파일 가져 오기
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을 사용하는 경우 파일 객체로 변환하는 방법을 모르겠습니다.
OK .. 이제 사용하고 있습니다. 보이는 그 대답 :)
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();
}
당신 * 구축 * 그것 또는이 파일을로드 할 수 있습니다 그 후
너 * 달리는거야? –
루트 패키지 ('com')에서 시작하는 경로를 사용하여 클래스 로더 (컨텍스트 클래스 로더 또는 현재 클래스를로드 한 클래스 로더)에서'getResource' 메소드를 사용하는 방법을 참조하십시오. 당신은 절대 경로를 사용하고 있습니다. –
또한 파일이 SRC 폴더 아래에있는 것처럼 보입니다. 일반적으로 SRC 폴더가 제외되어 빌드됩니다. 확인할 수 있습니까? – r0ast3d