2011-02-14 53 views
0

我可以使用jetty的ServletTester通過POST方法發送文件嗎?怎麼樣?我試圖發送原始請求字符串:jetty ServletTester發佈文件?

String reqString = "POST /avScan HTTP/1.1\r\n" + 
       "Host: tester\r\n" + 
       "\r\n" + 
       "Content-Type: multipart/form-data; boundary=---------------------------121396480018623072891156223118\r\n" + 
       "Content-Length: 63\r\n" + 
       "-----------------------------121396480018623072891156223118\r\n" + 
       "Content-Disposition: form-data; name=\"file\"; filename=\"test-file\"\r\n" + 
       "Content-Type: application/octet-stream\r\n" + 
       "Just some text file.\r\n" + 
       "-----------------------------121396480018623072891156223118--\r\n"; 

ServletTester tester = new ServletTester(); 
tester.addServlet(FileReceiver.class, "/avScan"); 
tester.start(); 

HttpTester request = new HttpTester(); 
request.setContent(reqString); 

String responseString = tester.getResponses(request.generate()); //Exception thrown 
HttpTester response = new HttpTester(); 
response.parse(responseString); 

不過,我得到java.lang.IllegalStateException: State==HEADER,我在代碼中標記它。

我可以使用ServletTester測試POST servlet接收文件嗎?如果沒有,你會推薦什麼工具?

回答

2

從碼頭的MultipartFilterTest.java

HttpTester request = new HttpTester(); 
    String boundary="XyXyXy"; 
    request.setHeader("Content-Type","multipart/form-data; boundary="+boundary); 
    String content = "--" + boundary + "\r\n"+ 
    "Content-Disposition: form-data; name=\"fileup\"; filename=\"test.upload\"\r\n"+ 
    "Content-Type: application/octet-stream\r\n\r\n"+ 
    "How now brown cow."+ 
    "\r\n--" + boundary + "-\r\n\r\n"; 

    request.setContent(content); 
+0

*保存。我的。生活*你真棒。 – Coderer 2011-07-18 21:49:15