亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何將圖像從服務器發送到我的客戶端?

我如何將圖像從服務器發送到我的客戶端?

冉冉說 2023-05-24 17:43:31
我一直在學習 spring 并將所有東西放在一起,我正在制作一個電子商務應用程序。我已經使用 rest api 連接客戶端和服務器。現在我需要將圖像發送給客戶端。我的圖像已經存儲在 src/resources 文件夾中。我需要知道的是如何通過 rest api 發送這些圖像。這樣我就可以在我的客戶中使用它我對此很菜鳥。我試過谷歌,我能找到的只是上傳到服務器的圖像文件的例子。我找不到通過 rest api 從服務器向客戶端發送文件的示例。過去三天我一直被困在這這是我的休息控制器:現在我需要知道下一步該做什么,以便我可以發送圖像@RestController@RequestMapping("/api")public class CategoriesRestController {// autowire customer service@Autowiredprivate CategoriesService service;//add mapping for GET all customer@GetMapping("/categories")public List<Categories> getCategories() {    return service.getCategories();}// adding mapping for GET only one customer@GetMapping("/categories/{categoryId}")public Categories getCategory(@PathVariable int categoryId) {    Categories categories = service.getCategory(categoryId);    if(categories == null) {        throw new CustomerNotFoundException("Customer id not found- "+ categoryId);    }else {    return categories;    }}// adding mapping for POST/customer i.e. insert a customer@PostMapping("/categories")public Categories addCategories(@RequestBody Categories theCategories) { //@RequestBody will convert JSON to JAVA object    // just to make things clear... always set id to 0 when inserting new object    // so that it will be created instead of update    theCategories.setId(0);    service.saveCategories(theCategories);    return theCategories;}
查看完整描述

2 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

您可能以錯誤的方式思考問題。HTML 只需要圖像的路徑,而不是通過其余 API 發送圖像本身。您將圖像存儲在一個目錄中,您可以將圖像的路徑傳遞給您的 HTML。您可以向類別添加變量“imagePath”,HTML 可以在標簽中引用它



查看完整回答
反對 回復 2023-05-24
?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

您可以將圖像轉換為 base64:


byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));

String encodedString = Base64.getEncoder().encodeToString(fileContent);

然后通過您的 API 發送此屬性。然后在你的客戶端你可以像這樣使用它:


<img src=json.encodedString />

這json是一個通過 API 發送的對象。


在發送之前,encodedString您可以在它的開頭附加一些類似下面的內容,以便更容易在前端顯示:


"data:image/png;base64,"

要在前端顯示 base64 圖像,您應該使用如下內容:


<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA

AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO

    9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />


查看完整回答
反對 回復 2023-05-24
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號