【发布时间】:2013-10-21 15:34:40
【问题描述】:
我有一个用户表单,其中允许用户上传图像。我将这些图像存储在 tomcat 服务器中,并将图像名称存储在数据库中以检索特定用户的图像。
我的图片存储代码如下..
String filePath = getServlet().getServletContext().getRealPath("/") +"Images";
File folder = new File(filePath);
if(!folder.exists()){
folder.mkdir();
}
String fileName = userForm.getUploadedFile().getFileName();
System.out.println("Server path:" +filePath);
File newFile = new File(filePath, fileName);
FileOutputStream fos = new FileOutputStream(newFile);
fos.write(userForm.getUploadedFile().getFileData());
fos.flush();
fos.close();
现在我想在登录后在用户的个人资料页面中显示存储的图像..
我在个人资料页面中做了类似的事情..
<img src="http://localhost:8082/Images/${sessionUser.image}"/>
但是我的图片没有显示出来..
请有人指导..
【问题讨论】:
-
您确定该文件正在上传到指定文件夹吗?您可以通过“我的电脑”访问它吗?还有
System.out.println("Server path:" +filepath);的输出是什么? -
是的,它正在上传,“服务器路径”给了我 D:\Arthi iyer\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\NewStrutsOBL\Images\Koala .jpg
-
查看您的个人资料页面源并复制
img标签的src属性的确切值并将其直接粘贴到您的浏览器地址栏中。结果是什么?它给了你哪个 HTTP 错误? -
在 chrome 之类的浏览器中试一试,并检查其开发者工具的“网络”选项卡。
status列中有什么内容?响应头是什么? -
您说您的页面中有
localhost:8082/Images/Koala.jpg,但在回复@Ashish 时您说localhost:8082/Images/${sessionUser.image}哪个是正确的?
标签: java