Java借助RestTemplate 模拟发送formdata请求(上传文件至fastdfs并获取返回值)

导包

import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

本地文件上传

 /**
     * 本地文件上传
     * @throws FileNotFoundException
     */
    @Test
    public void localPost() throws FileNotFoundException {
    	//fastdfs地址,文件上传地址
	    private String url = "http://xxx.xxx.xxx.xxx:8188/uploadfile4picture";
	    //文件路径
	    private String filePath = "C:\\Users\\rwto\\Desktop\\测试\\图片\\a.jpg";
        RestTemplate rest = new RestTemplate();
        //获取本地文件
        FileSystemResource resource = new FileSystemResource(new File(filePath));
        //将本地文件编辑为formdata格式的数据
        MultiValueMap<String,Object> param = new LinkedMultiValueMap<String,Object>();
        param.add("file", resource);

        //发送post请求,并获取返回信息
        String string = rest.postForObject(url, param, String.class);
        //String转json格式,获取返回的url地址
        JSONObject jsonObject = JSONObject.parseObject(string);
        JSONObject data = JSONObject.parseObject(jsonObject.getString("data"));
        System.out.println(data.getString("file_uri"));
    }

通过文件流上传

   /**
     * 文件流上传
     * @throws IOException
     */
    @Test
    public  void streamPost() throws IOException {
		//fastdfs地址,文件上传地址
	    private String url = "http://xxx.xxx.xxx.xxx:8188/uploadfile4picture";
	    //文件路径
	    private String filePath = "C:\\Users\\rwto\\Desktop\\测试\\图片\\a.jpg";
        RestTemplate rest = new RestTemplate();
        //获取文件流
        final FileInputStream in = new FileInputStream(new File(filePath));
        //新建流资源,必须重写contentLength()和getFilename()
        Resource resource = new InputStreamResource(in){
            //文件长度,单位字节
            @Override
            public long contentLength() throws IOException {
                long size = in.available();
                return size;
            }
            //文件名
            @Override
            public String getFilename(){
                return "a.jpg";
            }
        };
        //将本地文件编辑为formdata格式的数据
        MultiValueMap<String,Object> param = new LinkedMultiValueMap<String,Object>();
        param.add("file", resource);

        //发送post请求,并获取返回信息
        String string = rest.postForObject(url, param, String.class);
        //String转json格式,获取返回的url地址
        JSONObject jsonObject = JSONObject.parseObject(string);
        JSONObject data = JSONObject.parseObject(jsonObject.getString("data"));
        System.out.println(data.getString("file_uri"));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RwTo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值