<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • 如何使用Web Service傳輸文件

    時間:2024-10-31 21:56:02 J2EE培訓 我要投稿
    • 相關推薦

    如何使用Web Service傳輸文件

      server對外只開放80端口,并且還需要提供文件上傳和下載功能的應用,下面yjbys小編為大家準備了關于如何使用Web Service傳輸文件的文章,歡迎閱讀。

      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傳輸文件】相關文章:

    Web Service的開發與應用基礎07-12

    如何使用qq秒傳文件08-09

    電腦文件怎么傳輸到iPad07-30

    如何面試Web前端開發10-10

    iTunes文件共享功能怎么使用09-19

    TTF字體文件如何安裝11-03

    Excel文件如何設置密碼08-25

    學習如何打開php文件10-10

    如何由淺入深實踐學習 Web 標準10-10

    使用XQEngine來搜索XML文件內容07-07

    主站蜘蛛池模板: 91久久精品国产成人久久| 午夜成人精品福利网站在线观看| 国产欧美精品一区二区色综合 | 国产高清一级毛片精品| 精品调教CHINESEGAY| 欧美亚洲成人精品| 国产精品九九久久免费视频 | 在线精品无码字幕无码AV| 久久国产精品一区| 国产精品成人无码久久久久久| 国产精品视频免费| 国产成人精品高清在线观看93| 亚洲AV永久精品爱情岛论坛| 欧美精品亚洲精品日韩精品| 国产叼嘿久久精品久久| 久久久精品免费国产四虎| 99久久免费国产精精品| 国产精品高清一区二区三区 | 欧美激情视频精品一区二区| 国产在线91精品入口| 99精品国产成人一区二区| 777欧美午夜精品影院| 亚洲国产精品一区二区久久| 精品无码国产污污污免费网站国产| 久久久久国产成人精品亚洲午夜| 国语自产拍精品香蕉在线播放| 99re这里只有精品热久久| 国产亚洲欧美精品永久| 日韩国产成人精品视频| 无码精品人妻一区二区三区漫画| 亚洲人精品午夜射精日韩| 午夜精品一区二区三区在线观看| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | www国产精品| 国产成人精品综合网站| 97精品国产91久久久久久| 9久久9久久精品| 亚洲国产精品一区| 国产乱人伦偷精品视频| 久久久WWW成人免费精品| 日本精品视频在线观看|