正在开发上传图像的代码。但这不能正常工作。在我的 servlet
中,我将检查 contenttype
是否 contexttype
与 "image/jpeg"
或 "image 匹配/jpeg"
文件将写入,否则显示错误消息
Part filePart = request.getPart("pho`to");
String str = filePart.getContentType();
if (str != "image/jpeg" && str != "image/jpg") {
response.sendRedirect("uploadimage?action=errorinfiletype");
return;
}
User.updateImage(inputStream, uid);
response.sendRedirect("uploadimage?action=changed");
但是上面的代码总是显示错误信息。 我认为我的 if 条件有错误。有人请帮忙
最佳答案
您可以执行以下操作:
if (str==null || !str.matches("image/jp(e)?g") ) {
response.sendRedirect("uploadimage?action=errorinfiletype");
return;
}
关于java - MultipartConfig 中的文件类型检查错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722511/