java 我想上传 .csv 文件到 servlet 并解析它,但不知道如何打开上传的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/662642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to upload .csv file To servlet and parse it, but dont know how to open uploaded file
提问by Raam
I have a form that allows user to select a .csv file and post it to a servlet. However I am having dificullty getting a handle on the file from the Java Servet on the server any ideas?
我有一个允许用户选择 .csv 文件并将其发布到 servlet 的表单。但是我很难从服务器上的 Java Servet 获取文件的句柄有什么想法吗?
回答by kgiannakakis
I recommend using Apache Commons FileUpload
回答by Vasil
A mistake I make myself and see other people make often is in HTML rather than you serverside. Make sure you have enctype="multipart/form-data" in your form tag.
我自己和看到其他人经常犯的一个错误是在 HTML 中,而不是在服务器端。确保您的表单标签中有 enctype="multipart/form-data"。
If that's ok see this:
如果没问题,请看这个:
回答by Raam
Try OpenCSV for reading and parsing CSV files. I just tried it. It is very easy and quick. It is also with commercial-friendly license. So you can use it your production code as well.
尝试使用 OpenCSV 读取和解析 CSV 文件。我刚试过。这非常简单快捷。它还具有商业友好的许可证。所以你也可以在你的生产代码中使用它。
Check out this link http://opencsv.sourceforge.net/
回答by Mr Lou
you can not open the file you upload.you need to use inputstream to parser the file.and
您无法打开您上传的文件。您需要使用输入流来解析文件。并且
“Make sure you have enctype="multipart/form-data" in your form tag”
“确保你的表单标签中有 enctype="multipart/form-data"”
In java programming,any upload file just can use io to parser that.When you use servlet to parser the file,use request.getInputStream.
在java编程中,任何上传的文件都可以使用io来解析。当你使用servlet解析文件时,使用request.getInputStream。
回答by muralidhar
To Upload your csv file into the any one of the database then you have to save your file in a folder first by browse and selecting the file.then afterwords you have to read all the columns in the csv file using FileInputStream class ...
要将您的 csv 文件上传到任何一个数据库中,您必须首先通过浏览和选择文件将您的文件保存在一个文件夹中。然后,您必须使用 FileInputStream 类读取 csv 文件中的所有列......
By using this code in your program FileInputStream fis=new FileInputStream(pathheader); FileOutputStream fos=new FileOutputStream(filenamehdr);
通过在您的程序FileInputStream 中使用此代码 fis=new FileInputStream(pathheader); FileOutputStream fos=new FileOutputStream(filenamehdr);
for more check here..Upload CSV file using Servlets
在这里查看更多信息..使用 Servlets 上传 CSV 文件

