我正在尝试使用我的 jsp 上传图像并将此图像保存到 mysql 数据库。我有这个代码
<form method="POST" class="registrations" enctype="multipart/form-data" >
<div>
<span>Title :</span> <input type="text" name="title" maxlength="255" size="40" placeholder="e.g.(Update on ALert Status and Activity of Mayon Volcano, etc..)" required /><br />
</div>
<div>
<span>Overview:</span>
<textarea rows="15" cols="75%" name="overview" required placeholder="Type a simple topic description..."></textarea>
</div>
<div>
<span>Select an Image:</span>
<input type="file" name="image" accept="image/gif, image/jpeg, image/jpg, image/png, image/bmp"/>
</div>
<div>
<span>Actions Taken:</span>
<textarea rows="15" cols="75%" name="action" required placeholder="Type a simple topic description..."></textarea>
</div>
<div class="btns">
<input type="submit" value="Send Report" /><input type="reset" value="Reset Fields" />
</div>
</form>
在我的 servlet 我有这个
Map<String, String> errors = SevereAssignContent.validate(req);
if (!errors.isEmpty())
{
jsp.forward(req, resp);
return;
}
SevereBulletinItems emsItem = (SevereBulletinItems) req.getAttribute("emsItem");
new SevereWeatherDAO().create(emsItem);
String msg = "Severe Weather Bullerin Report Submitted!";
resp.sendRedirect(req.getContextPath() + "/pagasa/severe-weather-bulletin?msg=" + msg + "");
我的 SevereAssignContent 类
public class SevereAssignContent
{
static ServletConfig conf;
@SuppressWarnings("unchecked")
public static Map<String, String> validate(HttpServletRequest req)
{
SevereBulletinItems emsItem = new SevereBulletinItems();
HashMap<String, String> errors = new HashMap<String, String>();
req.setAttribute("errors", errors);
req.setAttribute("emsItem", emsItem);
try
{
String filePath = conf.getServletContext().getRealPath("") + File.separator;
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File(filePath));
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(req);
for (FileItem item : items)
{
if (item.isFormField())
{
String fieldname = item.getFieldName();
if (fieldname.equals("title"))
emsItem.setTitle(item.getString());
else if (fieldname.equals("overview"))
emsItem.setSituation(item.getString());
else if (fieldname.equals("action"))
emsItem.setAction(item.getString());
}
else
{
byte[] data = item.get();
data = item.get();
if (data.length > 0)
{
Blob blob;
try
{
blob = new SerialBlob(data);
emsItem.setImage(blob);
}
catch (SerialException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
return errors;
}
}
还有我的创建声明
public void create(SevereBulletinItems item) throws IOException
{
Long id = getUniqueId();
item.setReportId(id);
java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());
PreparedStatement statement = null;
Connection connection = null;
try
{
connection = getConnection();
String sql = "insert into severeweatherbulletin values (?, ?, ?, ?, ?, ?, ?)";
statement = connection.prepareStatement(sql);
statement.setLong(1, id.longValue());
statement.setString(2, item.getTitle());
statement.setTimestamp(3, date);
statement.setString(4, item.getSituation());
statement.setBlob(5, item.getImage());
statement.setString(6, item.getAction());
statement.setLong(7, 1);
statement.executeUpdate();
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
finally
{
close(statement, connection);
}
}
现在,当我单击提交时,我的插入项目都没有保存在我的数据库中。为什么?谢谢