2011年11月29日 星期二

[測試 Tools] NCIT - NCITTools_ATF.jar (ncit.utils.PostMd) Post method to upload file

前言 : 
因為測試需求, 需要使用 HTTP Post method 來上傳檔案, 所以開發這個小工具. 目前工具可在 4shared 下載

使用說明 : 
可以使用下面命令上傳檔案到某個 Server : 
#java -cp NCITTools_ATF.jar ncit.utils.PostMd http://hostname upload_file_path

使用範例 : 
1. 請先在 Server 端開啟 Fake Http 監聽 Port 80. 他會將上傳的檔案傳到執行目錄下的 upload 目錄 : 
#java -cp NCITTools_ATF.jar ncit.utils.DummyHttp
DummyHttpd v0.1
伺服器連接埠: 80
傾聽客戶端於 80 連接埠....
客戶端連線: /192.168.1.50

2. 請在 Client 執行下面命令, 將檔案 ./test_virus.exe 上傳到 http://wrs77.winshipway.com : 
#java -cp NCITTools_ATF.jar ncit.utils.PostMd http://wrs77.winshipway.com ./test_vir.exe
File Length = 2709
statusLine>>>
HTTP/1.0 200 OK

3. 觀察 Server 端 Console : 
[Dummy] POST / HTTP/1.1
[Dummy] Content-type: application/octet-stream
[Dummy] User-Agent: Jakarta Commons-HttpClient/3.1
[Dummy] Host: wrs77.winshipway.com
[Dummy]
 Content-Length: 2709
[Dummy]
[Dummy] Input 2709 bytes...
[Dummy] 100%
[Dummy] byebye...

實現代碼 : 
* DummyHttp.java : 模擬一個簡單的 HTTP Server, 監聽某個 Port 並將 Request 的 Body 給儲存在當前路徑下的 upload 目錄裡 
  1. package ncit.utils;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.net.ServerSocket;  
  7. import java.net.Socket;  
  8.   
  9. import org.apache.commons.httpclient.HttpClient;  
  10.   
  11. public class DummyHttp {  
  12.     public static void main(String[] args) {    
  13.         String upRoot="upload";     
  14.         String indexFile="";     
  15.         int port;     
  16.     
  17.         ServerSocket serverSkt;     
  18.         Socket clientSkt;     
  19.     
  20.         try {     
  21.             BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));     
  22.             System.out.println("DummyHttpd v0.1");     
  23.             System.out.print("伺服器連接埠: ");     
  24.             port = Integer.parseInt(buf.readLine());     
  25.     
  26.             if(port < 0 || port > 65535) {    
  27.                 port = 80;     
  28.             }    
  29.             buf.close();  
  30.         }     
  31.         catch(Exception e) {     
  32.             System.err.println("錯誤: " + e.toString());     
  33.             System.out.print("採用內定選項");     
  34.             upRoot = "upload";     
  35.             indexFile = "index.html";     
  36.             port = 80;     
  37.             System.out.println("下載根目錄: " + upRoot +     
  38.                                "\n索引文件: " + indexFile +     
  39.                                "\n連接埠: " + port);     
  40.         }     
  41.     
  42.         try {     
  43.             serverSkt = new ServerSocket(port);     
  44.             System.out.println("傾聽客戶端於 " +     
  45.                 serverSkt.getLocalPort() + " 連接埠....");     
  46.     
  47.             while(true) {     
  48.                 clientSkt = serverSkt.accept();     
  49.                 System.out.println("客戶端連線: " + clientSkt.getInetAddress());     
  50.     
  51.                 // 啟動一個客戶端執行緒     
  52.                 Thread clientThread = new Thread(new Dummy(clientSkt, upRoot));     
  53.                 clientThread.start();     
  54.             }     
  55.         }     
  56.         catch(IOException e) {     
  57.             e.printStackTrace();     
  58.         }     
  59.     }    
  60. }  

