how to read csv file which should not contains “” or ' or new line and then insert the modified csv into...
i have a csv file which i should read using apache poi,while reading the file it should follow some pattern like data should not have ' or '' or new line like that.After validating we need to insert the validated csv into db.The code which i wrote for this is below.
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public void uploadData(@RequestParam("file") final MultipartFile DataFile,
@PathVariable("DataType") final String DataType,
final Model model, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
byte bytes = null;
InputStream inputStream = null;
if (DataFile != null && !DataFile.isEmpty()) {
inputStream = DataFile.getInputStream();
LOGGER.info("Making Service call to save imported Enrichment details in DB ");
if (StringUtils.equalsIgnoreCase(DataType, "csvData1")) {
bytes = enrichmentDataFile.getBytes();
inputStream = new ByteArrayInputStream(Pattern.compile("(\r"|\n"|\r"\n"|"|')+")
.matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")));
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
} else if (StringUtils.equalsIgnoreCase(DataType, "csvData2")) {
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
}
}
}
iam able to insert csvData2 into db but when iam trying to insert csvData1,it was creating an empty file and that file was inserting into db.
can anyone suggest How can i validate the inputstream(scv) without having any ' or " or new lines and then insert validated one into db
java regex apache-poi
add a comment |
i have a csv file which i should read using apache poi,while reading the file it should follow some pattern like data should not have ' or '' or new line like that.After validating we need to insert the validated csv into db.The code which i wrote for this is below.
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public void uploadData(@RequestParam("file") final MultipartFile DataFile,
@PathVariable("DataType") final String DataType,
final Model model, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
byte bytes = null;
InputStream inputStream = null;
if (DataFile != null && !DataFile.isEmpty()) {
inputStream = DataFile.getInputStream();
LOGGER.info("Making Service call to save imported Enrichment details in DB ");
if (StringUtils.equalsIgnoreCase(DataType, "csvData1")) {
bytes = enrichmentDataFile.getBytes();
inputStream = new ByteArrayInputStream(Pattern.compile("(\r"|\n"|\r"\n"|"|')+")
.matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")));
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
} else if (StringUtils.equalsIgnoreCase(DataType, "csvData2")) {
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
}
}
}
iam able to insert csvData2 into db but when iam trying to insert csvData1,it was creating an empty file and that file was inserting into db.
can anyone suggest How can i validate the inputstream(scv) without having any ' or " or new lines and then insert validated one into db
java regex apache-poi
add a comment |
i have a csv file which i should read using apache poi,while reading the file it should follow some pattern like data should not have ' or '' or new line like that.After validating we need to insert the validated csv into db.The code which i wrote for this is below.
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public void uploadData(@RequestParam("file") final MultipartFile DataFile,
@PathVariable("DataType") final String DataType,
final Model model, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
byte bytes = null;
InputStream inputStream = null;
if (DataFile != null && !DataFile.isEmpty()) {
inputStream = DataFile.getInputStream();
LOGGER.info("Making Service call to save imported Enrichment details in DB ");
if (StringUtils.equalsIgnoreCase(DataType, "csvData1")) {
bytes = enrichmentDataFile.getBytes();
inputStream = new ByteArrayInputStream(Pattern.compile("(\r"|\n"|\r"\n"|"|')+")
.matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")));
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
} else if (StringUtils.equalsIgnoreCase(DataType, "csvData2")) {
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
}
}
}
iam able to insert csvData2 into db but when iam trying to insert csvData1,it was creating an empty file and that file was inserting into db.
can anyone suggest How can i validate the inputstream(scv) without having any ' or " or new lines and then insert validated one into db
java regex apache-poi
i have a csv file which i should read using apache poi,while reading the file it should follow some pattern like data should not have ' or '' or new line like that.After validating we need to insert the validated csv into db.The code which i wrote for this is below.
@RequestMapping(value = "/insert", method = RequestMethod.POST)
public void uploadData(@RequestParam("file") final MultipartFile DataFile,
@PathVariable("DataType") final String DataType,
final Model model, final HttpServletRequest request,
final HttpServletResponse response) throws Exception {
byte bytes = null;
InputStream inputStream = null;
if (DataFile != null && !DataFile.isEmpty()) {
inputStream = DataFile.getInputStream();
LOGGER.info("Making Service call to save imported Enrichment details in DB ");
if (StringUtils.equalsIgnoreCase(DataType, "csvData1")) {
bytes = enrichmentDataFile.getBytes();
inputStream = new ByteArrayInputStream(Pattern.compile("(\r"|\n"|\r"\n"|"|')+")
.matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")));
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
} else if (StringUtils.equalsIgnoreCase(DataType, "csvData2")) {
DataService.insertData(inputStream,
DataFile.getOriginalFilename());//reading data using Apache POI and inserting into db
}
}
}
iam able to insert csvData2 into db but when iam trying to insert csvData1,it was creating an empty file and that file was inserting into db.
can anyone suggest How can i validate the inputstream(scv) without having any ' or " or new lines and then insert validated one into db
java regex apache-poi
java regex apache-poi
edited Nov 14 '18 at 7:26
deHaar
2,42941628
2,42941628
asked Nov 14 '18 at 7:17
vyasvyas
115
115
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your InputStream mutation works perfectly. Problem should be in DataService.insertData method.
if there is problem with my service method then csvData2 should not get inserted but it was working fine the pblm is there with csvData1 that too at that line only
– vyas
Nov 14 '18 at 9:00
your service inserted data with quotes and double quotes, but not without it. Try siply send on your service new ByteArrayInputStream("blablabla".getBytes()). I suppose you will get also empty file. Maybe your POI parser requires quotes.
– Wladimir Diskowski
Nov 14 '18 at 9:05
same again getting empty file in this approach also
– vyas
Nov 14 '18 at 9:09
And if you try new ByteArrayInputStream(" 'blablabla' ".getBytes()) or new ByteArrayInputStream(" "blablabla" ".getBytes()) content will be probably not empty
– Wladimir Diskowski
Nov 14 '18 at 9:12
@Wladinir thats what i am doing in my code .matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")))
– vyas
Nov 14 '18 at 9:24
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294921%2fhow-to-read-csv-file-which-should-not-contains-or-or-new-line-and-then-inse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your InputStream mutation works perfectly. Problem should be in DataService.insertData method.
if there is problem with my service method then csvData2 should not get inserted but it was working fine the pblm is there with csvData1 that too at that line only
– vyas
Nov 14 '18 at 9:00
your service inserted data with quotes and double quotes, but not without it. Try siply send on your service new ByteArrayInputStream("blablabla".getBytes()). I suppose you will get also empty file. Maybe your POI parser requires quotes.
– Wladimir Diskowski
Nov 14 '18 at 9:05
same again getting empty file in this approach also
– vyas
Nov 14 '18 at 9:09
And if you try new ByteArrayInputStream(" 'blablabla' ".getBytes()) or new ByteArrayInputStream(" "blablabla" ".getBytes()) content will be probably not empty
– Wladimir Diskowski
Nov 14 '18 at 9:12
@Wladinir thats what i am doing in my code .matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")))
– vyas
Nov 14 '18 at 9:24
|
show 2 more comments
Your InputStream mutation works perfectly. Problem should be in DataService.insertData method.
if there is problem with my service method then csvData2 should not get inserted but it was working fine the pblm is there with csvData1 that too at that line only
– vyas
Nov 14 '18 at 9:00
your service inserted data with quotes and double quotes, but not without it. Try siply send on your service new ByteArrayInputStream("blablabla".getBytes()). I suppose you will get also empty file. Maybe your POI parser requires quotes.
– Wladimir Diskowski
Nov 14 '18 at 9:05
same again getting empty file in this approach also
– vyas
Nov 14 '18 at 9:09
And if you try new ByteArrayInputStream(" 'blablabla' ".getBytes()) or new ByteArrayInputStream(" "blablabla" ".getBytes()) content will be probably not empty
– Wladimir Diskowski
Nov 14 '18 at 9:12
@Wladinir thats what i am doing in my code .matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")))
– vyas
Nov 14 '18 at 9:24
|
show 2 more comments
Your InputStream mutation works perfectly. Problem should be in DataService.insertData method.
Your InputStream mutation works perfectly. Problem should be in DataService.insertData method.
answered Nov 14 '18 at 8:40
Wladimir DiskowskiWladimir Diskowski
586
586
if there is problem with my service method then csvData2 should not get inserted but it was working fine the pblm is there with csvData1 that too at that line only
– vyas
Nov 14 '18 at 9:00
your service inserted data with quotes and double quotes, but not without it. Try siply send on your service new ByteArrayInputStream("blablabla".getBytes()). I suppose you will get also empty file. Maybe your POI parser requires quotes.
– Wladimir Diskowski
Nov 14 '18 at 9:05
same again getting empty file in this approach also
– vyas
Nov 14 '18 at 9:09
And if you try new ByteArrayInputStream(" 'blablabla' ".getBytes()) or new ByteArrayInputStream(" "blablabla" ".getBytes()) content will be probably not empty
– Wladimir Diskowski
Nov 14 '18 at 9:12
@Wladinir thats what i am doing in my code .matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")))
– vyas
Nov 14 '18 at 9:24
|
show 2 more comments
if there is problem with my service method then csvData2 should not get inserted but it was working fine the pblm is there with csvData1 that too at that line only
– vyas
Nov 14 '18 at 9:00
your service inserted data with quotes and double quotes, but not without it. Try siply send on your service new ByteArrayInputStream("blablabla".getBytes()). I suppose you will get also empty file. Maybe your POI parser requires quotes.
– Wladimir Diskowski
Nov 14 '18 at 9:05
same again getting empty file in this approach also
– vyas
Nov 14 '18 at 9:09
And if you try new ByteArrayInputStream(" 'blablabla' ".getBytes()) or new ByteArrayInputStream(" "blablabla" ".getBytes()) content will be probably not empty
– Wladimir Diskowski
Nov 14 '18 at 9:12
@Wladinir thats what i am doing in my code .matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")))
– vyas
Nov 14 '18 at 9:24
if there is problem with my service method then csvData2 should not get inserted but it was working fine the pblm is there with csvData1 that too at that line only
– vyas
Nov 14 '18 at 9:00
if there is problem with my service method then csvData2 should not get inserted but it was working fine the pblm is there with csvData1 that too at that line only
– vyas
Nov 14 '18 at 9:00
your service inserted data with quotes and double quotes, but not without it. Try siply send on your service new ByteArrayInputStream("blablabla".getBytes()). I suppose you will get also empty file. Maybe your POI parser requires quotes.
– Wladimir Diskowski
Nov 14 '18 at 9:05
your service inserted data with quotes and double quotes, but not without it. Try siply send on your service new ByteArrayInputStream("blablabla".getBytes()). I suppose you will get also empty file. Maybe your POI parser requires quotes.
– Wladimir Diskowski
Nov 14 '18 at 9:05
same again getting empty file in this approach also
– vyas
Nov 14 '18 at 9:09
same again getting empty file in this approach also
– vyas
Nov 14 '18 at 9:09
And if you try new ByteArrayInputStream(" 'blablabla' ".getBytes()) or new ByteArrayInputStream(" "blablabla" ".getBytes()) content will be probably not empty
– Wladimir Diskowski
Nov 14 '18 at 9:12
And if you try new ByteArrayInputStream(" 'blablabla' ".getBytes()) or new ByteArrayInputStream(" "blablabla" ".getBytes()) content will be probably not empty
– Wladimir Diskowski
Nov 14 '18 at 9:12
@Wladinir thats what i am doing in my code .matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")))
– vyas
Nov 14 '18 at 9:24
@Wladinir thats what i am doing in my code .matcher(new String(bytes)).replaceAll("").getBytes(Charset.forName("UTF-8")))
– vyas
Nov 14 '18 at 9:24
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294921%2fhow-to-read-csv-file-which-should-not-contains-or-or-new-line-and-then-inse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown