今週のプロジェクトでファイルダウンロード機能を実装するには、サーバー側とコンピューター側の両方を開発する必要があります。
サーバー側:SpringBoot実装、コンピューター側RestSharp実装、いくつかの調査の後、コードは次のとおりです。
SpringBoot:
@GetMapping("/download-client")
public ResponseEntity<Resource> downloadClient() throws IOException {
String filePath = "C:\\test.txt";
File file = new File(filePath);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
return ResponseEntity.ok()
.contentLength(file.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
RestSharp:
var client = new RestClient(url);
var request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
IRestResponse response = client.Execute(request);
string destPath = "C:\test.txt"
byte[] bytes = client.DownloadData(request);
File.WriteAllBytes(destPath, bytes);
IT業界の生産性向上のスピードを嘆かなければなりません。2012年に初めて業界に参入したとき、ファイルのダウンロードは面倒な作業であり、利用できる既製のソリューションはそれほど多くありませんでした。プログラミング言語とツールの繁栄により、今では簡単にできます。