因為測試需求, 需要使用 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 目錄裡
- package ncit.utils;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.ServerSocket;
- import java.net.Socket;
- import org.apache.commons.httpclient.HttpClient;
- public class DummyHttp {
- public static void main(String[] args) {
- String upRoot="upload";
- String indexFile="";
- int port;
- ServerSocket serverSkt;
- Socket clientSkt;
- try {
- BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("DummyHttpd v0.1");
- System.out.print("伺服器連接埠: ");
- port = Integer.parseInt(buf.readLine());
- if(port < 0 || port > 65535) {
- port = 80;
- }
- buf.close();
- }
- catch(Exception e) {
- System.err.println("錯誤: " + e.toString());
- System.out.print("採用內定選項");
- upRoot = "upload";
- indexFile = "index.html";
- port = 80;
- System.out.println("下載根目錄: " + upRoot +
- "\n索引文件: " + indexFile +
- "\n連接埠: " + port);
- }
- try {
- serverSkt = new ServerSocket(port);
- System.out.println("傾聽客戶端於 " +
- serverSkt.getLocalPort() + " 連接埠....");
- while(true) {
- clientSkt = serverSkt.accept();
- System.out.println("客戶端連線: " + clientSkt.getInetAddress());
- // 啟動一個客戶端執行緒
- Thread clientThread = new Thread(new Dummy(clientSkt, upRoot));
- clientThread.start();
- }
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
* Dummy.java : 當 DummyHttp 收到 Socket 連線後, 會開線程轉交這個類別處理
- package ncit.utils;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintStream;
- import java.net.Socket;
- public class Dummy implements Runnable{
- private Socket sock = null;
- private String dwFdrPath = null;
- public Dummy(Socket s){this(s, null);}
- public Dummy(Socket s, String dp){this.sock=s; this.dwFdrPath = dp;}
- @Override
- public void run() {
- File downFdr = null;
- File downF = null;
- if(dwFdrPath!=null)
- {
- downFdr = new File(dwFdrPath);
- downFdr.mkdirs();
- int i=1;
- downF = new File(downFdr,String.format("%d.upload", i));
- while(downF.exists())
- {
- i++;
- downF = new File(downFdr,String.format("%d.upload", i));
- }
- }
- String line = null;
- int len=-1;
- try
- {
- BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- FileWriter fw = null;
- if(downF!=null) fw = new FileWriter(downF);
- while((line=br.readLine())!=null)
- {
- System.out.printf("\t[Dummy] %s\n", line);
- if(line.trim().startsWith("Content-Length"))
- {
- len = Integer.valueOf(line.split(":")[1].trim());
- }
- if(line.trim().isEmpty()) break;
- }
- System.out.printf("\t[Dummy] Input %d bytes...\n", len);
- int rb=len;
- int lb=0;
- int tmp=0;
- char bs[]=null;
- if(len>0)
- {
- while(rb>0)
- {
- if(rb>1024)
- {
- bs = new char[1024];
- tmp=br.read(bs);
- rb-=tmp;
- lb+=tmp;
- }
- else
- {
- bs = new char[rb];
- br.read(bs);
- rb=0;
- lb+=rb;
- }
- if(fw!=null) fw.write(bs);
- System.out.printf("\t[Dummy] %s", String.format("%3d%c\r", lb*100/len, '%'));
- }
- System.out.println("\t[Dummy] 100%");
- }
- if(fw!=null) fw.close();
- //System.out.println();
- PrintStream printStream = new PrintStream(sock.getOutputStream());
- printStream.print("HTTP/1.0 200 OK\r\n");
- printStream.print("Server: TinyHttpd v0.1\r\n");
- printStream.print("\r\n\r\n");
- printStream.close();
- br.close();
- sock.close();
- System.out.printf("\t[Dummy] byebye...\n");
- }
- catch(IOException e)
- {
- }
- }
- }
* PostMd.java : 使用 HttpClient 模擬 HTTP Post 上傳檔案
- package ncit.utils;
- import java.io.File;
- import java.io.FileInputStream;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.methods.PostMethod;
- public class PostMd {
- /**
- * Reference
- * - http://ianjung1974.blogspot.com/2009/03/content-type.html
- * - http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload
- * @param args
- * @throws Exception
- */
- public static void main(String args[]) throws Exception
- {
- HttpClient client = new HttpClient();
- PostMethod postMethod = new PostMethod(args[0]);
- //client.setConnectionTimeout(8000);
- // Send file as the body of the POST request
- File f = new File(args[1]);
- System.out.println("File Length = " + f.length());
- postMethod.setRequestBody(new FileInputStream(f));
- postMethod.setRequestHeader("Content-type",
- "application/octet-stream");
- int statusCode1 = client.executeMethod(postMethod);
- System.out.println("statusLine>>>" + postMethod.getStatusLine());
- postMethod.releaseConnection();
- }
- }
* HTTP Content type 大全
* HttpClient-and-FileUpload
沒有留言:
張貼留言