美文网首页
linux命令之curl

linux命令之curl

作者: 程序员小白成长记 | 来源:发表于2020-08-06 10:49 被阅读0次

    curl上传文件

    后端是用的multipart控件接收

    curl -X'POST' url -F"param1=value1" -F "file=@/home/work/以父之名.pdf"
    

    测试代码清单一

    • curl
    curl -XPOST http://localhost:8700/api/v1/ceph/upload/file/kbase -F "file=@/home/ubuntu/sun/father.pdf"
    
    • Controller
    @ResponseBody
    @RequestMapping(value = "/upload/file/{bucketName}", method = RequestMethod.POST)
    public void uploadFile(@RequestParam(value = "file") MultipartFile file
            , HttpServletRequest request, @PathVariable String bucketName) {
    
        logger.debug("Enter into uploadFile");
        // 1, 获取文件名称
        String fileName = file.getOriginalFilename();
        logger.debug("[CephController][uploadFile] fileName: {}", fileName);
        // 2, 构造objectKey, objectKey生成规则, resContentId:ts:fileNameEncodeStr
        StringBuffer objectKey = new StringBuffer();
        objectKey.append(System.currentTimeMillis())
                .append(":").append(fileName);
        String objectKeyEncodeStr = "";
        try {
            // 3, objectKey编码,":"在浏览器会被自动转义为%3a
            objectKeyEncodeStr = URLEncoder.encode(objectKey.toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.debug("file name encode error: {}", e);
            throw new RuntimeException("Fail to parse file name");
        }
        logger.debug("[CephController][uploadFile] generate objectKey: {}, encode objectKey: {}"
                , objectKey.toString(), objectKeyEncodeStr);
        try {
            // 4, 获取文件流,将文件以流的方式上传到ceph
            InputStream inputStream = file.getInputStream();
            CephClient cephClient = new CephClient(endpoint, ak, sk);
            cephClient.putObject(bucketName, objectKeyEncodeStr, inputStream, null);
        } catch (IOException e) {
            logger.debug("[CephController][uploadFile][exception] upload ceph fail, e: {}", e);
            throw new RuntimeException("Fail upload file to ceph");
        }
    }
    

    参数

    curl -H

    curl "url" -H "k1: v1" -H "k2: v2"
    

    curl -X

    默认为GET请求,也可通过-X来指定

    -- POST请求
    curl -X'POST
    其中POST指定body体
    eg:
    curl -X'POST' "url"  -d 'json字符串'
    
    -- GET请求
    curl -X'GET'
    

    参考:

    【1】curl 命令详解~~

    相关文章

      网友评论

          本文标题:linux命令之curl

          本文链接:https://www.haomeiwen.com/subject/uulzrktx.html