文件讀寫操作inputStream轉為byte[] , 將InputStream寫入本地文件


 將InputStream寫入本地文件

    /**
     * 將InputStream寫入本地文件
     *
     * @param destination 寫入本地目錄
     * @param input       輸入流
     * @throws IOException IOException
     */
    public static void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();
    }

 

inputStream轉為byte[]

    /**
     * inputStream轉為byte[]
     *
     * @param inStream
     * @return byte[]
     * @throws IOException
     */
    public byte[] convert2ByteArray(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 1024)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in_b = swapStream.toByteArray();// in_b為轉換之后的結果
        return in_b;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM