具體實現(xiàn)過程請看如下實例:(推薦學(xué)習(xí):java視頻教程)
private void savePic(InputStream inputStream, String fileName) { OutputStream os = null; try { String path = "D:\testFile\"; // 2、保存到臨時文件 // 1K的數(shù)據(jù)緩沖 byte[] bs = new byte[1024]; // 讀取到的數(shù)據(jù)長度 int len; // 輸出的文件流保存到本地文件 File tempFile = new File(path); if (!tempFile.exists()) { tempFile.mkdirs(); } os = new FileOutputStream(tempFile.getPath() + File.separator + fileName); // 開始讀取 while ((len = inputStream.read(bs)) != -1) { os.write(bs, 0, len); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 完畢,關(guān)閉所有鏈接 try { os.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
推薦教程:java入門教程