/**
* 下载录音
*/
@GetMapping("/down")
public void downVoice(String sid, HttpServletResponse response) {
String id = sid;
/*String filePath = "/mnt/";
String fileName = "Model.txt";*/
// String path = filePath + fileName;
/* String userInfoId = service.getById(id).getUserInfoId();
String getuaId = mapper.getuaId(userInfoId);
String visitRecord = mapper.getVisitRecord(getuaId);*/
String visitRecord = service.getById(id).getRadioUrl();
if (StringUtils.isNotEmpty( visitRecord)) {
String path = visitRecord;
String suffixes="avi|mpeg|3gp|mp3|mp4|wav|jpeg|gif|jpg|png|apk|exe|pdf|rar|zip|docx|doc";
//正则判断
Pattern pat= Pattern.compile("[\\w]+[\\.]("+suffixes+")");
//条件匹配
// String path = "D://QR/thunder.mp3";
// String path = "http://data.huiyi8.com/yinxiao/mp3/85764.mp3";
Matcher mc=pat.matcher(path);
response.setContentType("application/octet-stream;charset=UTF-8");
String uuid = IdUtil.simpleUUID();
while(mc.find()){
//截取文件名后缀名
String suffix = mc.group();
response.setHeader("Content-Disposition", "attachment;filename=" + new String((uuid + suffix)));
}
try {
URL url = new URL(path);
//open获取url
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = conn.getInputStream();
byte[] getData = readInputStream(inputStream);
//以流的形式下载文件
// FileInputStream stream = new FileInputStream(path);
InputStream fis = new BufferedInputStream(inputStream);
byte[] buffer = new byte[fis.available()];
// fis.read(buffer);
fis.read(getData);
fis.close();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
// toClient.write(buffer);
toClient.write(getData);
toClient.flush();
toClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Validator.validateFalse(true,"无录音文件");
}
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
下载url
最新推荐文章于 2023-11-29 17:10:01 发布