Apache CXF实战之七 使用Web Service传输文件

首先声明我知道有个协议叫ftp,也知道有种编程叫sock编程,但我就是碰到了server对外只开放80端口,并且还需要提供文件上传和下载功能的应用,那好吧,开始干活。

1. 首先是一个封装了服务器端文件路径,客户端文件路径和要传输的字节数组的MyFile类。

  1. package com.googlecode.garbagecan.cxfstudy.filetransfer; 
  2.  
  3.  
  4. public class MyFile { 
  5.      
  6.     private String clientFile; 
  7.      
  8.     private String serverFile; 
  9.      
  10.     private long position; 
  11.      
  12.     private byte[] bytes; 
  13.  
  14.     public String getClientFile() { 
  15.         return clientFile; 
  16.     } 
  17.  
  18.     public void setClientFile(String clientFile) { 
  19.         this.clientFile = clientFile; 
  20.     } 
  21.  
  22.     public String getServerFile() { 
  23.         return serverFile; 
  24.     } 
  25.  
  26.     public void setServerFile(String serverFile) { 
  27.         this.serverFile = serverFile; 
  28.     } 
  29.  
  30.     public long getPosition() { 
  31.         return position; 
  32.     } 
  33.  
  34.     public void setPosition(long position) { 
  35.         this.position = position; 
  36.     } 
  37.  
  38.     public byte[] getBytes() { 
  39.         return bytes; 
  40.     } 
  41.  
  42.     public void setBytes(byte[] bytes) { 
  43.         this.bytes = bytes; 
  44.     } 
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接口

  1. package com.googlecode.garbagecan.cxfstudy.filetransfer; 
  2.  
  3. import javax.jws.WebMethod; 
  4. import javax.jws.WebService; 
  5.  
  6. @WebService 
  7. public interface FileTransferService { 
  8.      
  9.     @WebMethod 
  10.     void uploadFile(MyFile myFile) throws FileTransferException; 
  11.  
  12.     @WebMethod 
  13.     MyFile downloadFile(MyFile myFile) throws FileTransferException; 
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接口实现类,主要是一些流的操作

  1. package com.googlecode.garbagecan.cxfstudy.filetransfer; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.IOException; 
  6. import java.io.InputStream; 
  7. import java.io.OutputStream; 
  8. import java.util.Arrays; 
  9.  
  10. import org.apache.commons.io.FileUtils; 
  11. import org.apache.commons.io.IOUtils; 
  12.  
  13. public class FileTransferServiceImpl implements FileTransferService { 
  14.  
  15.     public void uploadFile(MyFile myFile) throws FileTransferException { 
  16.         OutputStream os = null
  17.          
  18.         try
  19.             if (myFile.getPosition() != 0) { 
  20.                 os = FileUtils.openOutputStream(new File(myFile.getServerFile()), true); 
  21.             } else
  22.                 os = FileUtils.openOutputStream(new File(myFile.getServerFile()), false); 
  23.             } 
  24.             os.write(myFile.getBytes()); 
  25.         } catch(IOException e) { 
  26.             throw new FileTransferException(e.getMessage(), e); 
  27.         } finally
  28.             IOUtils.closeQuietly(os); 
  29.         } 
  30.     } 
  31.  
  32.     public MyFile downloadFile(MyFile myFile) throws FileTransferException { 
  33.         InputStream is = null
  34.          
  35.         try
  36.             is = new FileInputStream(myFile.getServerFile()); 
  37.             is.skip(myFile.getPosition()); 
  38.             byte[] bytes = new byte[1024 * 1024]; 
  39.             int size = is.read(bytes); 
  40.             if (size > 0) { 
  41.                 byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size); 
  42.                 myFile.setBytes(fixedBytes); 
  43.             } else
  44.                 myFile.setBytes(new byte[0]); 
  45.             } 
  46.         } catch(IOException e) { 
  47.             throw new FileTransferException(e.getMessage(), e); 
  48.         } finally
  49.             IOUtils.closeQuietly(is); 
  50.         } 
  51.         return myFile; 
  52.     } 
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. 一个简单的文件传输异常类
  1. package com.googlecode.garbagecan.cxfstudy.filetransfer; 
  2.  
  3. public class FileTransferException extends Exception { 
  4.  
  5.     private static final long serialVersionUID = 1L; 
  6.  
  7.     public FileTransferException() { 
  8.         super(); 
  9.     } 
  10.  
  11.     public FileTransferException(String message, Throwable cause) { 
  12.         super(message, cause); 
  13.     } 
  14.  
  15.     public FileTransferException(String message) { 
  16.         super(message); 
  17.     } 
  18.  
  19.     public FileTransferException(Throwable cause) { 
  20.         super(cause); 
  21.     } 
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
  1. package com.googlecode.garbagecan.cxfstudy.filetransfer; 
  2.  
  3. import javax.xml.ws.Endpoint; 
  4.  
  5. public class FileTransferServer { 
  6.      
  7.     public static void main(String[] args) throws Exception { 
  8.         Endpoint.publish("http://localhost:9000/ws/jaxws/fileTransferService", new FileTransferServiceImpl()); 
  9.     } 
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类,用来发送文件上传和下载请求。
  1. package com.googlecode.garbagecan.cxfstudy.filetransfer; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.IOException; 
  6. import java.io.InputStream; 
  7. import java.io.OutputStream; 
  8. import java.util.Arrays; 
  9.  
  10. import org.apache.commons.io.FileUtils; 
  11. import org.apache.commons.io.IOUtils; 
  12. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; 
  13.  
  14. public class FileTransferClient { 
  15.      
  16.     private static final String address = "http://localhost:9000/ws/jaxws/fileTransferService"
  17.      
  18.     private static final String clientFile = "/home/fkong/temp/client/test.zip"
  19.     private static final String serverFile = "/home/fkong/temp/server/test.zip"
  20.      
  21.     public static void main(String[] args) throws Exception { 
  22.         long start = System.currentTimeMillis(); 
  23. //      uploadFile(); 
  24. //      downloadFile(); 
  25.         long stop = System.currentTimeMillis(); 
  26.         System.out.println("Time: " + (stop - start)); 
  27.     } 
  28.      
  29.     private static void uploadFile() throws FileTransferException { 
  30.         InputStream is = null
  31.         try
  32.             MyFile myFile = new MyFile(); 
  33.             is = new FileInputStream(clientFile); 
  34.             byte[] bytes = new byte[1024 * 1024]; 
  35.             while (true) { 
  36.                 int size = is.read(bytes); 
  37.                 if (size <= 0) { 
  38.                     break
  39.                 } 
  40.                  
  41.                 byte[] fixedBytes = Arrays.copyOfRange(bytes, 0, size); 
  42.                 myFile.setClientFile(clientFile); 
  43.                 myFile.setServerFile(serverFile); 
  44.                 myFile.setBytes(fixedBytes); 
  45.                  
  46.                 uploadFile(myFile); 
  47.                  
  48.                 myFile.setPosition(myFile.getPosition() + fixedBytes.length); 
  49.             } 
  50.         } catch(IOException e) { 
  51.             throw new FileTransferException(e.getMessage(), e); 
  52.         } finally
  53.             IOUtils.closeQuietly(is); 
  54.         } 
  55.     } 
  56.      
  57.     private static void uploadFile(MyFile myFile) throws FileTransferException { 
  58.         JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); 
  59.         factoryBean.setAddress(address); 
  60.         factoryBean.setServiceClass(FileTransferService.class); 
  61.         Object obj = factoryBean.create(); 
  62.  
  63.         FileTransferService service = (FileTransferService) obj; 
  64.         service.uploadFile(myFile); 
  65.     } 
  66.      
  67.     private static void downloadFile() throws FileTransferException { 
  68.         MyFile myFile = new MyFile(); 
  69.         myFile.setServerFile(serverFile); 
  70.         long position = 0
  71.         while (true) { 
  72.             myFile.setPosition(position); 
  73.             myFile = downloadFile(myFile); 
  74.             if (myFile.getBytes().length <= 0) { 
  75.                 break
  76.             } 
  77.              
  78.             OutputStream os = null
  79.             try
  80.                 if (position != 0) { 
  81.                     os = FileUtils.openOutputStream(new File(clientFile), true); 
  82.                 } else
  83.                     os = FileUtils.openOutputStream(new File(clientFile), false); 
  84.                 } 
  85.                 os.write(myFile.getBytes()); 
  86.             } catch(IOException e) { 
  87.                 throw new FileTransferException(e.getMessage(), e); 
  88.             } finally
  89.                 IOUtils.closeQuietly(os); 
  90.             } 
  91.              
  92.             position += myFile.getBytes().length; 
  93.         } 
  94.     } 
  95.      
  96.     private static MyFile downloadFile(MyFile myFile) throws FileTransferException { 
  97.         JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); 
  98.         factoryBean.setAddress(address); 
  99.         factoryBean.setServiceClass(FileTransferService.class); 
  100.         Object obj = factoryBean.create(); 
  101.  
  102.         FileTransferService service = (FileTransferService) obj; 
  103.         return service.downloadFile(myFile); 
  104.     } 
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注释,运行程序,检查目标文件查看结果。

这个程序还是比较简单的,但基本生完成了文件上传下载功能,如果需要,也可以对这个程序再做点修改使其支持断点续传。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值