首页 > 编程语言 > 详细

Java后端模拟前端请求

时间:2020-05-12 19:23:49      阅读:88      评论:0      收藏:0      [点我收藏+]

在知道源文件目录的情况下,直接在后端执行上传

 

package com.example.hystrix.controller;

import org.springframework.core.io.FileSystemResource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;

public class Demo{

    public void uploadTest() {

        String url = "http://localhost:8080/upload"; //上传的地址
        String filePath = "D:/test/test.mp4";

        RestTemplate rest = new RestTemplate();
        FileSystemResource resource = new FileSystemResource(new File(filePath));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", resource); //MultipartFile的名称
        String restResult = rest.postForObject(url, param, String.class);
        System.out.println(restResult);
    }
}

 

Java后端模拟前端请求

原文:https://www.cnblogs.com/Alwaysbecoding/p/12877950.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!