package com.dc.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
/**
* Project: test
* Package: console.controller
* Author: Yanchao
* CreateTime: 2020-02-17 17:30:48
* Description:
*/
@RestController
@RequestMapping("/down")
public class DownFileController {
RestTemplate restTemplate = new RestTemplate();
private static final Logger logger = LoggerFactory.getLogger(DownFileController.class);
ResponseEntity<JSONObject> response = null;
@PostMapping("/start")
public String start() throws IOException {
HttpHeaders header = new HttpHeaders();
JSONObject jsonObject = new JSONObject();
header.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), header);
response = restTemplate.postForEntity("https://app.mmteck.cn/voice/api/voice/index.ashx?action=typelist", request, JSONObject.class);
JSONObject results = response.getBody();
JSONArray datas = results.getJSONArray("rows");
List<ReceiveData> list = datas.toJavaList(ReceiveData.class);
for (int i = 0; i < list.size(); i++) {
String id = list.get(i).getId();
String foldername = list.get(i).getName();
Download(id, foldername);
logger.info("开始下载 {}", foldername);
}
return "ok";
}
/**
* 定义下载的文件夹名称
* @param typeid
* @param foldername
* @throws IOException
*/
public void Download(String typeid,String foldername) throws IOException {
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("typeid", typeid);
JSONObject jsonObject = new JSONObject();
jsonObject.put("typeid", typeid);
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), header);
response = restTemplate.postForEntity("https://app.mmteck.cn/voice/api/voice/index.ashx?action=voicedata", request, JSONObject.class);
JSONObject results = response.getBody();
logger.info("返回数据为 {}", results.toString());
JSONArray datas = results.getJSONArray("rows");
List<ReceiveData> list = datas.toJavaList(ReceiveData.class);
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
String url = "https:" + list.get(i).getSrc();
String fileName = list.get(i).getName() + ".mp3";
String dir = "OJ";
downloadHttpUrl(url, dir, fileName, foldername);
logger.info("开始下载 {}", fileName);
}
}
}
public void downloadHttpUrl(String url, String dir, String fileName, String foldername) throws IOException {
URL urlPath = new URL(url);
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();
}
FileUtils.copyURLToFile(urlPath, new File("E:/A_Voice_down/" + foldername, fileName));
}
//接受数据的封装对象
class ReceiveData {
String name ;
String src;
String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
}
}
网友评论