我想实现如下功能,提供一个传送文件的webservices服务端,上传的文件的个数不限,这样就需要把客户端上传的文件可以放在一个 List或者其它的集合当中,服务器端再遍历这个List集合就知道客户端上传了多少个文件了
以下是我写的上传单个文件的例子,请高手赐教
package ws;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import javax.activation.DataHandler;
import javax.activation.FileDataSource;import model.SubmitReq;import org.apache.axiom.attachments.utils.IOUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.engine.DefaultObjectSupplier;public class interopService {
public static final String TMP_PATH = "D:/itunes"; public OMElement upload(OMElement element) throws Exception {
OMElement _fileContent = null;
OMElement _mailboxnum = null;
OMElement _fileName = null;
OMElement _fileType = null;
OMElement _submitReq = null;
System.out.println(element);
for (Iterator _iterator = element.getChildElements(); _iterator
.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
_fileContent = _ele;
}
/* if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
_mailboxnum = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
_fileName = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
_fileType = _ele;
}*/
if (_ele.getLocalName().equalsIgnoreCase("submitReq")) {
_submitReq = _ele;
}
} if (_fileContent == null || _mailboxnum == null || _fileName == null
|| _fileType == null || _submitReq == null) {
throw new AxisFault(
"Either Image or FileName is null or _submitReq is null");
} SubmitReq submitReq = getSubmitReq(_submitReq);
System.out.print(submitReq.getTransactionID());
readFile(_fileContent);
// setting response
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace ns = fac.createOMNamespace("http://example.org/mtom/data",
"x");
OMElement ele = fac.createOMElement("response", ns);
ele.setText("true"); return ele;
} private static SubmitReq getSubmitReq(OMElement element) throws AxisFault {
if (element == null) {
return null;
}
SubmitReq submitReq = null;
submitReq = (SubmitReq) BeanUtil.processObject(element,SubmitReq.class, null, true, new DefaultObjectSupplier());
return submitReq;
}
public static void readFile(OMElement _fileContent){
Iterator iterator = _fileContent.getChildren();
int i=0;
while (iterator.hasNext()) {
OMText binaryNode = (OMText) iterator.next();
              i++;
String mboxNum ="makechange";// _mailboxnum.getText();
String fileName =i+".txt"; //_fileName.getText(); String greetingstoreDir = TMP_PATH + "/" + mboxNum;
File dir = new File(greetingstoreDir);
if (!dir.exists()) {
dir.mkdir();
}
String filePath = greetingstoreDir + "/" + fileName;

File greetingFile = new File(filePath);
if (greetingFile.exists()) {
greetingFile.delete();
greetingFile = new File(filePath);
} // Extracting the data and saving
DataHandler actualDH;
actualDH = (DataHandler) binaryNode.getDataHandler(); FileOutputStream imageOutStream;
try {
imageOutStream = new FileOutputStream(greetingFile);

InputStream is = actualDH.getInputStream();
imageOutStream.write(IOUtils.getStreamAsByteArray(is));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}





} private static List getObjList(OMElement element) throws AxisFault {
if (element == null) {
return null;
}
Iterator iterator = element.getChildElements();
List<SubmitReq> list = new ArrayList<SubmitReq>();
while (iterator.hasNext()) {
OMNode omNode = (OMNode) iterator.next();
if (omNode.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement) omNode;
if (omElement.getLocalName().toLowerCase().equals("person")) {
SubmitReq person = (SubmitReq) BeanUtil.processObject(omElement, SubmitReq.class, null, true,new DefaultObjectSupplier());
list.add(person);
}
}
}
return list; } public OMElement download(OMElement element) throws Exception {
System.out.println(element);
OMElement _mailboxnum = null;
OMElement _greetingType = null;
OMElement _fileType = null;
for (Iterator _iterator = element.getChildElements(); _iterator
.hasNext();) {
OMElement _ele = (OMElement) _iterator.next();
if (_ele.getLocalName().equalsIgnoreCase("mailboxnum")) {
_mailboxnum = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("greetingType")) {
_greetingType = _ele;
}
if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
_fileType = _ele;
}
}
String mboxNum = _mailboxnum.getText();
String greetingType = _greetingType.getText();
String fileType = _fileType.getText();
String filePath = TMP_PATH + "/" + mboxNum + "/" + greetingType + "."
+ fileType;
FileDataSource dataSource = new FileDataSource(filePath);
DataHandler expectedDH = new DataHandler(dataSource); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace ns = fac.createOMNamespace("http://example.org/mtom/data",
"x");
OMText textData = fac.createOMText(expectedDH, true);
OMElement ele = fac.createOMElement("response", ns);
ele.addChild(textData);
return ele;
}
}

