使用AngularJs和Java在SQL中存储图像?
我正在建立一个购买和销售二手音乐设备的网页。我正在尝试实现在发布新邮件时为邮件添加缩略图的功能。我的一切工作都在向我的SQL数据库发送项目名称、价格、描述、位置和联系信息,并通过在AngularJs中将其转换为JSON然后将其发送到Java/JDBC来存储信息。我还可以检索数据并将其显示在“当前发布”页面上。但是,我不确定如何将带有JSON的图像发送到后端以存储在数据库中。我尝试向Java构造函数添加一个BLOB,但它似乎总是返回NULL。这是我的一些代码 我的HTML使用AngularJs和Java在SQL中存储图像?,java,angularjs,image,file-upload,Java,Angularjs,Image,File Upload,我正在建立一个购买和销售二手音乐设备的网页。我正在尝试实现在发布新邮件时为邮件添加缩略图的功能。我的一切工作都在向我的SQL数据库发送项目名称、价格、描述、位置和联系信息,并通过在AngularJs中将其转换为JSON然后将其发送到Java/JDBC来存储信息。我还可以检索数据并将其显示在“当前发布”页面上。但是,我不确定如何将带有JSON的图像发送到后端以存储在数据库中。我尝试向Java构造函数添加一个BLOB,但它似乎总是返回NULL。这是我的一些代码 我的HTML <form
<form name="insertItem">
<div class="container">
<div class="card">
<div class="card-header">
<h4>Create New Posting</h4>
<button type="button" ng-click="createItem()"
class="btn btn-success">Create Posting</button>
<p>
<span class="text-warning" ng-bind="createStatus"></span>
</p>
</div>
<div class="card-body">
<table class="table table-responive small-table">
<tr>
<td><label for="itemName">Item Name:</label><br> <input
id="name" type="text" class="form-control"
ng-model="newItem.itemName"></td>
</tr>
<tr>
<td><label for="itemCat">Category:</label><br> <select
id="cat" class="form-control"
ng-options="value for value in itemCat"
ng-model="newItem.itemCat">
</select></td>
</tr>
<tr>
<td><label for="price">Price $:</label><br> <input
type="number" class="form-control" name="price"
ng-model="newItem.itemPrice"></td>
</tr>
<tr>
<td><label for="itemDesc">Description:</label><br> <textarea
rows="5" id="desc" class="form-control"
ng-model="newItem.itemDesc"></textarea></td>
</tr>
<tr>
<td><label for="location">Location:</label><br> <input
type="text" class="form-control" id="loc"
ng-model="newItem.location"></td>
</tr>
<tr>
<td><label for="contact">Contact Info:</label><br> <input
type="text" class="form-control" id="cont"
ng-model="newItem.contact"></td>
</tr>
<tr>
<td><label for="thumbnail">Upload a Thumbnail Image:
</label><br> <input type="file" class="form-control-file border"
name="thumbnail" ng-model="newItem.thumbnail"></td>
</tr>
</table>
<ul>
<li ng-repeat="file in files">{{file.name}}</li>
</ul>
</div>
</div>
</div>
</form>
我的球衣邮政
@POST
@Produces(MediaType.APPLICATION_JSON)
public List<item> insert(item newItem) {
return service.insert(newItem);
}
@POST
@产生(MediaType.APPLICATION_JSON)
公共列表插入(项目newItem){
退货服务。插入(新项目);
}
我的JDBC代码
private final static String addItems = "INSERT INTO item "
+ "(itemName, itemCat, itemPrice, itemDesc, contact, location, thumbnail) VALUES "
+ "(?, ?, ?, ?, ?, ?, ?);";
public List<item> insert(item newItem) {
List<item> items = new ArrayList<item>();
PreparedStatement prepStatement = null;
Connection conn = MariaDbUtil.getConnection();
try {
prepStatement = conn.prepareStatement(addItems);
prepStatement.setString(1, newItem.getItemName());
prepStatement.setString(2, newItem.getItemCat());
prepStatement.setInt(3, newItem.getItemPrice());
prepStatement.setString(4, newItem.getItemDesc());
prepStatement.setString(5, newItem.getContact());
prepStatement.setString(6, newItem.getLocation());
prepStatement.setBlob(7, newItem.getThumbnail()); //Blob
prepStatement.executeUpdate();
System.out.println("Row Added " + newItem.getItemName());
newItem.setItemId(getLastKey(conn));
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
prepStatement.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
items.add(newItem);
return items;
}
私有最终静态字符串addItems=“插入到项中”
+(itemName、itemCat、itemPrice、itemDesc、联系人、位置、缩略图)值
+ "(?, ?, ?, ?, ?, ?, ?);";
公共列表插入(项目newItem){
列表项=新建ArrayList();
PreparedStatement prepStatement=null;
连接conn=MariaDbUtil.getConnection();
试一试{
prepStatement=conn.prepStatement(附加项);
setString(1,newItem.getItemName());
setString(2,newItem.getItemCat());
setInt(3,newItem.getItemPrice());
setString(4,newItem.getItemDesc());
setString(5,newItem.getContact());
setString(6,newItem.getLocation());
prepStatement.setBlob(7,newItem.getThumbnail());//Blob
prepStatement.executeUpdate();
System.out.println(“添加行”+newItem.getItemName());
setItemId(getLastKey(conn));
}捕获(SQLE异常){
e、 printStackTrace();
}最后{
试一试{
如果(conn!=null){
康涅狄格州关闭();
prepStatement.close();
康涅狄格州关闭();
}
}捕获(SQLE异常){
e、 printStackTrace();
}
}
项目。添加(新项目);
退货项目;
}
我不会将缩略图存储在数据库中。我会存储缩略图所在的位置(实际图像的路径)。我会将图像上传到S3,或者让您自己在服务器上管理一个位置,可能使用Samba或其他方式。通过这种方式,您可以轻松地存储来自数据库的完整图像、缩略图和引用。然后每次加载数据库条目时,不必加载图像和缩略图。只要它们的位置就足以让HTML显示它们。然后,在其他一些场景中,您可能会发现您希望限制图像访问,以便在检索之前在这些图像路径上添加一些安全检查等等。我不会将缩略图存储在数据库中。我会存储缩略图所在的位置(实际图像的路径)。我会将图像上传到S3,或者让您自己在服务器上管理一个位置,可能使用Samba或其他方式。通过这种方式,您可以轻松地存储来自数据库的完整图像、缩略图和引用。然后每次加载数据库条目时,不必加载图像和缩略图。只要它们的位置就足以让HTML显示它们。然后,在其他一些场景中,您可能会发现您希望限制图像访问,以便在检索之前在这些图像路径上添加一些安全检查等等。图像的路径是字符串吗?确实是。如果要使用AWS S3进行存储,您可能会遇到这样的问题:图像的路径是字符串吗?确实是。如果将AWS S3用于存储,您可能会遇到这样的情况:coreng model
指令无法使用开箱即用的
。请参阅。核心ng型号
指令不适用于开箱即用的
。看见
private final static String addItems = "INSERT INTO item "
+ "(itemName, itemCat, itemPrice, itemDesc, contact, location, thumbnail) VALUES "
+ "(?, ?, ?, ?, ?, ?, ?);";
public List<item> insert(item newItem) {
List<item> items = new ArrayList<item>();
PreparedStatement prepStatement = null;
Connection conn = MariaDbUtil.getConnection();
try {
prepStatement = conn.prepareStatement(addItems);
prepStatement.setString(1, newItem.getItemName());
prepStatement.setString(2, newItem.getItemCat());
prepStatement.setInt(3, newItem.getItemPrice());
prepStatement.setString(4, newItem.getItemDesc());
prepStatement.setString(5, newItem.getContact());
prepStatement.setString(6, newItem.getLocation());
prepStatement.setBlob(7, newItem.getThumbnail()); //Blob
prepStatement.executeUpdate();
System.out.println("Row Added " + newItem.getItemName());
newItem.setItemId(getLastKey(conn));
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
prepStatement.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
items.add(newItem);
return items;
}