gpt4 book ai didi

html - 如何使用 jdbc、servlets 和 html 将文件远程上传到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-28 22:24:58 24 4
gpt4 key购买 nike

我一直在尝试将文件从 html 页面远程上传到 servlet,然后将其保存在数据库中,到目前为止我已经设法在本地完成,但是当在同一网络中使用另一台计算机时,我得到和错误解释系统找不到指定的文件。

我正在使用以下表格来接收文件:

       <form method="post" action="UploadFile" enctype="multipart/form-data">                    
<tr>
<td><Input name="name" type="text"></td>
<td><Input name="client_ID" type="text" required></td>
<td><input name="date" type="date"></td>
<td><input name="pdf" type="file"></td>
<td><input name="xml" type="file"></td>
</tr>
</form>

哪个链接到 servlet 中的以下操作:

    String url = "jdbc:mysql://localhost/database";
Connection con;
String path1=request.getParameter("pdf");
String path2=request.getParameter("xml");
String name=request.getParameter("name");
String date=request.getParameter("date");
String client=request.getParameter("client_ID");
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,"user", "password");
PreparedStatement stat = con.prepareStatement("insert into table(pdf, xml, date, client, employee_ID, client_ID) values(?,?,?,?,1,?)");
InputStream is = new FileInputStream(new File(path1));
stat.setBlob(1,is);
InputStream is2 = new FileInputStream(new File(path2));
stat.setBlob(2,is2);
stat.setString(3,date);
stat.setString(4,name);
stat.setString(5,client);

并抛出以下异常:

java.io.FileNotFoundException: file.pdf (The system could not find the specified file)

我知道问题出在 servlet 试图从主机上传文件。任何帮助将不胜感激。

最佳答案

这个问题与此重复。 read the contents of a file upload in java

  1. 您必须先将文件保存到本地硬盘。
  2. 将本地硬盘文件发送到MySql数据库。

读取流并保存到本地硬盘。

     String path1=request.getParameter("pdf");
//read this and save the content to local harddisk, location e.g. C:\\temp\\A.pdf
String path2=request.getParameter("xml");
//read this and save the content to local harddisk, location e.g. C:\\temp\\B.xml

现在,将本地路径传递给 MySql Query。

InputStream is = new FileInputStream(new File("C:\tmp\A.pdf"));
stat.setBlob(1,is);
InputStream is2 = new FileInputStream(new File("C:\tmp\B.xml"));
stat.setBlob(2,is2);

关于html - 如何使用 jdbc、servlets 和 html 将文件远程上传到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50146049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com