Java文字转语音,并写到本地磁盘

1、创建测试类(HttpUtils.java)

package com.jeff.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;

public class HttpUtils {

	/**
	 * 
	 * @description: 从服务器获得一个输入流
	 * @author: Jeff
	 * @date: 2019年12月7日
	 * @return
	 */
	public static InputStream getInputStream(String urlPath) {
		InputStream inputStream = null;
		HttpURLConnection httpURLConnection = null;
		try {
			URL url = new URL(urlPath);
			httpURLConnection = (HttpURLConnection) url.openConnection();
			// 设置网络连接超时时间
			httpURLConnection.setConnectTimeout(3000);
			// 设置应用程序要从网络连接读取数据
			httpURLConnection.setDoInput(true);
			httpURLConnection.setRequestMethod("GET");
			int responseCode = httpURLConnection.getResponseCode();
			System.out.println("responseCode is:" + responseCode);
			if (responseCode == HttpURLConnection.HTTP_OK) {
				// 从服务器返回一个输入流
				inputStream = httpURLConnection.getInputStream();
			} else {
				inputStream = httpURLConnection.getErrorStream();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return inputStream;
	}

	/**
	 * 
	 * @description: 将输入流输出到页面
	 * @author: Jeff
	 * @date: 2019年12月7日
	 * @param resp
	 * @param inputStream
	 */
	public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
		OutputStream out = null;
		try {
			out = resp.getOutputStream();
			int len = 0;
			byte[] b = new byte[1024];
			while ((len = inputStream.read(b)) != -1) {
				out.write(b, 0, len);
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String args[]) {
		// 服务器图片url
		String urlPath = "http://tts.baidu.com/text2audio?per=1&lan=zh&ie=UTF-8&spd=3&text=TEXT";
		// 本地文件夹路径
		String diskPath = "F:\\Jeff\\audios\\";
		// 需要转换为语音的文字
		String text = "100";
		// 从服务器端获得图片,并保存到本地
		FileUtils.saveImageToDisk(getInputStream(urlPath.replace("TEXT", text)), diskPath);
	}

}

2、创建文件流工具类(FileUtils.java)

package com.jeff.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class FileUtils {

	public static final String FORMAT_FILE_NAME = "%d.mp3";

	/**
	 * 
	 * @description: 判断文件夹是否存在,不存在则创建
	 * @author: Jeff
	 * @date: 2019年12月7日
	 * @param dir
	 */
	public static void isDirPathExist(File dir) {
		// 判断文件夹是否存在
		if (dir.isDirectory()) {
			System.out.println(dir + "文件夹已存在");
		} else {
			System.out.println(dir + "文件夹不存在");
			dir.mkdir();
			System.out.println("创建文件夹" + dir);
		}
	}

	/**
	 * 
	 * @description: 从服务器获得输入流InputStream,并写到本地磁盘
	 * @author: Jeff
	 * @date: 2019年12月7日
	 */
	public static void saveImageToDisk(InputStream inputStream, String diskPath) {
		String fileName = String.format(FORMAT_FILE_NAME, new Date().getTime());
		System.out.println("文件名字为:" + fileName);
		byte[] data = new byte[1024];
		int len = 0;
		FileOutputStream fileOutputStream = null;
		try {
			File dirFile = new File(diskPath);
			// 判断文件夹是否存在,不存在则创建
			isDirPathExist(dirFile);
			fileOutputStream = new FileOutputStream(new File(dirFile, fileName));
			while ((len = inputStream.read(data)) != -1) {
				fileOutputStream.write(data, 0, len);
			}
			fileOutputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
		System.out.println(fileName + "写到本地磁盘完成");
	}

}

3、控制台输出结果
在这里插入图片描述
4、本地磁盘结果
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值