webservice 上传下载 大文件

服务端:

package oa.webservice.Server;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

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.OMText;
import org.apache.axis2.AxisFault;

public class FileTransferServer {
    public static final String TMP_PATH = "C://temp//";

    public OMElement upload(OMElement element) throws Exception {
            OMElement _fileContent = null;//文件内容
            OMElement _fileName = null;//文件名
            OMElement _fileType = null;//文件类型
            //System.out.println("The element for upload: " + element);
            for (Iterator _iterator = element.getChildElements(); _iterator
                          .hasNext();) {
                  OMElement _ele = (OMElement) _iterator.next();
                  if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
                          _fileContent = _ele;
                  }
                  if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
                          _fileName = _ele;
                  }
                  if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
                          _fileType = _ele;
                  }
            }

            if (_fileContent == null || _fileType == null) {
                  throw new AxisFault("Either Image or FileName is null");
            }

            OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
            String fileName = _fileName.getText();
            String fileType = _fileType.getText();
            String storeDir = TMP_PATH + "//" + "velmaUpload";
            File dir = new File(storeDir);
            if (!dir.exists()) {
                  dir.mkdir();
            }
            String filePath = storeDir + "/" + fileName + "." + fileType;
            File uploadFile = new File(filePath);
            if (uploadFile.exists()) {
                  filePath = storeDir + "/" + fileName + "(1)" + "." + fileType;
                  uploadFile = new File(filePath);
            }

            // Extracting the data and saving
            DataHandler actualDH;
            actualDH = (DataHandler) binaryNode.getDataHandler();

            FileOutputStream imageOutStream = new FileOutputStream(uploadFile);
            //add by wangx 20090625 支持大文件上传,避免不能读数据
            actualDH.writeTo(imageOutStream);
            //删除该段代码,支持大文件上传
            //InputStream is = actualDH.getInputStream();
            //imageOutStream.write(IOUtils.getStreamAsByteArray(is));
            // setting response
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
                          "fd");
            OMElement ele = fac.createOMElement("response", ns);
            ele.setText("true");
            return ele;
    }

    public OMElement download(OMElement element) throws Exception {
            //System.out.println("The element for download: " + element);
            OMElement _userName = null;
            OMElement _fileName = null;
            OMElement _fileType = null;
            for (Iterator _iterator = element.getChildElements(); _iterator
                          .hasNext();) {
                  OMElement _ele = (OMElement) _iterator.next();
                  if (_ele.getLocalName().equalsIgnoreCase("userName")) {
                          _userName = _ele;
                  }
                  if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
                          _fileName = _ele;
                  }
                  if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
                          _fileType = _ele;
                  }
            }
            String userName = _userName.getText();
            String fileName = _fileName.getText();
            String fileType = _fileType.getText();
            String filePath = TMP_PATH + "/" + userName + "/" + fileName + "."
                          + fileType;
            //System.out.println("The filePath for download: " + filePath);
            FileDataSource dataSource = new FileDataSource(filePath);
            DataHandler expectedDH = new DataHandler(dataSource);
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
                          "fd");
            OMText textData = fac.createOMText(expectedDH, true);
            OMElement ele = fac.createOMElement("response", ns);
            ele.addChild(textData);
            return ele;
    }
}

 

客户端:

package com.webservice.Client;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
 
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
 
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.OMText;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import com.transaction.DBUtil;
 
public class FileTransferClient {
 
 static String IP=DBUtil.getResult("select serverip from xt_serverset");
 
 static String targetEndpoint="http://"+IP+"/axis2/services/FileOperation";
 //"http://192.168.1.58:8888/axis2/services/FileOperation"
   private static EndpointReference targetEPR =
 new EndpointReference(targetEndpoint);
 
  
   public static boolean upload(String fileName, File file, String fileType) {
     try {
       OMElement data = buildUploadEnvelope(fileName, file, fileType);
       Options options = buildOptions();
       ServiceClient sender = new ServiceClient();
       sender.setOptions(options);
       //System.out.println("The data in method upload: "+data);
       OMElement ome = sender.sendReceive(data);
       //System.out.println("Convert the data to element in method upload: "+ome);
       String b = ome.getText();
       System.out.println(b);
       //return Boolean.parseBoolean(b);
       return true;
     }
     catch(Exception e) {
       e.printStackTrace();
     }
     return false;
   }

   public static boolean download(String userName, String fileName, String fileType) {
     try {
       OMElement data = buildDownloadEnvelope(userName, fileName, fileType);
       Options options = buildOptions();
       ServiceClient sender = new ServiceClient();
       sender.setOptions(options);
       System.out.println("The data in method download: "+data);
       OMElement ome = sender.sendReceive(data);
       System.out.println("Convert the data to element in method download: "+ome);
       OMText binaryNode = (OMText) ome.getFirstOMChild();
       binaryNode.setOptimize(true); //必须加此句,否则会出现ContentID is null的异常!
       DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
       FileOutputStream imageOutStream = new FileOutputStream("C://temp//return//steal.xml");
       InputStream is = actualDH.getInputStream();
       imageOutStream.write(IOUtils.getStreamAsByteArray(is));
       return true;
      }
      catch(Exception e) {
        e.printStackTrace();
      }
     return false;
   }
 
   private static OMElement buildUploadEnvelope(String fileName, File file, String fileType) {
     DataHandler expectedDH;
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");
     OMElement data = fac.createOMElement("upload", omNs);
     OMElement fileContent = fac.createOMElement("fileContent", omNs);
     FileDataSource dataSource = new FileDataSource(file);
     expectedDH = new DataHandler(dataSource);
     OMText textData = fac.createOMText(expectedDH, true);
     fileContent.addChild(textData);
     OMElement _fileName = fac.createOMElement("fileName", omNs);
     _fileName.setText(fileName);
     OMElement _fileType = fac.createOMElement("fileType", omNs);
     _fileType.setText(fileType);
     data.addChild(_fileName);
     data.addChild(_fileType);
     data.addChild(fileContent);
     return data;
   }
 
   private static OMElement buildDownloadEnvelope(String userName, String fileName, String fileType) {
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");
     OMElement data = fac.createOMElement("download", omNs);
     OMElement _userName = fac.createOMElement("userName", omNs);
     _userName.setText(userName);
     OMElement _fileName = fac.createOMElement("fileName", omNs);
     _fileName.setText(fileName);
     OMElement _fileType=fac.createOMElement("fileType", omNs);
     _fileType.setText(fileType);
     data.addChild(_userName);
     data.addChild(_fileName);
     data.addChild(_fileType);
     return data;
   }
   private static Options buildOptions() throws AxisFault {
     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 file = "C://temp//velmaUpload//1.zip";
      String fn = "1";  //文件名称
      String ft="zip"; //文件类型
      boolean rtv = upload(fn,new File(file),ft);
      System.out.println("is upload success: "+rtv); */
     //下载
    //boolean rtv = download("return","steal","xml");
      //System.out.println("is download success: "+rtv);

   }
}

具体有问题可以 联系我 zjutsoft@163.com

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值