文件工具类

                                                   网络URL获取流

/**
     * 网络URL 转 InputStream
     *
     * @param url
     * @return
     */
    private static InputStream getImageStream(String url) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                return inputStream;
            }
        } catch (IOException e) {
            log.info("获取网络图片出现异常,图片路径为:" + url);
            e.printStackTrace();
        }
        return null;
    }

                                                 网络URL 转 MultipartFile

 /**
     * 网络URL 转 MultipartFile
     *
     * @param newFileName 新文件名称
     * @param oldFileName 旧文件名称
     * @param conCentType 原文件ContentType  mage/jpeg
     * @param url         网络图片地址
     * @return
     */
    public static MultipartFile getMultipartFile(String newFileName, String oldFileName, String conCentType, String url) throws IOException {
        InputStream imageStream = getImageStream(url);
        return new MockMultipartFile(newFileName, oldFileName, conCentType, imageStream);
    }

                                              文件传给第三方接口

private static int uploadFile(File file, String s3Path) throws IOException {
    URL url = new URL(U);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    // Set up the connection properties
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + "*****");
    OutputStream outputStream = null;
    PrintWriter writer = null;
    FileInputStream fileInputStream = null;
    BufferedReader reader = null;
    try {
        outputStream = connection.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
        fileInputStream = new FileInputStream(file);
        // Add the file part to the request
        String boundary = "*****";
        writer.append("--").append(boundary).append("\r\n");
        writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(s3Path).append("\"\r\n");
        writer.append("Content-Type: ").append("application/octet-stream").append("\r\n\r\n");
        writer.flush();
        // Write the file content
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fileInputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.flush();
        writer.append("\r\n");
        writer.append("--").append(boundary).append("--").append("\r\n");
        writer.flush();
        // Get the response from the server
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);
        if (responseCode != 200) {
            return 500;
        }
        // Read the response from the server (if needed)
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }

    } finally {
        connection.disconnect();
        //关闭流
        if (outputStream != null) {
            outputStream.close();
        }
        if (writer != null) {
            writer.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
        if (reader != null) {
            reader.close();
        }
    }
    return 200;
}

                                                  MultipartFile转File

  public static File toFile(MultipartFile multipartFile) {
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = "." + getExtensionName(fileName);
        File file = null;
        try {
            // 用uuid作为文件名,防止生成的临时文件重复
            file = File.createTempFile(IdUtil.simpleUUID(), prefix);
            // MultipartFile to File
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

                         inputStream 转 File

     static File inputStreamToFile(InputStream ins, String name) throws Exception {
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
        if (file.exists()) {
            return file;
        }
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }

  • 18
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值