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、本地磁盘结果