简体   繁体   English

是否可以上传file.xlsx,进行处理而不将其保存在服务器中? 我正在使用jaxrs

[英]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.

相关问题 我怎么知道我正在使用什么 jaxrs 实现? - How do I know what implementation of jaxrs I am using? 使用 PDFBox 保存方法将文件上传到 SFTP 服务器而不将文件存储到本地系统? - Upload a file to an SFTP server using PDFBox save method without storing the file to the local system? 将文件上传到服务器,进行处理,创建新文件(txt文件)并提示保存 - Upload a file to server, process it, create a new file (txt file) and prompt for save JaxRS 从服务器创建并返回 zip 文件 - JaxRS create and return zip file from server 我可以使用 FileOutputStream 在服务器上保存文件吗? - Can I save a file on server using FileOutputStream? 使用Apache CXF上传XLSX或ZIP文件会损坏文件 - Upload XLSX or ZIP file using Apache CXF corrupts the file 我正在尝试使用mulesoft通过Java代码将文件上传到文件夹,找不到404文件错误 - I am trying upload a file using mulesoft via Java code to a folder getting 404 file not found error 我必须使用Java swing将文件上传到服务器 - I have to upload a file to the server using a java swing 在java中上传图像并将其保存在服务器上。 没有 HTML5 - upload image in java and save it on server. Without Html5 如何在postgres中节省没有时区的时间。 我正在使用休眠的Spring MVC - how to save time without time zone in postgres . i am using hibernate Spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM