将fileupload formdata转换为base64时的部分字符串

时间:2018-06-11 13:26:28

标签: arrays base64 inputstream apache-commons fileitem

我试图将fileupload表单数据转换为base64。 ajax帖子将表单数据发送到后端。

$( "#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图像中,我只能看到图像的一半)。我做错了什么?

0 个答案:

没有答案
相关问题