1. <tt id="5hhch"><source id="5hhch"></source></tt>
    1. <xmp id="5hhch"></xmp>

  2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

    <rp id="5hhch"></rp>
        <dfn id="5hhch"></dfn>

      1. 如何使用Web Service傳輸文件

        時(shí)間:2020-10-06 09:56:55 J2EE培訓(xùn) 我要投稿

        如何使用Web Service傳輸文件

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

          1. 首先是一個(gè)封裝了服務(wù)器端文件路徑,客戶端文件路徑和要傳輸?shù)淖止?jié)數(shù)組的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. 文件傳輸?shù)腤eb 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. 文件傳輸?shù)腤eb Service接口實(shí)現(xiàn)類,主要是一些流的操作

          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. 一個(gè)簡單的文件傳輸異常類

          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類用來發(fā)布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類,用來發(fā)送文件上傳和下載請求。

          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);

          }

          }

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

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

        【如何使用Web Service傳輸文件】相關(guān)文章:

        如何壓縮Web Service數(shù)據(jù)12-10

        如何創(chuàng)建安全的Web Service12-10

        j2ee培訓(xùn):如何構(gòu)建RESTful Web Service10-10

        如何使用qq發(fā)送文件10-11

        如何使用qq秒傳文件10-11

        C#開發(fā)和調(diào)用Web Service實(shí)例12-06

        Web Service的開發(fā)與應(yīng)用基礎(chǔ)12-06

        電腦文件怎么傳輸?shù)絠Pad10-11

        Windows平臺(tái)下如何使用rsync實(shí)現(xiàn)文件同步11-27

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码

        1. <tt id="5hhch"><source id="5hhch"></source></tt>
          1. <xmp id="5hhch"></xmp>

        2. <xmp id="5hhch"><rt id="5hhch"></rt></xmp>

          <rp id="5hhch"></rp>
              <dfn id="5hhch"></dfn>