从MultipartFile读取数据,该文件具有从浏览器上传的csv [英] read data from MultipartFile which has csv uploaded from browser

查看:240
本文介绍了从MultipartFile读取数据,该文件具有从浏览器上传的csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能是通过使用MultipartFile上传功能造成的.

May be I am doing it worng by using MultipartFile upload feature.

我必须从csv文件中读取数据,该文件将由客户端通过浏览器选择.我用MultipartFile上传文件.该文件即将发送到控制器,但现在我无法从其中读取csv数据.请指导执行此操作的最佳方法,或帮助我从MultipartFile中读取csv数据. jsp具有

I have to read data from csv file which will be chosen by the client through the browser. I used MultipartFile to upload file. The file is coming to the controller but now I am unable to read csv data from it. Please guide the best way to do it or help me read csv data from MultipartFile. The jsp has

    <form method="POST" action="uploadFile" enctype="multipart/form-data">
            File to upload: <input type="file" name="file"> <input
                type="submit" value="Upload"> Press here to upload the
            file!
        </form>

控制器具有

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFileHandler(@RequestParam("file") MultipartFile file) {

谢谢.

推荐答案

我想出了一种解决方法.我将文件转换为字节,然后将字节转换为String.在String中,我应用了string.split()以从文件中获取想要的内容.

I figured out a workaround. I converted the file to bytes and then converted the bytes to String. From String I applied string.split() to get what I wanted out of the file.

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFileHandler(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();
            String completeData = new String(bytes);
            String[] rows = completeData.split("#");
            String[] columns = rows[0].split(",");

这篇关于从MultipartFile读取数据,该文件具有从浏览器上传的csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