XFire中进行文件下载

下面是Web服务端的代码

首先是web.xml和services.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app> <!-- 上面的是web.xml内容 --> <!-- 下面的是services.xml内容 --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>XFireDownloadDemo</name> <namespace>http://www.jadyer.com/XFireDownloadDemo</namespace> <serviceClass>com.jadyer.server.FileDownload</serviceClass> <implementationClass>com.jadyer.server.FileDownloadImpl</implementationClass> </service> </beans>

然后是Web服务端暴露的接口及其实现类

package com.jadyer.server; public interface FileDownload { public String download(String fileName); } /** * Web服务端暴露的接口及其实现类 */ package com.jadyer.server; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.codehaus.xfire.util.Base64; public class FileDownloadImpl implements FileDownload { public String download(String fileName) { String returnValue = null; try { File dir = new File("D://test//"); if (!dir.exists()) { // dir.mkdir(); return "Error: 目录不存在"; } File tmp = new File(dir, fileName); if (tmp.exists()) { InputStream is = new FileInputStream(tmp); // Get the size of the file long length = tmp.length(); // You cannot create an array using a long type. It needs to be an int type. // Before converting to an int type, check to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { System.out.println("File is too large"); } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset<bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file: " + tmp.getName()); } // Close the input stream and return bytes is.close(); String encodedFileString = Base64.encode(bytes); returnValue = encodedFileString; } } catch (IOException e) { e.printStackTrace(); } return returnValue; } }

下面的是客户端的代码,我们这里另外又建立了一个Java Project

首先是Web服务端提供给客户端的接口(这里假设Web服务端提供了)

package com.jadyer.server; public interface FileDownload { public String download(String fileName); }

然后是我们自己写的客户端代码

package com.jadyer.client; import java.io.FileOutputStream; import java.io.IOException; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import org.codehaus.xfire.util.Base64; import com.jadyer.server.FileDownload; /** * XFire实现文件下载,主要是将文件转换成Base64的字符串,来进行传递 * @see 本客户端需要引入MyEclipse6.5提供的XFire 1.2 Core、HTTP Client Libraries两个类库 */ public class testFileDownload { public static void main(String[] args) throws IOException { Service serviceModel = new ObjectServiceFactory().create(FileDownload.class); String url = "http://127.0.0.1:8080/XFire_Download/services/XFireDownloadDemo"; FileDownload service = (FileDownload) new XFireProxyFactory().create(serviceModel, url); //这里ResourceBundleEditor_v0.7.7.zip是位于D:/test/目录下的 //该目录由Web服务端FileDownloadImpl.java指定的 String result = (String) service.download("ResourceBundleEditor_v0.7.7.zip"); //接下来,主要就是将Base64串输出成文件 byte[] bytes = Base64.decode(result); FileOutputStream fos = new FileOutputStream("D://BB.zip"); fos.write(bytes); fos.flush(); fos.close(); } }

客户端在测试之前,先要在D盘根目录下面建立一个名为test的文件夹,这是服务端的接口实现类中限定的

然后将ResourceBundleEditor_v0.7.7.zip拷贝到D:/test/中,最后再运行testFileDownload类

此时在控制台上,将会是一片空白。于是我们移步到D盘的根目录下查看,发现BB.zip则文件下载成功  ^_^   ^_^

你可能感兴趣的:(XFire中进行文件下载)