httpclient中文乱码问题

        HttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost httppost = new HttpPost(“http://localhost:8080/servlet/UploadServlet”); 

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(Charset.forName("UTF-8"));//设置请求的编码格式
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//设置浏览器兼容模式
        builder.addTextBody("projectNo", "dfdsfd");
        ContentType contentType = ContentType.create("text/plain",Charset.forName("UTF-8"));//解决中文乱码
        builder.addTextBody("project", "我看看",contentType);

        builder.addBinaryBody("file1",new File("C:/截图.png") );//增加附件,第二个参数可以是File,byte[],InputStream,中文文件名也可以
        httppost.setEntity(builder.build());
      
        HttpResponse response = httpclient.execute(httppost); 

       备注:多数解决方案是不使用addTextBody而使用addPart与StringBody,但StringBody已不建议使用。




你可能感兴趣的:(java,httpclient文件上传,乱码)