gpt4 book ai didi

java - 使用文件选择器选择数据输入文件

转载 作者:行者123 更新时间:2023-12-01 10:17:00 25 4
gpt4 key购买 nike

这是完整的问题,因为我在上一个问题中问了一点点Get a path value in java file from html/jsp ,这里我基本上将输入文件作为 Excel 工作表,该工作表充当源数据库,并且该文件每天都会更改,因为会发送新的 Excel 工作表。现在我直接使用如下代码。

public class DBConnection {
private Connection conn, conn1;
private Statement stmt;
private ResultSet rs;
private PreparedStatement ps, ps1;
private String excelPath = "D:/MyScouceExcel.xls";
}

但是我想尝试在索引页面中提供一个文件选择器和一个按钮(如下所示),当我选择该文件并点击按钮时,excelPath 应该设置为该路径。

<input type="file" id="inputFile" name="inputFile"/>
<input type="button" value="set"/>

我不想上传文件,只要路径就够了。

在Java类中,自从我研究过Apache-poi以来,我知道如何处理excel数据。 。我需要知道的是我怎样才能通过这条路。请让我知道我该怎么做。

谢谢

最佳答案

我希望下面的代码对您有用。首先你需要一个servlet来读取服务器的文件结构;您可能需要编辑以下代码来根据您的需要进行修复;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = { "/serverpath" })
public class ServerPath extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {

String link = "<li><a href=\"serverpath?path=%s\">%s</a>&nbsp;<input type=\"button\" value=\"select\" onclick=\"window.opener.document.getElementById('path').value = '%s'; window.close();\" /></li>";
response.setContentType("text/html");
try {
String path = req.getParameter("path");
if (path == null) {
// take disk drives, for linux / is enough for me
response.getOutputStream().print(String.format(link, "/", "/", "/"));
} else {
File file = new File(path); //read clicked file path and its sub paths.
if (file.isDirectory()) {
File[] subDir = file.listFiles();
for (File file2 : subDir) {
if (file2.isDirectory()) {
response.getOutputStream().println(
String.format(link, file2.getAbsolutePath(), file2.getAbsolutePath(), file2.getAbsolutePath()));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

html/jsp 页面示例;

<input type="text" id="path" />
<input type="button" value="Select" onclick="window.open('serverpath', '', 'width=700,height=500,top=150,left=150,scrollbars=yes,location=no')">

enter image description here

关于java - 使用文件选择器选择数据输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35838970/

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