2015年3月2日 星期一

測試檔案上傳及下載的WebService with Java

最近為了撰寫有關檔案upload and download的WS先試寫了Sample Code with Java

WebService 程式如下

     @WebMethod
      public void upload(@WebParam(name = "fileName")String fileName, byte[] imageBytes) {
       
      String filePath = "d:/MsgServerUpload/" + fileName;
       
      try {
          FileOutputStream fos = new FileOutputStream(filePath);
          BufferedOutputStream outputStream = new BufferedOutputStream(fos);
          outputStream.write(imageBytes);
          outputStream.close();
           
          System.out.println("Received file: " + filePath);
           
      } catch (IOException ex) {
        System.out.println("Exception from here");
          System.err.println(ex);
          throw new WebServiceException(ex);
      }
}
 
     @WebMethod
      public byte[] download(String fileName) {
      String filePath = "d:/MsgServerDownload/" + fileName;
      System.out.println("Sending file: " + filePath);
       
      try {
          File file = new File(filePath);
          FileInputStream fis = new FileInputStream(file);
          BufferedInputStream inputStream = new BufferedInputStream(fis);
          byte[] fileBytes = new byte[(int) file.length()];
          inputStream.read(fileBytes);
          inputStream.close();
           
          return fileBytes;
      } catch (IOException ex) {
          System.err.println(ex);
          throw new WebServiceException(ex);
      }      
  }

請容許我稍微解釋一下這2個Method的路徑,d:/MsgServerUpload/ 是指 user在呼叫WS後,會將使用者上傳的的檔案放在Web Server的該路徑下,也就是說上傳的檔案都會放在這裡啦!
那d:/MsgServerDownload/當然是我在Server上放的要供使用者download的目錄
也就是,使用者upload or download的路徑我都事先預設好了,當然你的Web Server下要有該目錄喔!

接著我們可以寫個Java版的測試WS是否可呼叫成功
完整的程式碼如下,我用的方式必須先將WS利用NetBean開WS Build成WSClient.jar才能呼叫API

package com.sti.ws.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;

import com.sti.web.msg.webservice.Exception_Exception;
import com.sti.web.msg.webservice.MSGWebService;
import com.sti.web.msg.webservice.MSGWebService_Service;


public class TestWS {

    public static void main(String[] args) {
   MSGWebService msgws = getMsgWS();
   try {
    String fileName = "test.txt";
   
       String filePath = "d:/YourLocalFileFolder/" + fileName;
       File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream inputStream = new BufferedInputStream(fis);
            byte[] imageBytes = new byte[(int) file.length()];
            inputStream.read(imageBytes);
             
            msgws.upload(file.getName(), imageBytes);
            
            inputStream.close();
            System.out.println("File uploaded: " + filePath);
        } catch (IOException ex) {
        ex.printStackTrace();
            System.err.println(ex);
        }
   
   
   try {
   //指定要download的檔名
   String fileName = "test1.txt";
   //要求download到哪裡
        String filePath = "d:/YourLocalDownloadFolder/" + fileName;
        byte[] fileBytes = msgws.download(fileName);
         
      
            FileOutputStream fos = new FileOutputStream(filePath);
            BufferedOutputStream outputStream = new BufferedOutputStream(fos);
            outputStream.write(fileBytes);
            outputStream.close();
             
            System.out.println("File downloaded: " + filePath);
        } catch (IOException ex) {
            System.err.println(ex);
        }*/
   
   
    }

private final static String MSGWS_WSDL_LOCATION;
private final static String MSGWS_QName;
private final static String MSGWS_qname;

static {
MSGWS_WSDL_LOCATION = "http://192.168.73.1:7001/YourWebService?wsdl";
MSGWS_QName = "http://webservice.msg.web.sti.com/";
MSGWS_qname = "MSGWebService";
}

public static MSGWebService getMsgWS() {
if(MSGWS_WSDL_LOCATION == null || "".equals(MSGWS_WSDL_LOCATION)) {
} else {
try {
URL url = new URL(MSGWS_WSDL_LOCATION);
QName qname = new QName(MSGWS_QName, MSGWS_qname);
MSGWebService service = new MSGWebService_Service(url, qname).getMSGWebServicePort();
System.out.println("OK");
return service;
} catch(MalformedURLException me) {
System.out.println("Fail");
me.printStackTrace();
System.out.println(me.getMessage());
}
}
return null;
}

}

我在範例測試程式裡,將我Local下的 test.txt檔案上傳,上傳成功後,您會發現你的Web Server下的d:/MsgServerUpload/有你剛才上傳的檔案,
而在download的範例程式,我假設在Web Server的d:/MsgServerDownload/目錄下
有一個檔案叫做 test1.txt,注意你必須先做好一個檔案放至此目錄下
接著指定我要download到我Local的d:/YourLocalDownloadFolder/ 路徑下就會出現一個叫做test1.txt的檔案了

為什麼我要分開目錄呢,是為了區別來自於不同的程式,你可以把WS程式的目錄指向同一個,也就是說自己Local上傳的檔案,也可以透過WS自己DownLoad回來。

Cheers