zoukankan      html  css  js  c++  java
  • 处理httpClient上传附件时 附件名中文乱码问题

    import java.io.File;
    import java.io.IOException;
    import java.nio.charset.Charset;

    import org.apache.http.Consts;
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.entity.mime.content.StringBody;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.CharsetUtils;
    import org.apache.http.util.EntityUtils;

    public class FileUploadTest {

    /**
    * 这个例子展示了如何执行请求包含一个多部分编码的实体 模拟表单提交
    *
    * @throws IOException
    */
    public static void main(String[] args) throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();

    try {
    // 要上传的文件的路径
    String filePath = new String("D:\吹风.gif");
    // 把一个普通参数和文件上传给下面这个地址 是一个servlet
    HttpPost httpPost = new HttpPost(
    "http://localhost:8080/abc/updateUserBgImg");
    // 把文件转换成流对象FileBody
    File file = new File(filePath);
    FileBody bin = new FileBody(file);
    StringBody userId = new StringBody(
    "用户ID", ContentType.create(
    "text/plain", Consts.UTF_8));
    //以浏览器兼容模式运行,防止文件名乱码。
    HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
    .addPart("multipartFile", bin)
    .addPart("userId", userId).setCharset(CharsetUtils.get("UTF-8")).build();

    httpPost.setEntity(reqEntity);

    System.out.println("发起请求的页面地址 " + httpPost.getRequestLine());
    // 发起请求 并返回请求的响应
    CloseableHttpResponse response = httpClient.execute(httpPost);
    try {
    System.out.println("----------------------------------------");
    // 打印响应状态
    System.out.println(response.getStatusLine());
    // 获取响应对象
    HttpEntity resEntity = response.getEntity();
    if (resEntity != null) {
    // 打印响应长度
    System.out.println("Response content length: "
    + resEntity.getContentLength());
    // 打印响应内容
    System.out.println(EntityUtils.toString(resEntity,
    Charset.forName("UTF-8")));
    }
    // 销毁
    EntityUtils.consume(resEntity);
    } finally {
    response.close();
    }
    } finally {
    httpClient.close();
    }
    }

    }

  • 相关阅读:
    移动端html的overflow:hidden属性失效问题
    js获取url传递参数,js获取url?号后面的参数
    zoom和transform:scale的区别
    css媒体查询来书写二倍图三倍图设置
    ajax和promise的结合使用
    react-router 嵌套路由 内层route找不到
    antd中按需加载使用react-app-rewired报错
    ts+antd报错error TS2605: JSX element type Xxx is not a constructor function for JSX elements
    在taro中跳转页面的时候执行两遍componentDidMount周期的原因和解决方法
    HDU 4602 Partition (矩阵乘法)
  • 原文地址:https://www.cnblogs.com/ZJ0065/p/9711531.html
Copyright © 2011-2022 走看看