pom文件jar包添加:
一般的webservice jar包这里不写了,有两个传输文件需要的jar包:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
服务端代码:
package com.baosight.wzplat.mm.wo.common.wms;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import javax.activation.DataHandler;
import org.apache.commons.lang.StringUtils;
import com.baosight.iplat4j.core.data.id.UUIDHexIdGenerator;
import com.baosight.iplat4j.core.util.DateUtils;
import com.baosight.iplat4j.eu.dm.util.FileUploadConstant;
import com.baosight.iplat4j.eu.dm.util.IFileUploader;
import com.baosight.wzplat.lm.sp.common.JSONUtil;
import com.baosight.wzplat.mm.wi.common.BeanFactoryUtils;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class webServiceUpload {
private static final String FAIL = "-1";
private static final String SUCCESS = "1";
private static final String dirEname = "A";
private static final String docTag = "hk_file11";
private IFileUploader fileUploader = (IFileUploader) BeanFactoryUtils.getBeanById("platFileUploader");
public String upload(DataHandler handler, String fileName, String json_str) throws Exception {
if (StringUtils.isBlank(fileName) || StringUtils.isBlank(json_str))
return FAIL;
// String dirEname = "A";
// String docTag = "hk_file11";
InputStream inputStream = null;
inputStream = handler.getInputStream();
Map<String, String> pathMap = fileUploader.getRealPath(dirEname);
String dirId = pathMap.get("dirId");
String realPath = pathMap.get("path");
String docName = fileName;
String chgName = fileUploader.transformFileName(docName);
String loginId = "admin";
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdirs();
}
String docId = UUIDHexIdGenerator.generate().toString();
Map map = new HashMap();
map.put(FileUploadConstant.DOC_ID, docId);
map.put(FileUploadConstant.DIR_ID, dirId);
map.put(FileUploadConstant.DOC_TAG, docTag);
map.put(FileUploadConstant.DOC_NAME, docName);
map.put(FileUploadConstant.CHG_NAME, chgName);
int docSize = inputStream.available();
map.put(FileUploadConstant.DOC_SIZE, docSize);
map.put(FileUploadConstant.REC_CREATOR, loginId);
map.put(FileUploadConstant.REC_CREATE_TIME, DateUtils.curDateTimeStr14());
fileUploader.uploadDoc(map);
String attachmentName = realPath + "/" + chgName;
File ff = new File(attachmentName);
FileOutputStream fileOut = null;
fileOut = new FileOutputStream(ff);
byte[] b = new byte[1024];
int len = 0;
while ((len = inputStream.read(b)) != -1)
fileOut.write(b, 0, len);
if (fileOut != null)
fileOut.close();
if (inputStream != null)
inputStream.close();
saveDocId(json_str, docId);
return SUCCESS;
}
private String saveDocId(String json_str, String docId) throws Exception {
Map m = JSONUtil.parseJSON2Map(json_str);
Class<?> clazz = Class.forName(m.get("className").toString());
Method method = clazz.getMethod(m.get("methodName").toString(), String.class);
method.setAccessible(true);
m.put("docId", docId);
json_str = JSONUtil.toJSON(m);
return method.invoke(clazz.newInstance(), json_str).toString();
}
}
客服端测试代码:
package com.baosight.wzplat.mm.wo.common.wms;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
public class Test {
public static void main(String[] args) throws ServiceException, RemoteException {
String url = "http://localhost:8089/iPlat4j6/services/UploadFile";
//String fileName = "readMe.txt";
String path = "D:/新建文本文档5.txt";
System.out.println(path);
// 这样就相当于构造了一个带文件路径的File了
DataHandler handler = new DataHandler(new FileDataSource(path));
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
/**
* 注册异常类信息和序列化类 ns:FileUploadHandler 和 wsdd
* 配置文件中的typeMapping中的xmlns:hns="ns:FileUploadHandler" 的对应 DataHandler 和
* wsdd 配置文件中的typeMapping中的qname="hns:DataHandler"的DataHandler对应
*/
QName qn = new QName("ns:FileUploadHandler", "DataHandler");
call.registerTypeMapping(DataHandler.class, qn, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class);
call.setOperationName(new QName(url, "upload"));
// 设置方法形参,注意的是参数1的type的DataHandler类型的,和上面的qn的类型是一样的
call.addParameter("handler", qn, ParameterMode.IN);
call.addParameter("fileName", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("json_str", XMLType.XSD_STRING, ParameterMode.IN);
// 设置返回值类型,下面2种方法都可以
call.setReturnClass(String.class);
// call.setReturnType(XMLType.XSD_STRING);
String result = (String) call.invoke(new Object[] { handler, "test56.txt"," {\"className\":\"com.baosight.wzplat.mm.wo.common.wms.TestUpdateId\",\"methodName\":\"updateDocId\",\"recId\":\"201807120008\"}" });
System.out.println(result);
}
}