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

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

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

 

[java]  view plain copy print ?
 
  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.     }  
  45. }  

2. 文件传输的Web Service接口

 

 

[java]  view plain copy print ?
 
  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;  
  14. }  

3. 文件传输的Web Service接口实现类,主要是一些流的操作

 

 

[java]  view plain copy print ?
 
  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.     }  
  53. }  

4. 一个简单的文件传输异常类

[java]  view plain copy print ?
 
  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.     }  
  22. }  

5. 下面是Server类用来发布web service

[java]  view plain copy print ?
 
  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.     }  
  10. }  

6. 最后是Client类,用来发送文件上传和下载请求。

[java]  view plain copy print ?
 
  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.     }  
  105. }  

首先需要准备一个大一点的文件,然后修改代码中的clientFile和serverFile路径,然后分别打开uploadFile和downloadFile注释,运行程序,检查目标文件查看结果。

 

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

猜你喜欢

转载自liming495.iteye.com/blog/1845025