我正在使用jaxrs上传文件* .xlsx。是否可以上传此文件并在内存中处理它们?我只想读取该文件,使用Apache POI处理并在DataBase中保存信息。 不在服务器中保存
由于
答案 0 :(得分:1)
抱歉迟到的回复,这个回答可能对某人有帮助。
是的,可以在内存中读取上传的.xlsx文件的内容[不将上传的内容保存到物理文件]。但这种方法需要更多内存,因为整个文件内容将存储在缓冲区中。
在servlet下面的是一个很好的例子,如果需要,可以稍加修改,将这段代码转换成JSP。
在创建XSSFWorkbook对象时传递上传文件的输入流,如下所示,您将能够像示例代码一样读取文件内容。
在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);
}
}
你的jsp / html文件应该有enctype =&#34; multipart / form-data&#34;在表格中
<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>