標籤:目錄 down pojo 檔案上傳 關閉 head 命名空間 invoke 執行檔案
測試環境:axis2-1.6.1、6.0.20、jdk1.5
說明:本方式僅適用於檔案小於10M的情境(否則會出現記憶體溢出),大檔案的上傳下載應另選其他方式。
1、建立要發布成webservice的java類。
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; }}
2、將上面的java類編譯後的class檔案放到axis2\WEB-INF\pojo目錄下。
3、編寫用戶端程式。
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); //設定入參(1、檔案名稱,2、檔案位元組流數組) 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()+" 檔案下載完成"); }}
文章來源:http://huangqiqing123.iteye.com/blog/1454819
webservice檔案上傳下載(byte[] 實現方式)