今天在为公司写网络硬盘的压力测试工具时使用了HttpClient,比较容易的解决了登录、上传、下载、列表等功能的测试,但也遇到了一些问题,先看看代码:
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 // 若上传的文件比较大 , 可在此设置最大的连接超时时间
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 }
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 // 若上传的文件比较大 , 可在此设置最大的连接超时时间
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两个参数我是通过StringPart来传递的,结果传递上去的值就成了:
action=
Content-Transfer-Encoding: 8bit
submit
而且目前这种方法上传的文件,也会在文件内容的第一行中出现:Content-Transfer-Encoding: 8bit,由于不影响测试的效果,所以就搁置了,等有空时再来研究这个问题,如果有朋友知道问题的原因,也请一定不要吝啬您的回答哦。
本文介绍使用HttpClient进行文件上传时遇到的问题及解决方案。作者在实现网络硬盘压力测试工具的过程中,使用HttpClient完成登录、上传等功能,但在文件上传时遇到了参数传递错误和文件内容异常问题。
367

被折叠的 条评论
为什么被折叠?



