我通过此下方法去发送一个文件的附件,但是客户端却接收不到附件,请大家帮个忙
服务端:
FileInputStream fis = new FileInputStream(file);
int cnt = 0;
byte[] fileContent = new byte[1024*1024];

int rand = 0; while ((cnt = fis.read(fileContent)) > -1) {
if ( rand == this.num ){
break;
}
      rand++;
      isNewFile = false;
}
if ( cnt < 0 ){
isTransformer = true ;
}

Name hAttFileName = env.createName("FileName");
env.addAttribute(hAttFileName, file.getName());
Name hAttNewFile = env.createName("IsNewFile");
env.addAttribute(hAttNewFile, Boolean.toString(isNewFile));
Name hAttFileTransformerSuccess = env.createName("FileTransformerSuccess");
env.addAttribute(hAttFileTransformerSuccess,Boolean.toString(isTransformer)); if ( cnt > -1 ){
//创建临时文件
    File temp = new File( "C:\\"+ UUID.getUID()+".dat");
    FileOutputStream fos = new FileOutputStream(temp);
    fos.write(fileContent,0,cnt);
    fos.close();
    
//     将临时文件写入SOAP的附件
    FileDataSource fds = new FileDataSource(temp);
    DataHandler dh = null;       try {
dh = new DataHandler(fds,"text/plain");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    AttachmentPart ap1 = msg.createAttachmentPart(dh);
//     ap1.setContentType("text/xml");
//     ap1.setContentType("multipart/*");
    ap1.setContentId(file.getName());
    msg.addAttachmentPart(ap1);
// 删除临时文件
//     temp.delete();
  }
    msg.saveChanges();
}客户端接收
Name hAttFileName = envelope.createName("FileName");
Name hAttIsNewFile = envelope.createName("IsNewFile");
Name FileTransformerSuccess = envelope
.createName("FileTransformerSuccess");
// Name hAttWorkItemID = envelope.createName("WorkItemID");
// Name hAttSendObjIID = envelope.createName("SendObjIID");
// Name hAttSendObjName = envelope.createName("SendObjName");
// Name hAttUserLoginID = envelope.createName("UserLoginID");// String dataURL = "";
String fileName = "";
String isNewFile = "";
String transformerF = "";
// String WorkItemID = "";
// String SendObjIID = "";
// String SendObjName = "";
// String UserLoginID = "";
// String BusinessType = "";
// String ProductID = ""; fileName = envelope.getAttributeValue(hAttFileName);
isNewFile = envelope.getAttributeValue(hAttIsNewFile);
transformerF = envelope.getAttributeValue(FileTransformerSuccess);
// WorkItemID = envelope.getAttributeValue(hAttWorkItemID);
// SendObjIID = envelope.getAttributeValue(hAttSendObjIID);
// SendObjName = envelope.getAttributeValue(hAttSendObjName);
// UserLoginID = envelope.getAttributeValue(hAttUserLoginID); // 如果transformerF 的值为true说明文件已经传输完毕,将文件写入数据库
if ("true".equals(transformerF)) {
sFileName = sFilePath + File.separator + fileName;
} else {

java.util.Iterator iterator = message.getAttachments();
while (iterator.hasNext()) {
Object obj = iterator.next();
if (obj instanceof AttachmentPart) {
AttachmentPart ap1 = (AttachmentPart) obj; DataHandler dh = ap1.getDataHandler();
int packagesize = ap1.getSize();
byte[] b = new byte[packagesize];
dh.getInputStream().read(b);
File directory = new File(sFilePath);
if (!directory.exists())
directory.mkdirs();
File file = new File(sFilePath + File.separator
+ fileName);
if (("true".equals(isNewFile))) {
if (file.exists()) {
file.delete();
}
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(file.length());
raf.write(b);
raf.close();
} else {
System.out.println("没有附件!");
}
}
}
其中java.util.Iterator iterator = message.getAttachments();这个地方始终接收不到附件,其他都能接收到,不知道为什么,谢谢各位