zoukankan      html  css  js  c++  java
  • 文件上传

    • java 文件上传
        private static String uploadFile(File file, String url) {
            CloseableHttpClient httpClient = null;
            String result = "";
            try {
                httpClient = HttpClients.createDefault();
                HttpPost post = new HttpPost(url);
                RequestConfig config = RequestConfig
                        .custom()
                        .setConnectTimeout(200000)
                        .setConnectionRequestTimeout(200000)
                        .setSocketTimeout(200000)
                        .build();
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                builder.addBinaryBody("image_file", file);
                post.setEntity(builder.build());
                post.setConfig(config);
                HttpResponse response = httpClient.execute(post);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        result = EntityUtils.toString(resEntity, "UTF-8");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    httpClient.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("返回结果:" + result);
            return result;
        }
    故乡明
  • 相关阅读:
    SDN第七次上机作业
    SDN第六次上机作业
    SDN第五次上机实验
    SDN阅读作业(二)
    软件评测——腾讯音视频
    SDN上机第四次作业
    SDN上机第三次作业
    SDN阅读作业
    SpringBoot整合Swagger2
    JavaWeb项目前后端分离
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14074671.html
Copyright © 2011-2022 走看看