解决方案 »

  1.   

    客户端代码
    package client;
    import java.io.File;
    import java.io.InputStream;import javax.activation.DataHandler;
    import javax.activation.FileDataSource;import model.SubmitReq;import org.apache.axiom.om.OMAbstractFactory;
    import org.apache.axiom.om.OMElement;
    import org.apache.axiom.om.OMFactory;
    import org.apache.axiom.om.OMNamespace;
    import org.apache.axiom.om.OMText;
    import org.apache.axiom.om.impl.builder.StAXOMBuilder;
    import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
    import org.apache.axiom.soap.SOAP11Constants;
    import org.apache.axis2.Constants;
    import org.apache.axis2.addressing.EndpointReference;
    import org.apache.axis2.client.Options;
    import org.apache.axis2.client.ServiceClient;
    import org.apache.axis2.databinding.utils.BeanUtil;
    import org.apache.axis2.util.StreamWrapper;
    public class FileTransferClient {
       private static EndpointReference targetEPR = new EndpointReference("http://localhost/mm7webservices/services/interopService?wsdl");
       
       public static boolean upload(SubmitReq submitReq , File file1) {
         try {
          OMElement data = buildUploadEnvelope(submitReq , file1);
          Options options = buildOptions();
          ServiceClient sender = new ServiceClient();
          sender.setOptions(options);
          System.out.println(data);
          OMElement ome = sender.sendReceive(data);
          System.out.println(ome);
          String b = ome.getText();
          return Boolean.parseBoolean(b);
         }
         catch(Exception e) {
           
         }
         return false;
       }
      
       
       private static OMElement buildUploadEnvelope(SubmitReq submitReq, File file) {
         DataHandler expectedDH1;
         OMFactory fac = OMAbstractFactory.getOMFactory();
         OMNamespace omNs = fac.createOMNamespace("http://mdc.cn/caixin/data", "x");
         OMElement data = fac.createOMElement("upload", omNs);
         
         OMElement fileContent = fac.createOMElement("fileContent", omNs);
         
        FileDataSource dataSource1 = new FileDataSource(file);
         expectedDH1 = new DataHandler(dataSource1);
         OMText textData1= fac.createOMText(expectedDH1, true);
         fileContent.addChild(textData1);
       
         
         
        /* OMElement mboxnum = fac.createOMElement("mailboxnum", omNs);
         mboxnum.setText(mailboxnum);
         OMElement gtType = fac.createOMElement("fileName", omNs);
         gtType.setText(fileName+"");
         OMElement fileType=fac.createOMElement("fileType", omNs);
         fileType.setText(FileType);*/
       

       
         javax.xml.stream.XMLStreamReader reader = BeanUtil.getPullParser(submitReq);
         StreamWrapper parser = new StreamWrapper(reader);
         StAXOMBuilder stAXOMBuilder =OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), parser);
         OMElement submitMessage =  stAXOMBuilder.getDocumentElement();
         data.addChild(submitMessage);
     /*    data.addChild(mboxnum);
         data.addChild(gtType);
         data.addChild(fileType);*/
         data.addChild(fileContent);
         return data;
       }

       private static Options buildOptions() {
         Options options = new Options();
         options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
         options.setTo(targetEPR);
         // enabling MTOM in the client side
         options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
         options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
         return options;
       }
       public static void main(String agrs[]) {
         String file1 = "D:/opt/bdgdf.txt";
         File smilfile= new File(file1);
         SubmitReq submitReq=new SubmitReq();
         //发送到的号码  
         submitReq.setTransactionID("15221832900");
      /*   submitReq.setVASID("106558815");
    //    ---VASPID,SP代码(必填)
         submitReq.setVASPID("11564");
                   //业务代码(必填)
         submitReq.setServiceCode("31110086");
         // //设置MM始发方的地址  
         submitReq.setSenderAddress("10086");
         //主题
         submitReq.setSubject("经营分析日报");*/
         boolean rtv = upload(submitReq,smilfile);
         System.out.println(rtv);
       //  InputStream is = download(mn,gt,ft);
       }
    }
      

  2.   

    JavaBeanpackage model;import java.util.Date;
    import java.util.List;import org.apache.axiom.om.OMElement;public class SubmitReq { private String TransactionID; public String getTransactionID() {
    return TransactionID;
    }
    public void setTransactionID(String transactionID) {
    TransactionID = transactionID;
    }}