webservice文件上傳下載(byte[] 實現方式)

測試環境:axis2-1.6.一、6.0.20、jdk1.5java

 

說明:本方式僅適用於文件小於10M的場景(不然會出現內存溢出),大文件的上傳下載應另選其餘方式。web

 

一、建立要發佈成webservice的java類。apache

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class BlobService {

	/*
	 * 文件上傳服務
	 */
    public boolean uploadFile(String fileName,byte[] bytes)
    {
        FileOutputStream fos = null;
        try{
        	fos = new FileOutputStream("F:\\"+fileName);
        	
        	//將字節數組bytes中的數據,寫入文件輸出流fos中
            fos.write(bytes);
            fos.flush();
        }catch (Exception e){
        	e.printStackTrace();
            return false;
        }finally{
        	try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  	
        }
        return true;
    }
    /*
     * 文件下載服務
     */
    public byte[] downloadFile()
    {
    	String filepath = "F:\\head.jpg";
    	FileInputStream in = null;
    	byte bytes[] = null;
		try {
			in = new FileInputStream(filepath);
			bytes = new byte[in.available()];
			
			//從輸入流in中,將 bytes.length 個字節的數據讀入字節數組bytes中
			in.read(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{		
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	return bytes;
    }
}

 

 

二、將上面的java類編譯後的class文件放到axis2\WEB-INF\pojo目錄下。數組

 

三、編寫客戶端程序。測試

package client;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class BlobRPCClient
{
    public static void main(String[] args) throws Exception
    {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/BlobService");
        options.setTo(targetEPR);
       
        //=================測試文件上傳==================================
        
        String filePath = "f:\\head.jpg";
        FileInputStream fis = new FileInputStream(filePath);
       
        // 建立保存要上傳的圖像文件內容的字節數組
        byte[] buffer = new byte[fis.available()];
        
        //將輸入流fis中的數據讀入字節數組buffer中
        fis.read(buffer);  
      
        //設置入參(一、文件名,二、文件字節流數組)
        Object[] opAddEntryArgs = new Object[]{"我是上傳的文件.jpg", buffer};
        
        //返回值類型
        Class<?>[] classes = new Class<?>[]{ Boolean.class };
        
        //指定要調用的方法名及WSDL文件的命名空間
        QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile");
        
        //關閉流
        fis.close();
     
        //執行文件上傳
        System.out.println(new Date()+" 文件上傳開始");
        Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
        System.out.println(new Date()+" 文件上傳結束,返回值="+returnValue);
      
        //=================測試文件下載==================================

        opAddEntry = new QName("http://ws.apache.org/axis2", "downloadFile");
        
        System.out.println(new Date()+" 文件下載開始");
        byte bytes[] = (byte[]) serviceClient.invokeBlocking(opAddEntry, new Object[]{}, new Class[]{byte[].class})[0];
        FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下載的文件.jpg");
       
        //將字節數組bytes中的數據,所有寫入輸出流fileOutPutStream中
        fileOutPutStream.write(bytes);
        fileOutPutStream.flush();
        fileOutPutStream.close();
        System.out.println(new Date()+" 文件下載完成");
    }
}

 

 

四、運行客戶端程序,輸出結果以下:spa

Thu Mar 15 20:42:55 CST 2012 文件上傳開始
Thu Mar 15 20:42:56 CST 2012 文件上傳結束,返回值=true
Thu Mar 15 20:42:56 CST 2012 文件下載開始
Thu Mar 15 20:42:56 CST 2012 文件下載完成

 

 

五、打開目錄 F:\,會看到:code


 

 

http://huangqiqing123.iteye.com/admin/blogs/1454819xml

 

可能用到的jar包:blog


  

相關文章
相關標籤/搜索