-->

function sendBinary(file)
{
const reader = new FileReader();
reader.onload = function(e)
{
// ArrayBufferをUint8Arrayに変換
const unit8Array = new Uint8Array(e.target.result);
const decodeBinaryString = uint8Array => uint8Array.reduce(
(binaryString, uint8) => binaryString + String.fromCharCode(uint8),'',);
// Uint8Arrayを文字列に変換
const binaryString = decodeBinaryString(unit8Array);
// Uint8Array文字列データをBase64に変換
const base64 = btoa(binaryString);
// Ajaxでservletにリクエストを送信
var request = {
base64 : base64,
};
$.ajax({
type : "POST",
url : [アクセスしたいサーブレットのURL],
data : request
});
};
// 引数のFileをArrayBufferに変換
reader.readAsArrayBuffer(file);
}

@WebServlet(urlPatterns = {[任意のアノテーション]})
public class TransportImage extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//リクエストパラメータを取得
String base64 = request.getParameter("base64");
//元々のByteの配列データへ戻す
Base64.Decoder decoder = Base64.getDecoder();
byte[] decoded = decoder.decode(base64.getBytes(StandardCharsets.UTF_8));
//ファイルの書き込み
FileOutputStream fileOutoutStream = new FileOutputStream([書き込みしたい任意のファイルパス]);
fileOutoutStream.write(decoded);
fileOutoutStream.close();
}
}