Arrays 将fileupload formdata转换为base64时的部分字符串
我正在尝试将文件上载表单数据转换为base64。ajax post将表单数据发送到后端Arrays 将fileupload formdata转换为base64时的部分字符串,arrays,base64,inputstream,apache-commons,fileitem,Arrays,Base64,Inputstream,Apache Commons,Fileitem,我正在尝试将文件上载表单数据转换为base64。ajax post将表单数据发送到后端 $( "#profileModalForm" ).submit(function( event ) { var formData = new FormData(this); $.ajax({ cache: false, url: 'SaveProfilePopupData.ws', type: "POST", enctype: '
$( "#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端,我尝试使用ApacheCommonsFileUpload读取图像并保存到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图像中,我只能看到图像的一半)。我做错了什么?1。您正在使用base64—您很可能不需要使用它。2. <代码>字节[]str=新字节[stream.available()]代码>您只处理立即可用的内容;如果还有更多呢?相关: