0

我正在尝试将文件上传表单数据转换为 base64。ajax post 将表单数据发送到后端。

$( "#profileModalForm" ).submit(function( event ) {
    var formData = new FormData(this);
    $.ajax({
        cache: false,
        url: 'SaveProfilePopupData.ws',
        type: "POST",
        enctype: 'multipart/form-data',
        data: formData,        
        contentType: false,
        processData: false,
        success: function (html) {
            $('#notificationArea').html(html);
        }
    });
    event.preventDefault();
});

然后在java端我尝试使用apache commons fileupload读取图像并保存到base64字符串。

            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iterator = upload.getItemIterator(request);
            while(iterator.hasNext()){
                FileItemStream item = iterator.next();
                InputStream stream = item.openStream();
                if(!item.isFormField()){
                    byte[] str = new byte[stream.available()];
                    stream.read(str);
                    imageBase64String = new String(Base64.getEncoder().encode(str));

                }
            }

我只能获得我正在上传的图像的部分 base64 字符串值(在 7KB 的图像中,我只能看到图像的一半)。我究竟做错了什么?

4

0 回答 0