首先声明我知道有个协议叫ftp,也知道有种编程叫sock编程,但我就是碰到了server对外只开放80端口,并且还需要提供文件上传和下载功能的应用,那好吧,开始干活。
1. 首先是一个封装了服务器端文件路径,客户端文件路径和要传输的字节数组的MyFile类。
- package com.googlecode.garbagecan.cxfstudy.filetransfer;
- public class MyFile {
- private String clientFile;
- private String serverFile;
- private long position;
- private byte[] bytes;
- public String getClientFile() {
- return clientFile;
- }
- public void setClientFile(String clientFile) {
- this.clientFile = clientFile;
- }
- public String getServerFile() {
- return serverFile;
- }
- public void setServerFile(String serverFile) {
- this.serverFile = serverFile;
- }
- public long getPosition() {
- return position;
- }
- public void setPosition(long position) {
- this.position = position;
- }
- public byte[] getBytes() {
- return bytes;
- }
- public void setBytes(byte[] bytes) {
- this.bytes = bytes;
- }
- }
2. 文件传输的Web Service接口
- package com.googlecode.garbagecan.cxfstudy.filetransfer;
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- @WebService
- public interface FileTransferService {
- @WebMethod
- void uploadFile(MyFile myFile) throws FileTransferException;
- @WebMethod
- MyFile downloadFile(MyFile myFile) throws FileTransferException;
- }
3. 文件传输的Web Service接口实现类,主要是一些流的操作
- package com.googlecode.garbagecan.cxfstudy.filetransfer;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Arrays;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- public class FileTransferServiceImpl implements FileTransferService {
- public void uploadFile(MyFile myFile) throws FileTransferException {
- OutputStream os = null;
- try {
- if (myFile.getPosition() != 0) {
- os = FileUtils.openOutputStream(new File(myFile.getServerFile()), true);
- } else {
- os = FileUtils.openOutputStream(new File(myFile.getServerFile()), false);
- }
- os.write(myFile.getBytes());
- } catch(IOException e) {
- throw new FileTransferException(e.getMessage(), e);
- } finally {
- IOUtils.closeQuietly(os);
- }
- }
- public MyFile downloadFile(MyFile myFile) throws FileTransferException {
- InputStream is = null;
- try {
- is = new FileInputStream(myFile.getServerFile());
- is.skip(myFile.getPosition());
- byte[] bytes = new byte[1024 * 1024];
- int size = is.read(bytes);
- if (size > 0) {
- byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size);
- myFile.setBytes(fixedBytes);
- } else {
- myFile.setBytes(new byte[0]);
- }
- } catch(IOException e) {
- throw new FileTransferException(e.getMessage(), e);
- } finally {
- IOUtils.closeQuietly(is);
- }
- return myFile;
- }
- }
4. 一个简单的文件传输异常类
- package com.googlecode.garbagecan.cxfstudy.filetransfer;
- public class FileTransferException extends Exception {
- private static final long serialVersionUID = 1L;
- public FileTransferException() {
- super();
- }
- public FileTransferException(String message, Throwable cause) {
- super(message, cause);
- }
- public FileTransferException(String message) {
- super(message);
- }
- public FileTransferException(Throwable cause) {
- super(cause);
- }
- }
5. 下面是Server类用来发布web service
- package com.googlecode.garbagecan.cxfstudy.filetransfer;
- import javax.xml.ws.Endpoint;
- public class FileTransferServer {
- public static void main(String[] args) throws Exception {
- Endpoint.publish("http://localhost:9000/ws/jaxws/fileTransferService", new FileTransferServiceImpl());
- }
- }
6. 最后是Client类,用来发送文件上传和下载请求。
- package com.googlecode.garbagecan.cxfstudy.filetransfer;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Arrays;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- public class FileTransferClient {
- private static final String address = "http://localhost:9000/ws/jaxws/fileTransferService";
- private static final String clientFile = "/home/fkong/temp/client/test.zip";
- private static final String serverFile = "/home/fkong/temp/server/test.zip";
- public static void main(String[] args) throws Exception {
- long start = System.currentTimeMillis();
- // uploadFile();
- // downloadFile();
- long stop = System.currentTimeMillis();
- System.out.println("Time: " + (stop - start));
- }
- private static void uploadFile() throws FileTransferException {
- InputStream is = null;
- try {
- MyFile myFile = new MyFile();
- is = new FileInputStream(clientFile);
- byte[] bytes = new byte[1024 * 1024];
- while (true) {
- int size = is.read(bytes);
- if (size <= 0) {
- break;
- }
- byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size);
- myFile.setClientFile(clientFile);
- myFile.setServerFile(serverFile);
- myFile.setBytes(fixedBytes);
- uploadFile(myFile);
- myFile.setPosition(myFile.getPosition() + fixedBytes.length);
- }
- } catch(IOException e) {
- throw new FileTransferException(e.getMessage(), e);
- } finally {
- IOUtils.closeQuietly(is);
- }
- }
- private static void uploadFile(MyFile myFile) throws FileTransferException {
- JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
- factoryBean.setAddress(address);
- factoryBean.setServiceClass(FileTransferService.class);
- Object obj = factoryBean.create();
- FileTransferService service = (FileTransferService) obj;
- service.uploadFile(myFile);
- }
- private static void downloadFile() throws FileTransferException {
- MyFile myFile = new MyFile();
- myFile.setServerFile(serverFile);
- long position = 0;
- while (true) {
- myFile.setPosition(position);
- myFile = downloadFile(myFile);
- if (myFile.getBytes().length <= 0) {
- break;
- }
- OutputStream os = null;
- try {
- if (position != 0) {
- os = FileUtils.openOutputStream(new File(clientFile), true);
- } else {
- os = FileUtils.openOutputStream(new File(clientFile), false);
- }
- os.write(myFile.getBytes());
- } catch(IOException e) {
- throw new FileTransferException(e.getMessage(), e);
- } finally {
- IOUtils.closeQuietly(os);
- }
- position += myFile.getBytes().length;
- }
- }
- private static MyFile downloadFile(MyFile myFile) throws FileTransferException {
- JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
- factoryBean.setAddress(address);
- factoryBean.setServiceClass(FileTransferService.class);
- Object obj = factoryBean.create();
- FileTransferService service = (FileTransferService) obj;
- return service.downloadFile(myFile);
- }
- }
首先需要准备一个大一点的文件,然后修改代码中的clientFile和serverFile路径,然后分别打开uploadFile和downloadFile注释,运行程序,检查目标文件查看结果。
这个程序还是比较简单的,但基本生完成了文件上传下载功能,如果需要,也可以对这个程序再做点修改使其支持断点续传。
相关推荐
**实战Web Service与Apache CXF开发** Web服务是一种在互联网上进行通信的标准协议,它允许应用程序之间进行数据交换。Apache CXF是一个开源框架,用于构建和部署Web服务,支持多种Web服务标准,如SOAP、RESTful ...
Apache CXF 是一个开源的Java框架,主要用于构建和开发服务导向架构(Service-Oriented Architecture, SOA)和Web服务。本学习笔记旨在提供对Apache CXF的基本理解、功能特性和实际操作指导。 **1. CXF 简介** 1.1...
### 开发Web服务:使用Apache CXF与Axis2(第三版) #### 一、书籍概述 本书《开发Web服务:使用Apache CXF与Axis2》是针对希望学习如何使用Java创建Web服务的专业人士所编写的实用教程。作者Kent Kai Ok Tong以...
通过上述内容的学习,我们可以了解到Web Service技术的基本概念及其在Java环境中的应用,特别是Apache CXF框架如何帮助开发者快速搭建Web Service服务端。这对于从事Java软件开发的工程师来说是非常实用的知识点。...
2. **生成WSDL**:使用CXF工具,可以从接口生成WSDL(Web服务描述语言)文件。WSDL描述了服务的接口,包括服务位置、操作以及消息格式。 3. **实现服务**:接着,你需要实现之前定义的接口,提供实际的服务逻辑。 ...
1.3.3 CXF:CXF是另一个流行的开源Web Service框架,它结合了Xfire和Apache SOAP项目的优点,支持多种Web Service规范,并且可以与Spring框架无缝集成。 1.3.4 主流 Web Service 的比较 Axis适合初学者,因为它的...
通过这个工程,学习者可以了解XML、WSDL、SOAP的作用,以及如何使用JAX-WS和Apache CXF来创建和使用Web服务。同时,项目还涉及到服务的部署和客户端调用,提供了一个完整的Web服务生命周期的实战体验。
《实战Mule:利用Mule调用XFire发布的Web服务》这篇文章主要讲解了如何使用Mule ESB(Enterprise Service Bus)来调用由XFire框架发布的Web服务。在这个过程中,我们将深入理解Mule ESB的功能以及它与XFire集成的方式...
本课程关注的是Web服务的基础与实践,特别是针对Apache CXF框架的使用。 **Apache CXF** 是一个开源的Java框架,用于构建和部署Web服务。CXF允许开发者以Java编程语言来创建服务端和客户端应用程序,支持多种协议,...
XFire是Apache CXF的前身,它是一个用于创建和消费Web服务的框架。XFire提供了XML到Java绑定,使得开发者可以轻松地创建服务接口,并自动生成相应的服务实现。通过XFire,我们可以快速发布一个文件上传服务,这个...
2. **WSDL(Web Service Description Language)**:为了调用一个SOAP Web服务,开发者通常需要WSDL文件,这是一个XML文档,描述了服务的位置、使用的消息格式以及提供的操作。通过解析WSDL,开发者可以了解如何构建...
Xfire是Apache CXF项目的前身,它简化了Web服务的开发流程,提供了强大的XML绑定和自动代码生成功能。Xfire支持JAXB(Java Architecture for XML Binding)和Aegis两种数据绑定机制,可以方便地将Java对象转换为XML...
文件"Axis高级编程.pdf"可能深入讲解了如何使用Axis进行WebService的高级操作。 3. **XFire**:XFire是另一个流行的Java WebService框架,后来演变成了CXF。XFire提供了一种简单的方式去创建和消费WebService,它...
ActiveMQ是Apache软件基金会开发的一款开源消息代理和队列服务器,它支持多种消息协议,如JMS(Java Message Service)。在速运快递项目中,`ActiveMQ`起到了消息传递和协调的作用。通过消息队列,系统能够处理高...