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

    @ResponseBody  
    @RequestMapping(value = "/upload.do", method = RequestMethod.POST)  
    public String upload(String fileName, MultipartFile jarFile) {  
        // 下面是测试代码  
        System.out.println(fileName);  
        String originalFilename = jarFile.getOriginalFilename();  
        System.out.println(originalFilename);  
        try {  
            String string = new String(jarFile.getBytes(), "UTF-8");  
            System.out.println(string);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        // TODO 处理文件内容...  
        return "OK";  
    }  


    @Test  
    public void testUpload() throws Exception {  
        String url = "http://127.0.0.1:8080/test/upload.do";  
        String filePath = "C:\Users\MikanMu\Desktop\test.txt";  
      
        RestTemplate rest = new RestTemplate();  
        FileSystemResource resource = new FileSystemResource(new File(filePath));  
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();  
        param.add("jarFile", resource);  
        param.add("fileName", "test.txt");  
      
        String string = rest.postForObject(url, param, String.class);  
        System.out.println(string);  
    } 
    String string = rest.postForObject(url, param, String.class);  
    可以换成以下方式 HttpEntity
    <MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String,Object>>(param); ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.POST, httpEntity, String.class); System.out.println(responseEntity.getBody());
         
    @PostMapping(value = "/uploadFile")
        @ResponseBody
        @ApiOperation(value = "上传附件", notes = "返回结果,SUCCESS:200,FAILED:500", httpMethod = "POST")
        public String upload(@ApiParam MultipartFile importFile) throws Exception {
            String suffix = importFile.getOriginalFilename().substring(importFile.getOriginalFilename().lastIndexOf("."));
            String showUrl = aLiYunOssService.upload(importFile.getBytes(), suffix);
            return showUrl;
    
        }
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    ByteArrayResource arrayResource = new ByteArrayResource(getBytes(),"test.jpg");
    parts.add(
    "importFile", arrayResource);
    HttpEntity
    <MultiValueMap<String, Object>> httpEntity2 = new HttpEntity<MultiValueMap<String,Object>>(parts);
    ResponseEntity
    <String> responseEntity2 = rest.exchange(url, HttpMethod.POST, httpEntity2, String.class);
    System.out.println(
    "返回地址1==="+responseEntity2.getBody());
  • 相关阅读:
    golang删除数组某个元素
    golang用通道实现信号量,控制并发个数
    什么是ScaleIO中的forwards rebuild和backwards rebuild?
    SQL Server中的database checkpoint
    如何将thick provision lazy zeroed的VMDK文件转换为thick provision eager zeroed?
    LoadTestAgentResultsLateException in VS2010
    SQL Server Instance无法启动了, 因为TempDB所在的分区没有了, 怎么办?
    VMware vCenter中, 如何辩认虚机上Raw Device Mapping过了的一块物理磁盘?
    SQL Server AlwaysOn Setup Step-By-Step Guide
    TPC-E在populate测试Database时需要注意的一些事项
  • 原文地址:https://www.cnblogs.com/yy123/p/6956720.html
Copyright © 2011-2022 走看看