急遽、ReactとJava、API通信にはaxiosを用いたアップロード機能を実装することになりました。
下記の記事を参考にしてフロント側のファイルを作成いたしました。
https://qiita.com/wanwanwan/items/62cfe2ad1ad9d767f133
ここから、Java(Spring)とやりとりしたいと考えているのですが、Springの知識が浅く、現在のところ下記Javaのコマンドまでしか書くことができません。
浅い知識と質問で大変申し訳ありません、
ここからどのようにJavaのコマンドを記述していくか、助言をいただけますでしょうか。
何卒、よろしくお願いいたします。
javaScript
1import { useState } from 'react'; 2import axios from 'axios'; 3 4export const Upload = () => { 5 const [image, setImage] = useState<any>(); 6 7 const getImage = (e: React.ChangeEvent<HTMLInputElement>) => { 8 if (!e.target.files) return; 9 const img: any = e.target.files[0]; 10 setImage(img); 11 }; 12 13 const submitImage = () => { 14 const header = { 15 headers: { 16 'Content-Type': 'application/json;charset=UTF-8', 17 'Access-Control-Allow-Origin': '*', 18 }, 19 }; 20 const data = new FormData(); 21 data.append('file', image); 22 const postImageUri = '任意のURL'; 23 axios 24 .post(postImageUri, data, header) 25 .then((res) => { 26 console.log(res.data) 27 }) 28 }; 29 30 return ( 31 <div> 32 <form> 33 <label htmlFor='img'>画像</label> 34 <input id='img' type='file' accept='image/*,.png,.jpg,.jpeg,.gif' onChange={getImage} /> 35 <input type='button' value='保存' onClick={submitImage} /> 36 </form> 37 </div> 38 ); 39}; 40
Java
1package jp.co.itfllc.merecycle.v1.controllers.dashboard; 2 3import org.springframework.http.HttpStatus; 4import org.springframework.web.bind.annotation.RequestMapping; 5import org.springframework.web.bind.annotation.ResponseStatus; 6import org.springframework.web.bind.annotation.RestController; 7import jp.co.itfllc.merecycle.v1.Constants; 8 9@RestController 10@RequestMapping(Constants.API_VERSION +"file/upload") 11@ResponseStatus(HttpStatus.OK) 12public class UploadFile { 13 14} 15

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。