今天在為公司寫網(wǎng)絡(luò)硬盤的壓力測(cè)試工具時(shí)使用了HttpClient,比較容易的解決了登錄、上傳、下載、列表等功能的測(cè)試,但也遇到了一些問題,先看看代碼:

 1   File f1 = new File("D:\\download\\aa.txt");
 2   PostMethod filePost = new PostMethod("http://10.3.3.106:8090/~jid=aWHLXB6y4O9a/webdrive/upload.do?contentID=99999820&action=submit");
 3   try{
 4       Part[] parts = {new FilePart("file0", f1)};
 5       filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
 6   }catch(FileNotFoundException e){
 7       System.out.println("===================file not found exception: " + e);
 8   }
 9   HttpClient client = new HttpClient();
10   // 若上傳的文件比較大 , 可在此設(shè)置最大的連接超時(shí)時(shí)間 
11   //client.getHttpConnectionManager(). getParams().setConnectionTimeout(5000); 
12   int status = 0;
13   try {
14       
15        status = client.executeMethod(filePost);
16                
17   } catch (HttpException e) {
18        System.out.println("===================http exception: " + e);
19   } catch (IOException e) {
20        System.out.println("===================io exception: " + e);
21   }finally {
22        filePost.releaseConnection();
23   }
24   if (status == HttpStatus.SC_OK) {
25       System.out.println("============================UpLoad file OK! ");
26   } 

     本來URL中的contentID和action兩個(gè)參數(shù)我是通過StringPart來傳遞的,結(jié)果傳遞上去的值就成了:
     action=
Content-Transfer-Encoding: 8bit
     submit
     而且目前這種方法上傳的文件,也會(huì)在文件內(nèi)容的第一行中出現(xiàn):Content-Transfer-Encoding: 8bit,由于不影響測(cè)試的效果,所以就擱置了,等有空時(shí)再來研究這個(gè)問題,如果有朋友知道問題的原因,也請(qǐng)一定不要吝嗇您的回答哦。