java+post+http文件上传

依赖两个jar包

 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
 </dependency>
<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.3</version>
</dependency>

java代码

   /**
     * 文件上传支持中文
     *
     * @param reportUrl
     * @param filepath
     * @param userId
     * @param systemName
     * @throws IOException
     */
    public static void uploadReport(String reportUrl, String filepath,
                                    String userId, String systemName) {
        CloseableHttpClient closeableHttpClient = null;
        CloseableHttpResponse response = null;
        try {
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(reportUrl);
            String filename = filepath.split("/")[filepath.split("/").length - 1];
            File file = new File(filepath);

            FileBody fileBody = new FileBody(file, ContentType.create("multipart/form-data", "UTF-8"), filename);
            StringBody userIdStringBody = new StringBody(userId, ContentType.create("multipart/form-data", "UTF-8"));
            StringBody systemNameStringBody = new StringBody(systemName, ContentType.create("multipart/form-data", "UTF-8"));
            StringBody reportNameStringBody = new StringBody(filename, ContentType.create("multipart/form-data", "UTF-8"));

            HttpEntity httpEntity = MultipartEntityBuilder
                    .create()
                    .setCharset(Charset.forName("UTF-8"))
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addPart("file", fileBody)
                    .addPart("userId", userIdStringBody)
                    .addPart("systemName", systemNameStringBody)
                    .addPart("reportName", reportNameStringBody)
                    .build();

            httpPost.setEntity(httpEntity);
            response = closeableHttpClient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                System.out.println("上传成功");
            } else {
                BufferedReader reader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
                StringBuffer buffer = new StringBuffer();
                String str = "";
                while ((str = reader.readLine()) != null && (str = reader.readLine()).trim().length() > 0) {
                    buffer.append(str);
                }
                System.out.println(buffer.toString());
                System.out.println("上传失败:" + statusCode);
            }
            closeableHttpClient.close();
            if (response != null) {
                response.close();
            }

        } catch (Exception ex) {
            System.out.println("uploadReport发生异常:" + ex);
        } finally {
            try {
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值