亚洲一级电影在线观看,九九精品无码专区免费,亚洲AV无码资源在线观看 ,欧美国产高清

如何使用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

主站蜘蛛池模板: 国产精品国产三级国产专i| 亚洲AV无码久久精品色欲| 伊人蕉久影院| 最新国产成人在线| aⅴ激情视频| 国产香蕉一区二区在线网站| 成人性生交片无码免费看 | 天干天干啦夜天干天天爽| 少妇激情一区二区三区视频小说 | 在线播放国产精品三级网| 无码国产精品一区二区免费模式| 亚洲人成无码www久久久| 色妞色综合久久夜夜| 国产乱码精品一区三上| 亚洲综合伊人久久综合| 亚洲国产综合专区在线播放| 亚洲综合色区在线播放2019 | 狠狠噜天天噜日日噜色综合| 日本一区二区三区免费高清| 亚洲熟妇无码爱v在线观看| 国产性夜夜春夜夜爽免费下载| 成年无码按摩AV片在线| 亚洲国产精品一区二区高清无码久久 | 国产97公开成人免费视频| 国产91线观看| 亚洲日本中文字幕乱码在线电影 | 靖远县| 中文人妻熟女乱又乱精品| 乱子伦一区二区三区| 俄罗斯老熟妇乱子伦视频| 亚洲综合色区在线播放2019| 日韩精品一区二区三区免费在线观看| 亚洲日韩欧美在线成人| 极品老师腿张开粉嫩小泬| 玩爽少妇人妻系列无码| 精品国偷自产在线电影| 久久亚洲精品日韩高清| 江北区| 影音先锋人妻av中文字幕久久| 无码中文字幕日韩专区| 诏安县|