尝试使用 servlet + jsp + mysql 将多个文件(带有任何扩展名)上传到 mysql 数据库中
Posted
技术标签:
【中文标题】尝试使用 servlet + jsp + mysql 将多个文件(带有任何扩展名)上传到 mysql 数据库中【英文标题】:trying to upload multiple files(with any extensions) into mysql databse using servlet + jsp + mysql 【发布时间】:2020-03-10 00:46:28 【问题描述】:我是 Servlet 和 JSP 的新手,我正在尝试将多个文件上传到 mysql 数据库,但只有一个文件存储到数据库中。我在 index.jsp 页面中添加了 (multiple="multiple" while selection files)。但只存储我一开始选择的一个文件。我不知道我必须对我的 java 类 (UploadServletClass.java) 做哪些更改。请帮我解决一下这个。我正在使用 netbeans 8.2、apache tomcat 8、xampp MySQL。我的代码仅将一个文件成功上传到 MySQL 数据库。但我想同时上传多个文件。在此先感谢:)
这是我的 index.jsp 页面
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Document Management System</title>
<style>
h1
text-shadow: 2px 2px 5px red;
body
background-image: url("background.png");
</style>
</head>
<body>
<br><br><br><br>
<center>
<form action="UploadServletClass" method="post" enctype="multipart/form-data">
<center>
<h1>Welcome to Document Management System...</h1>
</center>
<table align="center" border="2" bordercolor="#33CCFF">
<tr>
<td align="center" colspan="2">Form Details</td>
</tr>
<tr>
<td>First Name</td>
<td>
<input type="text" required="" name="firstname">
</td>
</tr>
<tr>
<td>Last Name</td>
<td>
<input type="text" required="" name="lastname">
</td>
</tr>
<tr>
<td>Division</td>
<td>
<input type="text" required="" name="division">
</td>
</tr>
<tr>
<td>Reporting Unit</td>
<td>
<input type="text" required="" name="reportingunit">
</td>
</tr>
<tr>
<td>Document Number</td>
<td>
<input type="number" required="" name="documentnumber">
</td>
</tr>
<tr>
<td>Document Name</td>
<td>
<input type="text" required="" name="documentname">
</td>
</tr>
<tr>
<td>Document Uploader</td>
<td>
<input type="text" required="" name="documentuploader">
</td>
</tr>
<tr>
<td>Document Owner</td>
<td>
<input type="text" required="" name="documentowner">
</td>
</tr>
<tr>
<td>Document Type</td>
<td>
<select name="Document_Type">
<option value="Agreement">Agreement</option>
<option value="Contract">Contract</option>
<option value="PO">PO</option>
<option value="Invoice">Invoice</option>
<option value="COA">COA</option>
<option value="Lease Deed">Lease Deed</option>
<option value="AMC">AMC</option>
</select>
</td>
</tr>
<br><br>
<tr>
<td>Document Category</td>
<td>
<select name="Document_Category">
<option value="Customer">Customer</option>
<option value="Vendor">Vendor</option>
<option value="Internal">Internal</option>
</select>
</td>
</tr>
<br><br>
<tr>
<td>Select File</td>
<td>
<input type="file" required="" name="file" id="file" multiple="multiple">
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
<br><a href="filelist.jsp">View List</a>
</center>
</body>
</html>
这是我的 UploadServletClass.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
response.setContentType("text/plain;charset=UTF-8");
try
out = response.getWriter();
session = request.getSession(false);
String folderName = "resources";
//String uploadPath = request.getServletContext().getRealPath("") + File.separator + folderName;
String uploadPath = request.getServletContext().getRealPath("") + File.separator + folderName;
File dir = new File(uploadPath);
if(!dir.exists())
dir.mkdirs();
Part filepart = request.getPart("file");
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
String fileName = filepart.getSubmittedFileName();
String path = folderName + File.separator + fileName;
Timestamp added_date = new Timestamp(System.currentTimeMillis());
String div = request.getParameter("division"); //textbox value of division field
String repunit = request.getParameter("reportingunit"); //textbox value of reportingunit field
int docnum = Integer.parseInt(request.getParameter("documentnumber")); ///textbox value of documentnumber field
String docName = request.getParameter("documentname"); //textbox value of documentname field
String docUploader = request.getParameter("documentuploader"); //textbox value of documentuploader field
String docOwner = request.getParameter("documentowner"); //textbox value of documentowner field
String docType = request.getParameter("Document_Type"); //textbox value of Document_Type field
String docCategory = request.getParameter("Document_Category"); //textbox value of Document_Category field
System.out.println("filename:" +fileName);
System.out.println("path:" +uploadPath);
System.out.println("Name:" +firstName);
InputStream ins = filepart.getInputStream();
Files.copy(ins, Paths.get(uploadPath + File.separator + fileName),StandardCopyOption.REPLACE_EXISTING);
try
con = DB.getConnection();
System.out.println("connection done");
String sql = "insert into newfiles(firstname,lastname,filename,path,division,reportingunit,documentnumber,documentname,documentuploader,documentowner,documenttype,documentcategory,added_date) values(?,?,?,?,?,?,?,?,?,?,?,?,?)"; //inserting all values into database
ps = con.prepareStatement(sql);
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, fileName);
ps.setString(4, path);
ps.setString(5, div); //index specifies the respective parameter in the query
ps.setString(6, repunit);
ps.setInt(7, docnum);
ps.setString(8,docName);
ps.setString(9, docUploader);
ps.setString(10, docOwner);
ps.setString(11,docType);
ps.setString(12, docCategory);
ps.setTimestamp(13, added_date);
int status = ps.executeUpdate();
if(status > 0)
session.setAttribute("filename", fileName);
String msg = "" + fileName + " File Uploaded Successfully...";
request.setAttribute("msg", msg);
RequestDispatcher rd = request.getRequestDispatcher("/process.jsp");
rd.forward(request,response);
System.out.println("File Uploaded Successfully");
System.out.println("Uploaded Path:" +uploadPath);
catch(SQLException e)
out.println("Exception:" +e);
System.out.println("Exception1:" +e);
finally
try
if(ps != null)
ps.close();
if(con != null)
con.close();
catch (SQLException e)
out.println(e);
catch(IOException | ServletException e)
out.println("Exception:" +e);
System.out.println("Exception2:" +e);
【问题讨论】:
看看learningprogramming.net/java/jsp-servlet/… - 特别是创建助手部分。 看起来你甚至没有解析文件部分.....你的表单多部分启用了吗?您是否也为 servlet 指定了多部分?您需要发布所有相关代码。 @JonathanLaliberte 编辑了 index.jsp 文件。 有人请帮我解决这个问题 【参考方案1】:将此符号添加到您的 Servlet @MultipartConfig
@WebServlet(name = "test", urlPatterns = "/test" )
@MultipartConfig
public class Test extends HttpServlet
//code
要获取所有文件,请在您的 doPost 中使用它:
Collection<Part> fileparts = request.getParts();
在此处查看完整示例以及您可以使用/更改的更多选项:https://docs.oracle.com/javaee/6/tutorial/doc/glrbb.html
【讨论】:
嘿伙计,我已将这些行添加到我的 UploadServletClass.java 文件中。字符串文件名 = 文件部分.getSubmittedFileName(); List以上是关于尝试使用 servlet + jsp + mysql 将多个文件(带有任何扩展名)上传到 mysql 数据库中的主要内容,如果未能解决你的问题,请参考以下文章
使用Servlet上的switch语句运行取决于JSP链接的方法[重复]