我想在名为图像的项目目录文件夹中上传图像文件,并将图像名称存储在数据库中。
文件名已保存在数据库中,浏览器正确检索了我的图像,但当我检查名为图像的目录文件夹时,它是空的。
我的问题是:我的文件存储在哪里以及为什么我在文件夹中看不到图像文件?
这是我的Index.jsp文件
<%--
Document : index
Created on : Mar 15, 2018, 7:30:15 PM
Author : Lenovo
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="form1" method="post" enctype="multipart/form-data" action="insertimage.jsp">
<p>
<input type="file" name="ImageFile" id="ImageFile" />
</p>
<p>
<input type="submit" name="submit" value="submit" />
</p>
</form>
</body>
</html>
这是我的insertimage.jsp文件
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="java.util.*, java.io.*" %>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page import="java.io.File"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="getcon.jsp"%> <!-- to connect a database-->
<%
try
{
String ImageFile="";
String itemName = "";
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart)
{
}
else
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try
{
items = upload.parseRequest(request);
}
catch (FileUploadException e)
{
e.getMessage();
}
Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
if(name.equals("ImageFile"))
{
ImageFile=value;
}
}
else
{
try
{
itemName = item.getName();
File savedFile = new File(config.getServletContext().getRealPath("/") + "Images\\" + itemName);
out.println("File Uploaded.." + itemName);
item.write(savedFile);
}
catch (Exception e)
{
out.println("Error" + e.getMessage());
}
}
}
try
{
st.executeUpdate("insert into test(image) values ('" + itemName + "')");
}
catch(Exception el)
{
out.println("Inserting error" + el.getMessage());
}
}
}
catch (Exception e)
{
out.println(e.getMessage());
}
%>
这是我的retrieveimage.jsp文件
<%@ include file="getcon.jsp"%>
<html>
<head>
<title>View Image Page</title>
</head>
<body>
<table width="100%" border="0">
<!-- main content -->
<%
ResultSet rs=null;
try
{
rs = st.executeQuery("select image from test");
while(rs.next())
{
%>
<table width="70%" height="160" border="1" align="center">
<tr>
<!-- Mention Directory where your images has been saved-->
<td><img src="Images/<%=rs.getString("image") %>" width="115" height="128" /></td>
</tr>
</table>
<%
}
}
catch(Exception e)
{
out.print("" + e.getMessage());
}
%>
</table>
</body>
</html>
这是我的连接文件
<%@page import="java.sql.*" %>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample", "root", "root");
Statement st = con.createStatement();
%>
答案 0 :(得分:0)
以下有更多工作机会。
File savedFile = new File(config.getServletContext().getRealPath("/Images/" + itemName));
savedFile.getParentFile().mkdirs();
...
不确定服务器是否也是Windows;在其他系统上,路径名称区分大小写(具有大写i的图像)。
您可以提供固定的itemName进行测试。
img标签可能需要:
<img src="/Images/ ...