调用第三方接口实现单个文件下载,第三方接口地址https://ccm-uat.cmitry.com/march/march-contract/accessory/contractAccessory; 请求方式:GET; 请求头:Content-Type:application/json,Authorization, 请求参数:String id,String name,不要把请求参数拼接到url里, 响应报文:把IO流返回给前端。浏览器会自动提示保存并下载生成的文件,用Java实现Controller层,帮我写并标注释,打印出入参日志
时间: 2023-03-29 22:04:45 浏览: 131
好的,我可以回答这个问题。以下是Java实现Controller层的代码,并附有注释和入参日志打印:
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
@RestController
public class DownloadController {
private static final Logger LOGGER = Logger.getLogger(DownloadController.class.getName());
@GetMapping("/download")
public ResponseEntity<byte[]> downloadFile(@RequestParam String id, @RequestParam String name) throws IOException {
LOGGER.info("入参:id=" + id + ", name=" + name);
// 构造请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "your_authorization_token_here");
// 构造请求URL
String url = "https://ccm-uat.cmitry.com/march/march-contract/accessory/contractAccessory";
// 构造请求参数
String requestBody = "{\"id\":\"" + id + "\",\"name\":\"" + name + "\"}";
// 发送请求并获取响应
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<byte[]> response = restTemplate.postForEntity(url, requestBody, byte[].class, headers);
// 构造响应头
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.setContentDispositionFormData("attachment", name);
// 返回响应实体
return new ResponseEntity<>(response.getBody(), responseHeaders, HttpStatus.OK);
}
}
```
请注意,这里的Authorization仅仅是一个示例,你需要将其替换为你自己的授权令牌。另外,这里的代码仅仅是一个示例,你需要根据自己的实际情况进行修改和优化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)