我使用jaxrs上传文件*.xlsx。是否可以上传这些文件并在内存中处理它们?我只想读那个文件,用Apache POI处理,并在数据库中保存信息。不保存在服务器
谢谢
很抱歉回复晚了,这个答案可能对某人有所帮助。
是的,可以在内存中读取上传的。xlsx文件的内容[而不将上传的内容保存到物理文件中]。但是这种方法需要更多的内存,因为整个文件内容将存储在缓冲区中。
servlet下面的是一个工作示例,如果需要,您可以稍微修改一下,将此代码转换为JSP。
在创建XSSFWorkbook对象时传递上传文件的输入流,如下所示,您将能够像示例代码一样读取文件内容。
在WEB-INF/lib文件夹中添加apache-poi jar和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="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>