* Dummy.java : 當 DummyHttp 收到 Socket 連線後, 會開線程轉交這個類別處理 
  1. package ncit.utils;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.io.PrintStream;  
  9. import java.net.Socket;  
  10.   
  11. public class Dummy implements Runnable{  
  12.     private Socket              sock = null;  
  13.     private String              dwFdrPath = null;  
  14.   
  15.     public Dummy(Socket s){this(s, null);}  
  16.     public Dummy(Socket s, String dp){this.sock=s; this.dwFdrPath = dp;}  
  17.       
  18.     @Override  
  19.     public void run() {  
  20.         File downFdr = null;  
  21.         File downF = null;  
  22.         if(dwFdrPath!=null)  
  23.         {  
  24.             downFdr = new File(dwFdrPath);  
  25.             downFdr.mkdirs();  
  26.             int i=1;  
  27.             downF = new File(downFdr,String.format("%d.upload", i));  
  28.             while(downF.exists())  
  29.             {  
  30.                 i++;  
  31.                 downF = new File(downFdr,String.format("%d.upload", i));  
  32.             }  
  33.         }  
  34.         String line = null;  
  35.         int len=-1;  
  36.         try  
  37.         {  
  38.             BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));  
  39.             FileWriter fw = null;  
  40.             if(downF!=null) fw = new FileWriter(downF);  
  41.             while((line=br.readLine())!=null)  
  42.             {  
  43.                 System.out.printf("\t[Dummy] %s\n", line);  
  44.                 if(line.trim().startsWith("Content-Length"))  
  45.                 {  
  46.                     len = Integer.valueOf(line.split(":")[1].trim());  
  47.                 }  
  48.                 if(line.trim().isEmpty()) break;  
  49.             }  
  50.             System.out.printf("\t[Dummy] Input %d bytes...\n", len);  
  51.             int rb=len;  
  52.             int lb=0;  
  53.             int tmp=0;  
  54.             char bs[]=null;  
  55.             if(len>0)  
  56.             {  
  57.                 while(rb>0)  
  58.                 {  
  59.                     if(rb>1024)  
  60.                     {  
  61.                         bs = new char[1024];  
  62.                         tmp=br.read(bs);  
  63.                         rb-=tmp;  
  64.                         lb+=tmp;  
  65.                     }  
  66.                     else  
  67.                     {  
  68.                         bs = new char[rb];  
  69.                         br.read(bs);  
  70.                         rb=0;  
  71.                         lb+=rb;  
  72.                     }  
  73.                     if(fw!=null) fw.write(bs);  
  74.                     System.out.printf("\t[Dummy] %s", String.format("%3d%c\r", lb*100/len, '%'));  
  75.                 }  
  76.                 System.out.println("\t[Dummy] 100%");  
  77.             }  
  78.             if(fw!=null) fw.close();  
  79.             //System.out.println();  
  80.             PrintStream printStream = new PrintStream(sock.getOutputStream());  
  81.             printStream.print("HTTP/1.0 200 OK\r\n");  
  82.             printStream.print("Server: TinyHttpd v0.1\r\n");   
  83.             printStream.print("\r\n\r\n");  
  84.             printStream.close();  
  85.             br.close();  
  86.             sock.close();  
  87.             System.out.printf("\t[Dummy] byebye...\n");  
  88.         }  
  89.         catch(IOException e)  
  90.         {  
  91.               
  92.         }  
  93.     }  
  94. }  

* PostMd.java : 使用 HttpClient 模擬 HTTP Post 上傳檔案 
  1. package ncit.utils;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5.   
  6. import org.apache.commons.httpclient.HttpClient;  
  7. import org.apache.commons.httpclient.methods.PostMethod;  
  8.   
  9. public class PostMd {  
  10.     /** 
  11.      * Reference 
  12.      *  - http://ianjung1974.blogspot.com/2009/03/content-type.html 
  13.      *  - http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload 
  14.      * @param args 
  15.      * @throws Exception 
  16.      */  
  17.     public static void main(String args[]) throws Exception  
  18.     {  
  19.         HttpClient client = new HttpClient();  
  20.         PostMethod postMethod = new PostMethod(args[0]);  
  21.   
  22.         //client.setConnectionTimeout(8000);  
  23.   
  24.         // Send file as the body of the POST request  
  25.         File f = new File(args[1]);  
  26.         System.out.println("File Length = " + f.length());  
  27.   
  28.         postMethod.setRequestBody(new FileInputStream(f));  
  29.         postMethod.setRequestHeader("Content-type",  
  30.             "application/octet-stream");  
  31.   
  32.         int statusCode1 = client.executeMethod(postMethod);  
  33.   
  34.         System.out.println("statusLine>>>" + postMethod.getStatusLine());  
  35.         postMethod.releaseConnection();  
  36.     }  
  37. }  

HTTP Content type 大全 
HttpClient-and-FileUpload

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...