加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice传输大文件

发布时间:2020-12-17 00:41:19 所属栏目:安全 来源:网络整理
导读: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

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了


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读