server端:(根据需求写)
接口:
public interface MyFile { public DataHandler downFile(String type,String id); }实现类:
public class MyFileImpl implements MyFile { public DataHandler downFile(String fileName,String id) { Properties p = new Properties(); String path=id+".xml"; File file = null; try { p.load(MyFileImpl.class.getResourceAsstream("/parameter.properties")); } catch (IOException e) { e.printstacktrace(); } if(fileName.equals("0")){ file = new File(p.getProperty("0"),p.getProperty("f0")); } if (fileName.equals("1")) { file = new File(p.getProperty("1"),path); } else if(fileName.equals("2")) { file = new File(p.getProperty("2"),p.getProperty("f2")); }else if(fileName.equals("3")){ file = new File(p.getProperty("3"),p.getProperty("f3")); } DataHandler dh= new DataHandler(new FileDataSource(file)); return dh; } }
配置services.xml(位置在src/meta-inf/xfire下的service.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>FileService</name> <serviceClass>org.file.service.MyFile</serviceClass> <implementationClass> org.file.service.MyFileImpl </implementationClass> <properties> <property key="mtom-enabled">true</property> </properties> </service> </beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class> org.codehaus.xfire.transport.http.XFireConfigurableServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/servlet/XFireServlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <filter> <filter-name>CompressingFilter</filter-name> <filter-class> com.planetj.servlet.filter.compression.CompressingFilter </filter-class> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>statsEnabled</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CompressingFilter</filter-name> <url-pattern>/services/*</url-pattern> </filter-mapping> </web-app>
测试客户端:
public class MyTest { public static void main(String[] args) { Service serviceModle = new ObjectServiceFactory().create(MyFile.class,"FileService","http://service.file.org/FileService",null); MyFile service = null; try { service = (MyFile) new XFireProxyFactory().create(serviceModle,"http://192.168.3.67:8080/MyFile/services/FileService"); Client client = Client.getInstance(service); client.setProperty("mtom-enabled","true"); client.setProperty(HttpTransport.CHUNKING_ENABLED,"true"); } catch (MalformedURLException e) { e.printstacktrace(); } String fileName="3"; System.out.println("开始传输"); File file1 = new File("F://","1dsa.txt"); DataHandler dhl = service.downFile(fileName,null); try { InputStream in = dhl.getInputStream(); FileOutputStream out = new FileOutputStream(file1); file1.createNewFile(); byte[] buff = new byte[1024 * 1024]; int len = -1; while (-1 != (len = in.read(buff,buff.length))) { out.write(buff,len); } in.close(); out.flush(); out.close(); System.out.println("传输完毕"); } catch (IOException e) { e.printstacktrace(); } } }
注意项目中要加入的jar包
如果运行客户端报错; javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParserFactory not found和Exception in thread "main" org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. nested exception is org.codehaus.xfire.fault.XFireFault: Fault: java.lang.NullPointerException
org.codehaus.xfire.fault.XFireFault: Fault: java.lang.NullPointerException
看过论坛上有很多帖子对于这个错误都没有解决办法,那我在这里悄悄的告诉你,少包了。少下面这2个包(红框圈住的) 导入这2个包就OK了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。