<Form method="POST" enctype="multipart/form-data" accept-charset="utf-8" >
此时,浏览器将隐含以 UTF-8 编码,并提交表单中的键值对。
所谓“隐含”,就是不会在各个 Part 中显示指明 Content-Type: text/plian, charset=utf-8
发出去的内容如下:
Accept-Encoding: gzip,deflate,sdch
Content-Type: multipart/form-data; boundary=boudary
--boudary
Content-Disposition: form-data; name="bobi" //表单键值对
utf-8 编码的文字串
--boudary
Content-Disposition: form-data; name="file" filename="太阳花.jpg"
二进制数据
--boudary--
但是,当表单中如果没有指定 accept-charset="utf-8" 时,对于爬虫程序,就不知道该怎么编码字符串,并提交表单了
其一,我如果按 GB2312 作为默认编码,发中国的网站还好,发国外的网站,人家就不认识
其二、我如果按 utf-8 作为字符串编码,那么我要在上面哪个行位置上告诉服务器我用的是 UTF-8 编码的呢?难道是这里:
Content-Disposition: form-data; name="bobi"
Content-Type: text/plain; charset=utf-8
这样写行不行啊?
------解决方案--------------------
表单头加上:
<form method="post" enctype="multipart/form-data" acceptcharset="UTF-8" >
然后使用以下参数启动 jvm:
-Dfile.encoding=UTF-8
------解决方案--------------------
http client 请求?
你这样试试:
Request.setHeader(CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8);
或者这个:
Request.Post("someurl")
.useExpectContinue()
.version(HttpVersion.HTTP_1_1)
.bodyString("Important stuff", ContentType.create("text/plain", Consts.UTF_8))
.execute()
.returnContent().asBytes();
------解决方案--------------------
你加上, <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
------解决方案--------------------
楼主谁告诉你各种浏览器默认GB2312了
另外,你现在的爬虫用的是什么类库,主要爬的网站又是哪些?
文件名有多种编码格式,但是各种浏览器支持的种类不一。不过你是爬虫,理论上服务器应该都支持。
filename=*UTF8''%E5%A4%AA%E9%98%B3%E8%8A%B1.jpg
http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http