如何仅使用Java将csv文件上传到服务器 [英] How to upload only csv files into the server in Java

查看:1208
本文介绍了如何仅使用Java将csv文件上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要上传csv文件。



一切正常。我正在使用扩展名 .csv 成功上传文件。



现在这里是问题。有人可能会尝试通过将其扩展名更改为 .csv 来上传 exe 文件并上传到服务器。这是完美上传。有人建议我使用 mimetype 上传文件。我遵循教程。



我得到mimetype null for csv files。



感谢提前

解决方案

好吧,你是在正确的轨道。您需要进行某种文件检查,而不仅仅是信任文件扩展名。



您必须编写自己的文件,或使用第三方库你在做)。



如果你使用第三方框架,你必须有一个配置问题。



如果你只是想识别CSV文件,我会推荐一些东西简单,至少你明白和可以控制。例如这种方法:



假设CSV文件由0..n行与相同数量的元素pr组成。行,由以下字符之一分隔:,;



EXE文件不太可能具有这种结构,除非有人用你的实现知识来故意攻击你的系统。



阅读前两行。



在regexp([,;])上分割它们。



检查结果是否具有相同数量的元素。



为了更加安全,请检查它们是否以相同的字符拆分。


I have an application in which there is a requirement of Uploading csv files.

Everything is working fine. I am uploading files successfully with extension .csv.

Now here is the problem. someone may try to upload exe file by changing its extension to .csv and upload to server. It is perfectly uploading. Some one suggested me to upload file by using mimetype. I follow this tutorial.

I am getting mimetype null for csv files.

So can you please help me how to upload only csv files.

Thanks in Advance

解决方案

Well, you are ont he right track. You need to do some kind of file inspection instead of just trusting the file extension.

Either you have to program your own, or use a third party library (which is what you are doing).

If you keep with the third party framework, you must eb having a configuration issue. What the issue is I can't tell from your description, as I don't know the library.

If you are only interested in recognizing CSV files I would recommend something simple that at least you understand and can control. Such as this approach:

Assume that CSV files consist of 0..n lines with the same number of elements pr. line, separated by one of these characters: , ;

It's quite unlikely that an EXE file would have this structure, unless somebody deliberately attack your system with knowledge of your implementation.

Read the first couple of lines.

Split them on the regexp ([,;]).

Check that the results have the same number of elements.

For extra safety, check that they split on the same character.

这篇关于如何仅使用Java将csv文件上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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