java post文件,Java中的Http POST(带文件上传)

What I want to do is submit a web form from a java application. The form I need to fill out is located here: http://cando-dna-origami.org/

When the form is submitted, the server sends a confirmation email to the email address given, which for now I'm just checking by hand. I've tried filling out the form manually, and the emails get sent fine. (It should also be noted that when the form is filled out incorrectly, the page just refreshes and doesn't give any feedback).

I've never done anything with http before, but I looked around for a while, and came up with the following code, which is supposed to send a POST request to the server:

String data = "name=M+V&affiliation=Company&email="

+ URLEncoder.encode("m.v@gmail.com", "UTF-8")

+ "&axialRise=0.34&helixDiameter=2.25&axialStiffness=1100&bendingStiffness=230" +

"&torsionalStiffness=460&nickStiffness=0.01&resolution=course&jsonUpload="

+ URLEncoder.encode("C:/Users/Marjie/Downloads/twisted_DNA_bundles/monotwist.L1.v1.json",

"UTF-8") + "&type=square";

URL page = new URL("http://cando-dna-origami.org/");

HttpURLConnection con = (HttpURLConnection) page.openConnection();

con.setDoOutput(true);

con.setRequestMethod("POST");

con.connect();

OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());

out.write(data);

out.flush();

System.out.println(con.getResponseCode());

System.out.println(con.getResponseMessage());

out.close();

con.disconnect();

However, when it runs it doesn't appear to do anything - that is, I don't get any emails, although the program does print "200 OK" to System.out, which seems to indicate that something got received from the server, although I'm not sure what it means exactly. I think the problem might be in the file uploading, since I wasn't sure whether that data type required a different format.

Is this a correct way to send a POST request using Java? Do I need to do something different for the file uploading? Thanks!

After reading Adam's post, I used Apache HttpClient and wrote the following code:

List params = new ArrayList();

params.add(new BasicNameValuePair("type", "square"));

//... add more parameters

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);

HttpPost post = new HttpPost("http://cando-dna-origami.org/");

post.setEntity(entity);

HttpResponse response = new DefaultHttpClient().execute(post);

post = new HttpPost("http://cando-dna-origami.org/");

post.setEntity(new FileEntity(new File("C:/Users/Marjie/Downloads/twisted_DNA_bundles/monotwist.L1.v1.json"), "text/plain; charset=\"UTF-8\""));

HttpResponse responseTwo = new DefaultHttpClient().execute(post);

However, it still doesn't seem to be working; again, I wasn't sure how the uploaded file fit into the form, so I tried just sending two separate POST requests, one with the form and one with the other data. I am still looking for a way to combine these into one request; does anybody know something about this?

解决方案

You would probably be better off using something like Apache HttpClient, with which you can build up a POST request programatically.

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://.../whatever");

List params = new ArrayList();

params.add(new BasicNameValuePair("param1", "value1"));

params.add(new BasicNameValuePair("param2", "value2"));

...

httpost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

HttpResponse response = httpclient.execute(httppost);

If you need to upload a file along with your form, you will need to use a MultipartEntity instead:

MultipartEntity reqEntity = new MultipartEntity();

reqEntity.addPart("someParam", "someValue");

reqEntity.addPart("someFile", new FileBody("/some/file"));

....

httpost.setEntity(reqEntity);

There are some sample programs over on their site. The "Form based logon" and "Multipart encoded request entity" are good examples to start from.

It may also be worthwhile testing out your connections and taking a look at the underlying network data to see what is happening. Something like Firebug will let you see exactly what is happening in your browser, and you can turn up the HttpClient logging to see all of the data exchanged in your program. Alternatively, you can use something like Wireshark or Fiddler to watch your network traffic in real-time. This may give you a better idea of exactly what your browser is doing, versus what your program is doing.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值