gpt4 book ai didi

java - 浏览并选择 Excel 文件并在 java 中从中创建工作簿对象

转载 作者:行者123 更新时间:2023-12-02 02:43:11 24 4
gpt4 key购买 nike

嗨,我想从我的系统中选择一个文件并创建工作簿对象。我在下面写了提供代码来做到这一点。正如您所看到的,我将该文件写入某个临时位置,然后从该临时副本创建 Workbook 对象。但是,我想创建 Workbook 对象而不创建它的副本

<form action="UploadDownloadFileServlet" method="post" enctype="multipart/form-data">
Select File to Upload:<input type="file" name="fileName"><br>
<input type="submit" value="Upload"/>
</form>

doPost() 代码片段

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!ServletFileUpload.isMultipartContent(request)) {
throw new ServletException("Content type is not multipart/form-data");
}

PrintWriter out = response.getWriter();
try {
List<FileItem> fileItemsList = uploader.parseRequest(request);

FileItem fileItem = fileItemsList.get(0);
System.out.println("FieldName=" + fileItem.getFieldName());
System.out.println("FileName=" + fileItem.getName());
System.out.println("ContentType=" + fileItem.getContentType());
System.out.println("Size in bytes=" + fileItem.getSize());
File file = new File(File.separator + fileItem.getName());

System.out.println("Absolute Path at server=" + file.getAbsolutePath());
fileItem.write(file);
FileInputStream inputStream = new FileInputStream(file);
Workbook workbook = getWorkbook(inputStream, file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}

}

监听器类

@WebListener
public class FileLocationContextListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent servletContextEvent) {
String rootPath = System.getProperty("catalina.home");
ServletContext ctx = servletContextEvent.getServletContext();
String relativePath = ctx.getInitParameter("tempfile.dir");
File file = new File(rootPath + File.separator + relativePath);
if(!file.exists()) file.mkdirs();
System.out.println("File Directory created to be used for storing files");
ctx.setAttribute("FILES_DIR_FILE", file);
ctx.setAttribute("FILES_DIR", rootPath + File.separator + relativePath);
}

public void contextDestroyed(ServletContextEvent servletContextEvent) {
//do cleanup if needed
}

}

最佳答案

最后经过一点研究......我能够实现我的目标......

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,  IOException {
if (!ServletFileUpload.isMultipartContent(request)) {
throw new ServletException("Content type is not multipart/form-data");
}

PrintWriter out = response.getWriter();
try {
List<FileItem> fileItemsList = uploader.parseRequest(request);

FileItem fileItem = fileItemsList.get(0);
System.out.println("FieldName=" + fileItem.getFieldName());
System.out.println("FileName=" + fileItem.getName());
System.out.println("ContentType=" + fileItem.getContentType());
System.out.println("Size in bytes=" + fileItem.getSize());
InputStream inputStream=fileItem.getInputStream();
Workbook workbook;
if (fileItem.getName().endsWith("xlsx")) {
workbook = new XSSFWorkbook(inputStream);
} else if (fileItem.getName().endsWith("xls")) {
workbook = new HSSFWorkbook(inputStream);
} else {
throw new IllegalArgumentException("The specified file is not Excel file");
}

} catch (Exception e) {
e.printStackTrace();
}

}

关于java - 浏览并选择 Excel 文件并在 java 中从中创建工作簿对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45088891/

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