I am trying to add some simple user image upload functionality to my webpage. At the moment I have (as far as I can tell) a working image upload - when I upload an image, it appears in the correct folder in my web app directory. However, I try to display the image on the page once the user uploads but for some reason it doesn't appear, even thought the page source looks correct...
我想在我的网页上添加一些简单的用户图片上传功能。目前我(据我所知)上传了一张图片 - 当我上传图片时,它出现在我的网络应用目录中的正确文件夹中。但是,我尝试在用户上传后在页面上显示图像但由于某种原因它没有出现,甚至认为页面源看起来正确...
In the jsp file I use the code
在jsp文件中我使用代码
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () ) {
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
}
else {
file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + filePath + fileName + "<br></br>");
out.println("<img src = \"uploads/" + fileName + "\"></img>");
When I upload an image, the page displays "Uploaded Filename:..." but not the image. If I view page source I can also see the line of code referring to the image, and if I click on the image source "uploads/photo.png", the photo opens up in a new browser page. I have no clue why I can't display it - I have also tried changing the path but it seems like this should be the correct one...
当我上传图像时,页面显示“Uploaded Filename:...”但不显示图像。如果我查看页面源我还可以看到引用图像的代码行,如果我点击图像源“uploads / photo.png”,则会在新的浏览器页面中打开照片。我不知道为什么我无法显示它 - 我也试过改变路径但看起来这应该是正确的...
My folder structure is: webapps root index.jsp upload.jsp uploads photo.png
我的文件夹结构是:webapps root index.jsp upload.jsp uploads photo.png
Is this a problem with Tomcat (I am using Tomcat 7) or with the way I am using the jsp page or the filepath? If anyone has any clues it would be greatly appreciated...
这是Tomcat(我使用的是Tomcat 7)还是我使用jsp页面或文件路径的问题?如果有人有任何线索,将不胜感激......
2 个解决方案
#1
0
Your syntax for the image tag is not right. Img tag is self-closing (<img />
) not two parts (<img></img>
).
您的图像标记语法不正确。 Img标签是自动关闭的()而不是两个部分()。
out.println("<img src='uploads/" + fileName + "' />");
I changed the \"
to just '
to make it easier to read. HTML can use single-quotes for attributes.
我将“改为”只是为了让它更容易阅读.HTML可以使用单引号作为属性。
#2
0
Fixed it by adding these two lines to the top of the html file produced by the jsp:
通过将这两行添加到jsp生成的html文件的顶部来修复它:
out.println("<!DOCTYPE html>");
out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
#1
0
Your syntax for the image tag is not right. Img tag is self-closing (<img />
) not two parts (<img></img>
).
您的图像标记语法不正确。 Img标签是自动关闭的()而不是两个部分()。
out.println("<img src='uploads/" + fileName + "' />");
I changed the \"
to just '
to make it easier to read. HTML can use single-quotes for attributes.
我将“改为”只是为了让它更容易阅读.HTML可以使用单引号作为属性。
#2
0
Fixed it by adding these two lines to the top of the html file produced by the jsp:
通过将这两行添加到jsp生成的html文件的顶部来修复它:
out.println("<!DOCTYPE html>");
out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");