WebService实现文件上传下载

一:服务端:一个普通java web工程

package com.wzh.file;

import com.sun.xml.ws.developer.StreamingAttachment;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.ws.soap.MTOM;

/**
 * @author  y
 * @version V1.0
 * @desc    文件上传下载服务
 */
@StreamingAttachment(parseEagerly = true, memoryThreshold = 40000L)
@MTOM
@WebService(name = "MtomStreaming",
        serviceName = "MtomStreamingService",
        targetNamespace = "http://wzh.com",
        wsdlLocation = "StreamingImplService.wsdl")
public class FileManager {
    
    /**
     * 文件上传
     * @param fileName
     * @param data
     * @return 
     */
    @WebMethod
    public String fileUpload(String fileName,
            @XmlMimeType("application/octet-stream") DataHandler data){
        String result;
        
        File file = new File(fileName);
        
        try {
            OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
            
            data.writeTo(os);
            
            os.close();
            
            result = "1";
        } catch (FileNotFoundException ex) {
            result = "0";
            Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            result = "0";
            Logger.getLogger(FileManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        return result;
    }
    
    
    /**
     * 文件下载
     * @param filename
     * @return 
     */
    @XmlMimeType("application/octet-stream")
    @WebMethod
    public DataHandler fileDownload(String filename) {
        return new DataHandler(new FileDataSource(filename));
    } 
}

二:客户端:通过NetBeans建立Web服务客户端,根据Web服务端生成的wsdl进行创建,将服务端wsdl文件保存到本地,通过本地文件方式创建:

本地测试:

(1)新建InputStreamDataSource类继承DataSource

package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;

public class InputStreamDataSource implements DataSource {
    private final InputStream inputStream;

    public InputStreamDataSource(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return inputStream;
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        throw new UnsupportedOperationException("Not implemented");
    }

    @Override
    public String getContentType() {
        return "*/*";
    }

    @Override
    public String getName() {
        return "InputStreamDataSource";
    }
}

(2)Test.java

package test;

import com.wzh.MtomStreaming;
import com.wzh.MtomStreamingService;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        downloadFile();
    }
    
    
    public static void uploadFile(){
        MtomStreamingService service = new MtomStreamingService();
        MtomStreaming port = service.getMtomStreamingPort();
        
        InputStream is;

        try {
            is = new FileInputStream("/home/y/my_temp/tt.doc");

            DataHandler upf = new DataHandler(new InputStreamDataSource(is));
            String result = port.fileUpload("/home/y/my_temp2/tt.doc", upf);
            
            System.out.println("==========upload result:"+result);
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    public static void downloadFile(){
        MtomStreamingService service = new MtomStreamingService();
        MtomStreaming port = service.getMtomStreamingPort();
        
        DataHandler dhn = port.fileDownload("/home/y/my_temp/22.doc");
        try {
            File tempFile = new File("/home/y/my_temp/w01.doc");

            OutputStream output = new BufferedOutputStream(new FileOutputStream(tempFile));
            dhn.writeTo(output);

            output.close();
            
            System.out.println("====download");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    
}

 

posted @ 2015-04-16 15:58  yshy  阅读(4307)  评论(0编辑  收藏  举报