2016-10-21 12:39:38.0|分类: java|浏览量: 3210
httpclient上传文件功能,是一个很好的工具,但是最近使用它上传图片,接受端一直判断不出这个流是图片,接受端做为流保存到本地,不能直接识别出图片类型,直接显示。 代码: CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://xxxxxxxxxxxx"); MultipartEntityBuilder meBuilder = MultipartEntityBuilder.create(); meBuilder.addTextBody("data", "你好", ContentType.TEXT_PLAIN.withCharset("UTF-8")); meBuilder.addBinaryBody("attachments[]", new File("C:\\Users\\tianbx\\Desktop\\123123.jpg")); meBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); HttpEntity reqEntity = meBuilder.build(); httpPost.setEntity(reqEntity); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine()); // HttpEntity entity2 = response2.getEntity(); // // do something useful with the response body // // and ensure it is fully consumed // EntityUtils.consume(entity2); } finally { response2.close(); } 执行完这段代码发现,接受端没有识别出上传文件是一个图片,认为是一个流。经过debug分析发现httpclient头设置内容如下: [Content-Disposition: form-data; name="attachments[]"; filename="123123.jpg", Content-Type: application/octet-stream, Content-Transfer-Encoding: binary] 分析结果:Content-Type类型设置的是流,并不是图片类型。 经过查看httpclient代码发现有一个方法能设置Content-Type meBuilder.addPart("attachments[]", new FileBody(new File("C:\\Users\\tianbx\\Desktop\\123123.jpg"), "image/jpeg", "UTF-8")); 经过测试接受端还是不能识别出图片类型,经过debug分析发现httpclient头设置内容如下: [Content-Disposition: form-data; name="attachments[]"; filename="123123.jpg", Content-Type: image/jpeg; charset=UTF-8, Content-Transfer-Encoding: binary] 分析结果:Content-Type设置图片类型了,怎么接受端还是不能识别出图片类型? Content-Type设置图片类型image/jpeg;java代码有问题,那就看看浏览器是否能正确上传图片,浏览器中上传了一张图片,接收端能正确解析出文件是图片类型,看看请求头的打印信息: 43718 ------WebKitFormBoundaryTllA6B0LULvGM6v9 Content-Disposition: form-data; name="upfile"; filename="3.5KG翻新水-300.jpg" Content-Type: image/jpeg 经过发现浏览器设置Content-Type时并没有设置编码,估计编码在搞鬼,去掉编码 meBuilder.addPart("attachments[]", new FileBody(new File("C:\\Users\\tianbx\\Desktop\\123123.jpg"), "image/jpeg")); 分析结果:接受端正确是识别出图片类型,经过debug分析发现httpclient头设置内容如下: [Content-Disposition: form-data; name="attachments[]"; filename="123123.jpg", Content-Type: image/jpeg, Content-Transfer-Encoding: binary] 总结:httpclient提供的ContentType类型中没有image/jpeg,文件类型默认的都是application/octet-stream。需要自己设置图片类型,图片类型通过HttpURLConnection.guessContentTypeFromName()获取 httpclient提供的ContentType类型有: // constants public static final ContentType APPLICATION_ATOM_XML = create( "application/atom+xml", Consts.ISO_8859_1); public static final ContentType APPLICATION_FORM_URLENCODED = create( "application/x-www-form-urlencoded", Consts.ISO_8859_1); public static final ContentType APPLICATION_JSON = create( "application/json", Consts.UTF_8); public static final ContentType APPLICATION_OCTET_STREAM = create( "application/octet-stream", (Charset) null); public static final ContentType APPLICATION_SVG_XML = create( "application/svg+xml", Consts.ISO_8859_1); public static final ContentType APPLICATION_XHTML_XML = create( "application/xhtml+xml", Consts.ISO_8859_1); public static final ContentType APPLICATION_XML = create( "application/xml", Consts.ISO_8859_1); public static final ContentType MULTIPART_FORM_DATA = create( "multipart/form-data", Consts.ISO_8859_1); public static final ContentType TEXT_HTML = create( "text/html", Consts.ISO_8859_1); public static final ContentType TEXT_PLAIN = create( "text/plain", Consts.ISO_8859_1); public static final ContentType TEXT_XML = create( "text/xml", Consts.ISO_8859_1); public static final ContentType WILDCARD = create( "*/*", (Charset) null); 识别文件类型代码如下: String contentType = HttpURLConnection.guessContentTypeFromName("C:\\Users\\tianbx\\Desktop\\123123.jpg"); 代码使用到的包: |