zoukankan      html  css  js  c++  java
  • spring boot文件上传、http数据服务API的DEMO

    文件上传

    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    @Controller
    @RequestMapping(value = "/file")
    public class FileController {
    
        private static final Logger logger = LoggerFactory.getLogger(FileController.class);
    
        @RequestMapping(value = "upload")
        @ResponseBody
        public String upload(@RequestParam("roncooFile") MultipartFile file) {
            System.out.println("-------------------------");
            
            if (file.isEmpty()) {
                return "文件为空";
            }
    
            // 获取文件名
            String fileName = file.getOriginalFilename();
            logger.info("上传的文件名为:" + fileName);
    
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            logger.info("上传的后缀名为:" + suffixName);
    
            // 文件上传路径
            String filePath = "d:/test/";
    
            // 解决中文问题,liunx下中文路径,图片显示问题
            //fileName = UUID.randomUUID() + suffixName;
            
            File dest = new File(filePath + fileName);
    
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
    
            try {
                file.transferTo(dest);
                return "上传成功";
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "上传失败";
        }
    
    }

    http服务调用

    import org.springframework.web.client.RestTemplate;
    
    public class test {
    
        public static void main(String[] args) {
            String url = "http://www.baidu.com";
            RestTemplate template = new RestTemplate();
            String str = template.getForObject(url,String.class);
            System.out.println(str);
        }
    
    }
  • 相关阅读:
    1081. Rational Sum (20) -最大公约数
    在Debug模式下中断, 在Release模式下跳出当前函数的断言
    net-snmp配置文件详解
    net-snmp5.7.3移植到arm-linux平台
    NET-SNMP开发——日志输出
    SNMP常用数据操作
    40 网络相关函数(八)——live555源码阅读(四)网络
    39 网络相关函数(七)——live555源码阅读(四)网络
    38 网络相关函数(六)——live555源码阅读(四)网络
    37 网络相关函数(五)——live555源码阅读(四)网络
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/8630819.html
Copyright © 2011-2022 走看看