FileOutputStream與FileInputStream互相轉換


List<InstorageNoticeDto> noticeList = null;

        FileOutputStream fos = null;
        FileInputStream is = null;
        File file = new File(myfile.getOriginalFilename());
        int ch = 0;
        try
        {
            is = (FileInputStream) myfile.getInputStream();
            fos = new FileOutputStream(file);
            while ((ch = is.read()) != -1)
            {
                fos.write(ch);
            }
        } catch (IOException e1)
        {
            throw new Exception("文件導入失敗,失敗原因:" + e1.getMessage());
        } finally
        {
            try
            {
                fos.flush();
                fos.close();
            } catch (IOException e)
            {
                throw new Exception("文件導入失敗,失敗原因:" + e.getMessage());
            }
        }

 

這樣通過int值把InputStream流改成OutputStream流完成,不必考慮通過字節數組的長度問題


免責聲明!

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



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