日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ApacheCXF實戰(zhàn)之七:使用WebService傳輸文件

首先聲明我知道有個協(xié)議叫ftp,也知道有種編程叫sock編程,但我就是碰到了server對外只開放80端口,并且還需要提供文件上傳和下載功能的應(yīng)用,那好吧,開始干活。

成都創(chuàng)新互聯(lián)專業(yè)提供成都主機(jī)托管四川主機(jī)托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價格,機(jī)房位于中國電信/網(wǎng)通/移動機(jī)房,IDC機(jī)房托管服務(wù)有保障!

1. 首先是一個封裝了服務(wù)器端文件路徑,客戶端文件路徑和要傳輸?shù)淖止?jié)數(shù)組的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.     }  
  45. }  

2. 文件傳輸?shù)腤eb 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;  

3. 文件傳輸?shù)腤eb Service接口實現(xiàn)類,主要是一些流的操作

 
 
 
  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.     }  

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.     }  

5. 下面是Server類用來發(fā)布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.     }  

6. 最后是Client類,用來發(fā)送文件上傳和下載請求。

 
 
 
  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. }  

首先需要準(zhǔn)備一個大一點的文件,然后修改代碼中的clientFile和serverFile路徑,然后分別打開uploadFile和downloadFile注釋,運行程序,檢查目標(biāo)文件查看結(jié)果。

這個程序還是比較簡單的,但基本生完成了文件上傳下載功能,如果需要,也可以對這個程序再做點修改使其支持?jǐn)帱c續(xù)傳。

原文鏈接:http://blog.csdn.net/kongxx/article/details/7540930

【系列文章】

  1. Apache CXF實戰(zhàn)之六:創(chuàng)建安全的Web Service
  2. Apache CXF實戰(zhàn)之五:壓縮Web Service數(shù)據(jù)
  3. Apache CXF實戰(zhàn)之四:構(gòu)建RESTful Web Service
  4. Apache CXF實戰(zhàn)之三:傳輸Java對象
  5. Apache CXF實戰(zhàn)之二:集成Sping與Web容器
  6. Apache CXF實戰(zhàn)之一:Hello World Web Service

文章標(biāo)題:ApacheCXF實戰(zhàn)之七:使用WebService傳輸文件
網(wǎng)站URL:http://www.dlmjj.cn/article/djiispp.html