gpt4 book ai didi

mysql - 通过 AWS 上托管的 servlet 从 mySQL DB 下载文件

转载 作者:行者123 更新时间:2023-11-29 15:53:05 26 4
gpt4 key购买 nike

我当前正在创建一个网页,该网页显示包含来自 Mysql 数据库的数据的表。其中一列是一个文件(作为 BLOB 存储在数据库中)。该文件的名称是链接到我的 download.java servlet 的 anchor 标记。我的下载 servlet 在本地部署时可以工作,但是现在我已经部署到 AWS ElasticBeanstalk 实例,该 servlet 无法工作。

在日志中显示以下内容:

org.apache.coyote.http11.AbstractHttp11Processor.process Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

/usr/share/tomcat8/Downloads/sdc.png (No such file or directory)

servlet 代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "dbURL?serverTimezone=" + TimeZone.getDefault().getID();
Connection conn = DriverManager.getConnection(url , "username" , "password");
String fn = request.getParameter("Id");
String selectSQL = "SELECT file FROM Requests WHERE fileID=?";
PreparedStatement pstmt = conn.prepareStatement(selectSQL);
pstmt.setString(1, fn);
ResultSet rs = pstmt.executeQuery();
// write binary stream into file
String home = System.getProperty("user.home");
File file = new File(home+"/Downloads/" + fn);
FileOutputStream output = new FileOutputStream(file);
System.out.println("Writing to file " + file.getAbsolutePath());
while (rs.next()) {
InputStream input = rs.getBinaryStream("file");
byte[] buffer = new byte[1024];
while (input.read(buffer) > 0) {
output.write(buffer);
}
}
RequestDispatcher rd = request.getRequestDispatcher("/requests.jsp");
rd.forward(request, response);
rs.close();
pstmt.close();
} catch (SQLException | IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Servlet 应该将文件从 Mysql DB 下载到用户下载文件夹。但是,这仅在本地有效,在 AWS 服务器上则失败。我认为这是因为:

String home = System.getProperty("user.home");

返回AWS服务器实例的主路径,而不是用户/访问者的主路径。

请帮助我调整我的 servlet,以便它可以在 AWS 服务器上运行

更新:经过一些研究,我认为获取客户端下载文件夹的路径是不可能的。现在我想我需要创建一个“另存为”对话框。任何有关如何执行此操作的提示以及可以帮助我执行此操作的资源都将受到赞赏

最佳答案

我能够使用问题 here 中发布的代码让我的 servlet 工作

我的工作代码现在如下所示:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
try {
// Get Database Connection.
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "dbURL?serverTimezone=" + TimeZone.getDefault().getID();
conn = DriverManager.getConnection(url , "username" , "password");
String fileName = request.getParameter("Id");
System.out.println("File Name: " + fileName);
// queries the database
String sql = "SELECT file FROM requests WHERE fileID= ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, file);
ResultSet result = statement.executeQuery();
if (result.next()) {
// gets file name and file blob data
Blob blob = result.getBlob("file");
InputStream inputStream = blob.getBinaryStream();
int fileLength = inputStream.available();

System.out.println("fileLength = " + fileLength);

ServletContext context = getServletContext();

// sets MIME type for the file download
String mimeType = context.getMimeType(fileID);
if (mimeType == null) {
mimeType = "application/octet-stream";
}

// set content properties and header attributes for the response
response.setContentType(mimeType);
response.setContentLength(fileLength);
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", fileID);
response.setHeader(headerKey, headerValue);

// writes the file to the client
OutputStream outStream = response.getOutputStream();

byte[] buffer = new byte[1024];
int bytesRead = -1;

while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
}
else {
// no file found
response.getWriter().print("File not found for the fn: " + fileName);
}
} catch (Exception e) {
throw new ServletException(e);
}
}

关于mysql - 通过 AWS 上托管的 servlet 从 mySQL DB 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56662541/

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