在Junit中使用getBytes设置setAttachment

时间:2014-01-07 08:13:49

标签: java junit

以下是我用于junit保存附件的代码。它保存在数据库中,但是当我浏览应用程序时收到错误消息,例如“无法打开'test109.pdf',因为它不支持文件类型或可能已损坏”。其他文件类型也是如此。在这里使用 .getBytes 有什么问题吗?

@Test 
public void saveAttachment() throws SerException
{

final File blobIn = new File("F:\\src\\test4.pdf");
    InputStream blobIs = null;
    try {
        blobIs = new FileInputStream(blobIn);   
AttachmentReqDocument attachmentReqDocument = AttachmentReqDocument.Factory.newInstance();
    AttachmentReq attachmentReq = attachmentReqDocument.addNewSaveAttachmentReq();
    attachmentReq.setSuId("105"); 
    attachmentReq.setTId("104532520"); 
    attachmentReq.setFileName("test109.pdf"); 
    attachmentReq.setAttachment("test112".getBytes()); 
    attachmentReq.setUserId("test004"); 
    attachmentReqDocument.getSaveAttachmentReq().setInUserId(null);
    AttachmentResDocument attachmentResDocument =       
            testManager.saveTestAttachment(attachmentReqDocument);
    System.out.println(attachmentResDocument);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        System.out.println("File not found");
    }catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("Error Reading the bytes");
    }
}

您的任何帮助都非常感谢:)
提前谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您想要附加blobIs inputStream中的字节而不是"test112".getBytes(),这显然不是有效的pdf编码数据。