[英]Is possible to upload a file.xlsx, process it without save it in server? I am using jaxrs
I'm using jaxrs for uploading files *.xlsx. 我正在使用jaxrs上载文件* .xlsx。 Is possible to upload this files and process them in memory? 是否可以上传此文件并在内存中处理它们? I only want read that file, Process with Apache POI and save information at DataBase. 我只想读取该文件,使用Apache POI处理并将信息保存在数据库中。 Without save them in server 不将它们保存在服务器中
Thanks 谢谢
sorry for late reply, this answer may help somebody. 抱歉,您的回复很晚,此答案可能会有所帮助。
Yes, It is possible to read the content of an uploaded .xlsx file in memory [without saving the uploaded content to a physical file]. 是的,可以在内存中读取上载的.xlsx文件的内容[无需将上载的内容保存到物理文件中]。 but this approach require more memory, as the entire file content will be stored in buffer. 但是这种方法需要更多的内存,因为整个文件内容都将存储在缓冲区中。
below servlet, is a working example for this, with slight modification you can convert this code into JSP, if needed. servlet下面是一个有效的示例,只需稍作修改,您就可以将该代码转换为JSP(如果需要)。
pass the uploaded file's inputstream while creating XSSFWorkbook object, as shown below, you will be able to read the file content as in the example code. 在创建XSSFWorkbook对象时传递上传文件的输入流,如下所示,您将能够如示例代码中那样读取文件内容。
add apache-poi jars and apache commons-fileupload.jar in the WEB-INF/lib folder for the correct working of this code. 在WEB-INF / lib文件夹中添加apache-poi jars和apache commons-fileupload.jar,以使此代码正常工作。
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.List;
import java.util.Iterator;
@WebServlet("/FileUploadServlet")
public class FileUploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
OPCPackage pkg = null;
XSSFWorkbook xlsxbook = null;
InputStream xlsxContentStream = null;
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
List<FileItem> items = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = FilenameUtils.getName(item.getName());
xlsxContentStream = item.getInputStream();
pkg = OPCPackage.open(xlsxContentStream);
xlsxbook = new XSSFWorkbook(pkg);
XSSFSheet sheet = xlsxbook.getSheetAt(0);
Iterator<Row> itr = sheet.iterator();
while (itr.hasNext()) {
Row row = itr.next();
// Iterating over each column of Excel file
Iterator<Cell> cellIterator = row.cellIterator();
String text = "";
double num = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
text = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
num = cell.getNumericCellValue();
break;
default:
}
}
out.print(text + " " + num);
//call database insert method here and pass the xlsx column values
}
}
}
}
out.flush();
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
} catch (Exception e) {
throw new ServletException("", e);
} finally {
if(null!=xlsxContentStream){ xlsxContentStream.close();}
if(null!=pkg){ pkg.close();}
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
your jsp/html file should have enctype="multipart/form-data" in the form 您的jsp / html文件的格式应为enctype =“ multipart / form-data”
<form action="FileUploadServlet" name="form2" method="post" enctype="multipart/form-data">
Select a .xlsx File to upload :<input type="file" name="file" id="file" size="50" />
<input type="submit" value="Upload xlsx File"/>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.