为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 servlet 上读取 CSV 文件数据,作为表单数据发送

如何在 servlet 上读取 CSV 文件数据,作为表单数据发送

四季花海 2023-05-10 17:10:58
我设计了一个 servlet 来读取 CSV,并且我将 CSV 作为表单数据从邮递员传递,但我无法获得任何读取该数据的选项。我试过下面的代码String body = request.getReader().lines().collect(Collectors.joining());我得到了一个巨大的字符串,但我不知道如何解析这个字符串。----------------------------140780421327043896517058Content-Disposition: form-data; name="CSVfile"; filename="dummyDataSet.csv"Content-Type: text/csvID,Name ,Age,Address,State,Date,Salary1,Airi Satou,33,Tokyo,Tokyo,11/28/2008,"$162,700 "2,Angelica Ramos,47,London,London,10/9/2009,"$162,700 "3,Ashton Cox,66,San Francisco,San Francisco,1/12/2009,"$86,000 "4,Bradley Greer,41,London,London,10/13/2012,"$132,000 "5,Brenden Wagner,28,San Francisco,San Francisco,6/7/2011,"$206,850 "6,Brielle Williamson,61,New York,New York,12/2/2012,"$372,000 "7,Bruno Nash,38,London,London,5/3/2011,"$163,500 "8,Caesar Vance,21,New York,New York,12/12/2011,"$106,450 "9,Cara Stevens,46,New York,New York,12/6/2011,"$145,600 "10,Cedric Kelly,22,Edinburgh,Edinburgh,3/29/2012,"$433,060 "----------------------------140780421327043896517058--你能帮我吗,如何读取CSV数据。
查看完整描述

1 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

使用request.getParts()orrequest.getPart(name)读取多部分内容并向 servlet 添加 @MultipartConfig 注释。


干得好:


import javax.servlet.ServletException;

import javax.servlet.annotation.MultipartConfig;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.Part;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.stream.Collectors;


@WebServlet(urlPatterns = "/upload")

@MultipartConfig

public class MyFileUploadServelt extends HttpServlet {

    private static final String CONTENT_DISPOSITION_KEY = "content-disposition";

    private static final String FILE_NAME_KEY = "filename";

    private static final int BUFFER_SIZE = 2048;


    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("Content Type: " + req.getContentType());

        System.out.println("Content Length: " + req.getContentLength());

        System.out.println("Parts: " + req.getParts().toString());

        System.out.println("Part 1: " + req.getPart("myfile"));

        // Read parts. Also can be read by directly calling req.getPart(fileName)

        for (Part part : req.getParts()) {

            System.out.println("File Name: " + getFileName(part));

            System.out.println("File Content: " + getTextFromPart(part));

        }

    }


    private String getFileName(Part part) {

        for (String contentDisposition : part.getHeader(CONTENT_DISPOSITION_KEY).split(";")) {

            if (contentDisposition.trim().startsWith(FILE_NAME_KEY)) {

                return contentDisposition.substring(contentDisposition.indexOf('=') + 1).trim().replace("\"", "");

            }

        }

        return null;

    }



    private String getTextFromPart(Part part) throws IOException {

        BufferedReader reader =

                new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));

        StringBuilder value = new StringBuilder();

        char[] buffer = new char[BUFFER_SIZE];

        for (int length = 0; (length = reader.read(buffer)) > 0; ) {

            value.append(buffer, 0, length);

        }

        return value.toString();

    }

}



查看完整回答
反对 回复 2023-05-10
  • 1 回答
  • 0 关注
  • 101 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信