站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题[求助 急!!!!!!!!]使用HttpURLConnection 模拟multipart/form-data发送文件?格式是什么?

[求助 急!!!!!!!!]使用HttpURLConnection 模拟multipart/form-data发送文件?格式是什么?

摘要: [求助 急!!!!!!!!]使用HttpURLConnection 模拟multipart/form-data发送文件?格式是什么?


URL theURL = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection)theURL.openConnection();

connection.setRequestProperty("","")//具体要设计什么属性?

还有对要发送的文件要做什么处理?

请大虾帮助哈.
谢谢先


我以前写的一个例子,发送短信的:

//begin post
conn.setDoInput(true) ;
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
content=
"mobile="+URLEncoder.encode(MobileNo)+
"&msg="+URLEncoder.encode(ShortMsg)+
"&sname="+URLEncoder.encode("")+
"&user="+URLEncoder.encode(RegMobile)+
"&passwd="+URLEncoder.encode(RegPasswd);

output=new DataOutputStream(conn.getOutputStream());
output.writeBytes(content);
output.flush();
output.close();



uc.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-quickviewplus, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
uc.setRequestProperty("Host", strHost);
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("Cache-Control", "no-cache");



谢谢楼上两位

不过还有个疑问 需要不需要对要发送的文件进行处理


要不要处理?什么意思?


比如我要进行对文件"POST.XML"的发送?
要不要对先文件进行 MIME编码,在写到输出流中?
还是直接写到输出流中?







xml文件?直接写就可以,都是文本啊。


谢谢讲解
还有几个问题能不能解释下
问题1.如果文件是图片或其他格式的文件呢?需不需要处理?

问题2.还有 boundary=---------------------------7d318fd10011 //分隔是不是随便定义的?
问题3.还有个疑问 就是我试了一下,没有设置这些属性也能发送文件 好像没有影响 能不能说下为什么?
我用ethereal抓数据报刊到的,Content-type我设置成application/x-www-form-urlencoded 也是分成多部分发送的。什么原因呀?
这些属性设置有什么用嘛?





uc.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-quickviewplus, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
uc.setRequestProperty("Host", strHost);
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("Cache-Control", "no-cache");


能解释下的话 实在太感谢了



不是很清楚,考来考去的,修改的不多。
如果lz明白了咱也学习学习。


谢谢哈 我问了一下
1.好像不要处理。
2.可以自己定义
3.发送数据可以 服务器端接受要按照格式进行解码。

还是感谢。 给你加分。呵呵

小小进步一下,呵呵
gzpoplar(poplar) 还在不在??

// 发送文件:
sb = sb.append("--");
sb = sb.append(BOUNDARY);
sb = sb.append("/r/n");
sb = sb.append("Content-Disposition: form-data; name=/"1/"; filename=/"1.txt/"/r/n");
sb = sb.append("Content-Type: application/octet-stream/r/n/r/n");
byte[] data = sb.toString().getBytes();
byte[] end_data = ("/r/n--" + BOUNDARY + "--/r/n").getBytes();
// 设置HTTP头:
hc.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + ";boundary=" + BOUNDARY);
hc.setRequestProperty("Content-Length", String.valueOf(data.length + file.length + end_data.length));
// 输出:
output = hc.openOutputStream();
output.write(data);
output.write(file);
output.write(end_data);
// 读取服务器响应:
// TODO...

网上有一段例子是这样的 是不是 还需要这样处理?
URL url = new URL(strURL);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, application/x-quickviewplus, */*");
uc.setRequestProperty("Accept-Language", "zh-cn");
uc.setRequestProperty("Content-type", "multipart/form-data; boundary=---------------------------7d318fd100112");
uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
uc.setRequestProperty("Host", strHost);
uc.setRequestProperty("Connection", "Keep-Alive");
uc.setRequestProperty("Cache-Control", "no-cache");

String strHeader = "";
strHeader += "-----------------------------7d318fd100112/r/n";
strHeader += "Content-Disposition: form-data; name=/"fUpload/"; filename=/"E://xmltemp.xml/"/r/n";
strHeader += "Content-Type: text/xml/n";
String strTail = "";
strTail += "-----------------------------7d318fd100112/r/n";
strTail += "Content-Disposition: form-data; name=/"cbUpload/"/r/n/n";
strTail += "upload/r/n";
strTail += "-----------------------------7d318fd100112--/r/n/n/n";

DataOutputStream dos = new DataOutputStream(uc.getOutputStream());

dos.write(strHeader.getBytes());
dos.writeBytes("/n");

// 直接把string写入dos
//byte[] b = strXML.getBytes("UTF-8");
//dos.write(b, 0, b.length);

// 通过xml文件把数据写入dos
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(postxmlfile);
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setLineWidth(0);
format.setPreserveSpace(true);
XMLSerializer serializer = new XMLSerializer(dos, format);
serializer.asDOMSerializer();
serializer.serialize(doc);

dos.writeBytes("/n");
dos.write(strTail.getBytes());
dos.close();
太感谢了
不过这段代码 没有看懂
能不能解释下
// 通过xml文件把数据写入dos
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(postxmlfile);
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setLineWidth(0);
format.setPreserveSpace(true);
XMLSerializer serializer = new XMLSerializer(dos, format);
serializer.asDOMSerializer();
serializer.serialize(doc);
用到的类我都没有见过
麻烦了


↑返回目录
前一篇: 哪能找到优秀的JAVA源代码
后一篇: 从书上打的程序 怎么编译都通不过呢 大家给看看吧