使用multipartentitybuilder进行文件上传的时候出现了一些问题,调试的时候
public static void postFile(String filename, String url, String number) throws IOException { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);// 设置游览器兼容模式 builder.setBoundary(UUID.randomUUID().toString()); builder.setCharset(Charset.forName(HTTP.UTF_8)); File file = new File(filename); FileBody fileBody = new FileBody(file); builder.addPart("file", fileBody); builder.addTextBody("number", number); post.setEntity(builder.build()); try { HttpResponse response = client.execute(post); final HttpEntity entity = builder.build(); if (response.getStatusLine().getStatusCode() == 200) { String content = EntityUtils.toString(entity); Log.e("PostClient", "文件上传成功!" + content); entity.consumeContent(); client.getConnectionManager().shutdown(); } } catch (Exception e) { e.printStackTrace(); } }
解决方案