中文字幕在线一区二区在线,久久久精品免费观看国产,无码日日模日日碰夜夜爽,天堂av在线最新版在线,日韩美精品无码一本二本三本,麻豆精品三级国产国语,精品无码AⅤ片,国产区在线观看视频

      如何使用Web Service傳輸文件

      時間:2024-10-31 21:56:02 J2EE培訓(xùn) 我要投稿
      • 相關(guān)推薦

      如何使用Web Service傳輸文件

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

        1. 首先是一個封裝了服務(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接口實現(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. 一個簡單的文件傳輸異常類

        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)備一個大一點的文件,然后修改代碼中的clientFile和serverFile路徑,然后分別打開uploadFile和downloadFile注釋,運行程序,檢查目標(biāo)文件查看結(jié)果。

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

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

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

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

      電腦文件怎么傳輸?shù)絠Pad07-30

      如何面試Web前端開發(fā)10-10

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

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

      Excel文件如何設(shè)置密碼08-25

      學(xué)習(xí)如何打開php文件10-10

      如何由淺入深實踐學(xué)習(xí) Web 標(biāo)準(zhǔn)10-10

      使用XQEngine來搜索XML文件內(nèi)容07-07

      主站蜘蛛池模板: 免费视频这里是精品视频| 国产精品一码二码三码在线| 日韩中文字幕在线乱码| 中文字幕一区二区三区人妻精品| 亚洲a∨好看av高清在线观看| 韩国三级大全久久网站| 亚洲色www无码| 成人性生交大片免费看激情| 仙女白丝jk小脚夹得我好爽| 国产亚洲视频在线观看播放| 登封市| 亚洲人av毛片一区二区| 免费一级国产大片| 99日本亚洲黄色三级高清网站| 国产精品黑色丝袜在线播放| 大化| 人妻被猛烈进入中文字幕| 99久久这里只精品国产免费| 91在线区啪国自产网页| 香蕉久久av一区二区三区| 永久免费不卡在线观看黄网站| 广汉市| 尼玛县| 大渡口区| 屏南县| 临猗县| 中文字幕少妇人妻视频| 永清县| 遵化市| 无码人妻少妇久久中文字幕| 厦门市| 大港区| 视频精品熟女一区二区三区| 欧美日一本| 国产成人综合亚洲av| 欧美成人网视频| 自拍视频国产在线观看| 久久99亚洲网美利坚合众国| 国产精品女同久久久久久| 狼人av在线免费观看| 中文字幕第一页亚洲观看|