Java Web上传的图像不';t显示
我正在使用tomcat 8并通过以下代码将图像上载到服务器:Java Web上传的图像不';t显示,java,image,jsp,tomcat,servlets,Java,Image,Jsp,Tomcat,Servlets,我正在使用tomcat 8并通过以下代码将图像上载到服务器: public static String uploadImage(String description, HttpServletRequest request) throws ServletException { Part filePart; try { filePart = request.getPart(PARAM_NAME_IMAGE); try(O
public static String uploadImage(String description, HttpServletRequest request) throws ServletException {
Part filePart;
try {
filePart = request.getPart(PARAM_NAME_IMAGE);
try(OutputStream out = new FileOutputStream(new File(PATH + description + ".jpg"));
InputStream fileContent = filePart.getInputStream()){
int read;
byte[] bytes = new byte[1024];
while ((read = fileContent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
}
} catch (IOException e) {
LOG.error("IOException", e);
}
return PATH_TO_IMAGE + description + ".jpg";
}
然后,我将项目的图像路径保存到DB。然后我想通过以下方式在页面上查看:
<img src="${item.imagePath}" width="350" height="320" />
因此,我需要查看在不重新启动服务器的情况下上传的内容您的HTTP WEB服务器和Java服务器容器(Servlet或企业容器)是否配置为您的请求是多部分请求?多部分/表单数据规范(RFC-2388)。我将在请求的getParts之后遍历表单数据。thx,我将allowCasualMultipartParsing=“true”添加到Tomcat context.xml中,但是没有帮助这并不能解释为什么重新启动服务器后它会工作。@BalusC我读到我需要将映像存储在除IDE的项目文件夹(也称为服务器的部署文件夹)之外的任何可访问位置,但是如何将映像的路径放入其中?只需从上到下阅读重复的答案,包括“另请参阅”链接。@BalusC非常感谢,这很有帮助