我正在使用servlet,其中servlet一次上载文件夹中的多个文件,但是它没有获得数据库列中的所有文件名,有人可以帮助我编写代码如何一次获得列中的所有文件名吗?由servlet上载
我在数据库列中仅获得一个文件名,如20190416070203.192dellxps15.jpg,但我希望用户上传的列中有多个文件名,例如 20190416070203.192dellxps15.jpg 20190416070203.199华为MAtebookXpro.jpg 20190416070203.207s10plus.jpg
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String page = request.getParameter("page");
if (page == null) {
request.getRequestDispatcher("admin/login.jsp").forward(request, response);
;
} else {
doPost(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String page = request.getParameter("page");
if (page.equals("add_product")) {
String appPath = "C:/Users/zaid/eclipse-workspace/Ecommerce-shopping/WebContent/";
String savePath = appPath + File.separator + SAVE_DIR;
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
Part part1 = request.getPart("file");
String fileName1 = extractFileName(part1);
final DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
final Calendar c = Calendar.getInstance();
c.add(Calendar.MILLISECOND, 1);
String daformat=df.format(c.getTime());
for (Part part : request.getParts()) {
String fileName =extractFileName(part);
// refines the fileName in case it is an absolute path
if (fileName != null && !"".equals(fileName)) {
fileName = new File(fileName).getName();
part.write(savePath + File.separator +""+daformat+""+fileName);
}
}
request.setAttribute("message", "Upload has been done successfully!");
String name = request.getParameter("name");
String price = request.getParameter("price");
String category = request.getParameter("category");
String featured = request.getParameter("featured");
String image = request.getParameter("image");
Product p = new Product();
p.setName(name);
p.setPrice(price);
p.setCategory(category);
p.setFeatured(featured);
p.setImage("img/"+image);
DB account = new DB();
try {
account.addProduct(p);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "Product added Successfully", "Info", JOptionPane.INFORMATION_MESSAGE);
request.getRequestDispatcher("admin/index.jsp").forward(request, response);
}
}
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length() - 1);
}
}
return "";
}
}
//////////////// jsp ////////////////////////
<div class="signup-header">
<h2>Add Product</h2>
</div>
<form method="post" action="admin" enctype="multipart/form-data" >
<input type="hidden" name="page" value="add_product" >
<font color="#F24638"><c:out value="${message }"></c:out></font>
<div class="signup-group">
<label>Name</label>
<input type="text" name="name" placeholder="product name goes here" required>
</div>
<div class="signup-group">
<label>Price</label>
<input type="text" name="price" placeholder="product price" required>
</div>
<div class="signup-group">
<label>Category</label>
<input type="text" name="category" placeholder="product category" required>
</div>
<div class="signup-group">
<label>Featured</label>
<input type="text" name="featured" placeholder="yes/no" required>
</div>
<div class="signup-group">
<label for="fileupload"> Select an image to upload</label>
<input type="file" name="file" required multiple><br>
</div>
<div class="signup-group">
<input type="submit" value="Add Product">
</div>
</form>
<footer style="position: relative;top:60px; left: 0;bottom: 0;width: 100%; height:30%">
<div class="footer"> © 2018 Copyright:
Zoats.com
</div>
</footer>
如何在具有相同用户ID的数据库列中一次获取列中的多个图像名称,例如在servlet中使用单个请求。 用户111:20190416070203.192dellxps15.jpg user111:20190416070203.199HuaweiMAtebookXpro.jpg user111:20190416070203.207s10plus.jpg
答案 0 :(得分:0)
您应该将account.addProduct(p);
放入for
循环中,以循环访问所有附件。
答案 1 :(得分:0)
您可以创建一个String list
,然后在该列表中添加文件名。并将该字符串列表转换为comma separated String
,同时将其存储在数据库中
List<String> fileNames = new ArrayList<>();
for (Part part : request.getParts()) {
String fileName =extractFileName(part);
// refines the fileName in case it is an absolute path
if (fileName != null && !"".equals(fileName)) {
fileName = new File(fileName).getName();
part.write(savePath + File.separator +""+daformat+""+fileName);
fileNAmes.add(fileName);
}
}
request.setAttribute("message", "Upload has been done successfully!");
String name = request.getParameter("name");
String price = request.getParameter("price");
String category = request.getParameter("category");
String featured = request.getParameter("featured");
String image = request.getParameter("image");
Product p = new Product();
p.setName(name);
p.setPrice(price);
p.setCategory(category);
p.setFeatured(featured);
p.setImage(StringUtils.join(fileNames , ','));
从Apache Commons导入StringUtils
import org.apache.commons.lang3.StringUtils
答案 2 :(得分:-1)
Dhrumil Patel这是我的jsp文件,我在其中使用jstl检索图像`
<tr>
<td style="width: 50px;"><c:out value="${row.id }"></c:out></td>
<td style="width: 100px;"><c:out value="${row.name }"></c:out></td>
<td style="width: 100px;"><c:out value="${row.price }"></c:out></td>
<td style="width: 100px;"><c:out value="${row.category}"/></td>
<td style="width: 100px;"><img src="${row.image}" height="100" width="150" ></td>
<td style="width: 100px;"><a href="<%= request.getContextPath() %>/admin?page=edit&id=${row.id}" style="color: #6bb1f8;">edit</a> ||
<a href="<%= request.getContextPath() %>/admin?page=delete&id=${row.id}" style="color:#6bb1f8;">delete</a></td>
</tr>
</table>
</c:forEach>`
这是数据库类代码
public Product fetchProduct(String id) throws SQLException {
dbConnect();
String sql = "select * from product where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
ResultSet rst = pstmt.executeQuery();
Product p = new Product();
while(rst.next()){
p.setId(rst.getInt("id"));
p.setName(rst.getString("name"));
p.setPrice(rst.getString("price"));
p.setCategory(rst.getString("category"));
p.setFeatured(rst.getString("featured"));
p.setImage(rst.getString("image"));
}
dbClose();
return p;
}