Httpclient文件上传

2023-05-16

public static void upload(String url,File file,String filename) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
            httppost.setConfig(requestConfig);
            FileBody bin = new FileBody(file);
            StringBody comment = new StringBody(filename, ContentType.TEXT_PLAIN);
            HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("filename", comment).build();
            httppost.setEntity(reqEntity);
            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    String responseEntityStr = EntityUtils.toString(response.getEntity());
                    System.out.println(responseEntityStr);
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  

@PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file, String filename){
        Result s = new Result(1, "success");
        String usrHome = System.getProperty("user.home");
        try {
            String path = usrHome+"/image/";
            path = path.replace("\\","/");
            System.out.println(path);
            File f = new File(path);
            if(!f.exists()){
                f.mkdirs();
            }
            UploadUtils.uploadFileTest(file,path,filename);
        }catch (Exception e){
            s.setCode(0);
            s.setMessage("失败");
        }
        return s ;
    }

  


public static  void uploadFileTest(MultipartFile zipFile,String targetFilePath,String fileName) {
        File targetFile = new File(targetFilePath + File.separator + fileName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFile);
            IOUtils.copy(zipFile.getInputStream(), fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
            }
        }
    }  

 

 

 

 


<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>

 

转载于:https://www.cnblogs.com/syscn/p/11622840.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Httpclient文件上传 的相关文章

随机推荐

  • Python 文件操作Error: binary mode doesn't take an encoding argument

    Python 报错 xff1a ValueError binary mode doesn 39 t take an encoding argument 在运行文件操作相关功能时报错 xff1a ValueError binary mode
  • Mac OS X 完全卸载MySQL

    执行下列命令 sudo rm usr local mysqlsudo rm rf usr local mysql sudo rm rf Library StartupItems MySQLCOMsudo rm rf Library Pref
  • 揭秘微信红包:架构、抢红包算法、高并发和降级方案

    编者按 与传统意义上的红包相比 xff0c 近两年火起来的 红包 xff0c 似乎才是如今春节的一大重头戏 历经上千年时代传承与变迁 xff0c 春节发红包早已成为历史沉淀的文化习俗 xff0c 融入了民族的血脉 按照各家公布的数据 xff
  • iOS 富文本所有的NSAttributedStringKey

    NSAttributedStringKey xff1a 1 NSFontAttributeName 字体 xff0c value是UIFont对象 2 NSParagraphStyleAttributeName 绘图的风格 xff08 居中
  • android抓取logcat日志的方法

    这几天帮忙测试一个APP xff0c 报告结果需要提交日志文件 xff0c 于是百度了下安卓的获取日志方法 xff0c 其实很简单 xff0c 7个步骤搞定 xff0c 下面把我的总结分享给大家 1 下载adb工具包 https pan b
  • 如何获得大学教材的PDF版本?

    最近急需一本算法书的配套答案 xff0c 这本配套单独出售 xff0c 好像在市面上还买不到 xff0c 在淘宝上搜索也只是上一个版本 xff0c 并没有最新版本 xff0c 让我很无奈 加上平时肯定会有这么一种情况 xff0c 想看一些书
  • 创新的力量

    创新是个非常好的词 xff0c 虽然这个词已经被用滥了 xff0c 但我依然固执的认为这是一个充满了迷人光辉的词汇 如果把创新放入科技领域 xff0c 这应该是我在科技领域最喜欢的一个词了 我常常对同事或团队的成员说 xff0c 我们在做产
  • mysql连接池

    文章出处 https www cnblogs com wupeiqi articles 8184686 html DBUtils是Python的一个用于实现数据库连接池的模块 此连接池有两种连接模式 xff1a 模式一 xff1a 为每个线
  • Python操作MySQL

    文章出处 https www cnblogs com wupeiqi articles 5713330 html 本篇对于Python操作MySQL主要使用两种方式 xff1a 原生模块 pymsqlORM框架 SQLAchemy pyms
  • 免费的天气API

    高德地图 的提供了免费查看天气预报API xff08 https lbs amap com api webservice guide api weatherinfo xff09 xff0c 每日限制调100000次 xff1b 1 和风天气
  • final link failed: Nonrepresentable section on output

    编译live555的时候遇到了这个问题 xff0c 前面的编译没有问题 xff0c 是在链接的时候出现的 xff0c 在网上搜索说是缺少 libstdc 43 43 库 于是 xff0c 安装之 sudo apt get install l
  • centos执行apt-get提示不存在

    在centos下用yum install xxx yum和apt get的区别 一般来说著名的linux系统基本上分两大类 xff1a 1 RedHat系列 xff1a Redhat Centos Fedora等 2 Debian系列 xf
  • C语言用指针实现两个数组值互换

    C语言用指针实现两数组的值互换 span class hljs preprocessor include lt stdio h gt span span class hljs preprocessor define N 10 span sp
  • onclick或者其他事件在部分移动端无效的问题

    最近开发碰到一个问题 xff0c 大多数手机都可以正常访问点击 xff0c 但是有部分手机onclick无效 xff0c 不知道可能是什么原因 xff1f 该如何解决 我遇到的这个问题 xff0c 实际不是onclick的原因 xff0c
  • Linux软件的安装和卸载

    Linux软件的安装和卸载一直是困扰许多新用户的难题 在Windows中 xff0c 我们可以使用软件自带的安装卸载程序或在控制面板中的 添加 删除程序 来实现 与其相类似 xff0c 在Linux下有一个功能强大的软件安装卸载工具 xff
  • 凝思linux系统显卡设置,TaiShan服务器安装凝思操作系统Linx6.0.90并设置独立显卡WX2100输出...

    TaiShan服务器安装凝思操作系统Linx6 0 90并设置独立显卡WX2100输出 环境准备 xff1a TaiShan 2280服务器 xff0c 凝思操作系统Linx6 0 90 arm64 xff0c WX2100显卡 xff0c
  • 华为TaiShan 2280 ARM 服务器

    华为TaiShan 2280 ARM 服务器 华为TaiShan 2280 ARM 服务器 https e huawei com cn products cloud computing dc servers arm based taisha
  • InfoQ推荐语:我的梦想

    大家好 非常高兴能在这一期的架构师和大家见面 本人工作了十余载 先后在洪恩软件和用友集团任职 目前任职于用友集团瑞友科技研究院 主要关注领域 企业应用软件平台研发 领域驱动设计 OSGi 动态语言应 用 云计算 移动互联和Mac OS平台相
  • python TKinter的主窗口运行程序完毕后,怎么让其自动关闭

    如题 xff1a 在pycharm 调试Tkinter程序的时候 xff0c 关闭右上角的X 实际上并未退出进程 xff0c 长期以往 再大的内存也会被耗尽 一般就是下面的代码 xff1a 34 34 34 from tkinter imp
  • Httpclient文件上传

    public static void upload String url File file String filename CloseableHttpClient httpclient 61 HttpClients createDefau