[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

reading data from a text file in J2EE application

Posted on 2006-07-03
10
Medium Priority
?
611 Views
Last Modified: 2013-11-24
I am working in a J2ee application using Spring MVC framework and Hibernate with websphere application server. I am trying to develop a upload module where user can select a tab delimited text file from his machine and upload data from it into oracle 10g database (our backend). So the user should select the file, the file-type (a drop down menu) and click upload and the data should be in the database. The tab-delimited text file is nothing but excel spreadsheet tables (with first row as header). I have matching columns in the database for each column in the text file. (Note: I have seen the Apache POI framework but want to avoid it, as some of files are coming as text and some as .xsl, so I am converting everything into .txt).

Is there anything in Spring framework that will help me in this. Or else, I am thinking of using simple java IO and use regular expression to extract the data and pump it into hibernate to save it in persistant storage. The other option is perl, but how do I invoke a perl script in websphere app server. A small sample routine will be very helpful whatever the best way to do this.....

You can assume that my text file looks like this:

ColHdr1      ColdHdr2     ColHdr3      ColHdr4   -----> column header
qwe           bbbbb          qqwjxjx      bhjuytg   ------> row #1 data
dgt             aghui          kjiu             kiyrcd           ........
qwsju         kiuyt           juht             juyt              ........
0
Comment
Question by:subirc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17029457
0
 
LVL 35

Expert Comment

by:girionis
ID: 17029461
Simply use the <input type="file"... HTML tag and the file shjould be uploaded. Then you can open the file and use a StringTokenizer in order to get the data you want:

http://javaalmanac.com/egs/java.util/ParseString.html
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html
0
 
LVL 35

Expert Comment

by:girionis
ID: 17029464
And this one to upload the file: http://servlets.com/cos/
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 17029509
0
 

Author Comment

by:subirc
ID: 17029647
objects, how to use ComonsMultipartresolver ...any example? I am new to spring....

subirc
0
 

Author Comment

by:subirc
ID: 17029676
girionis,

Didn't understand your solution. Correct me if I am wrong, but did you mean that I will <input type=file......tag in my upload form which will submit to another servlet (<form name="uploadForm" onSubmit="UploadServlet">......... and in the servlet I can use a StringTokenizer directly using request.getParameter because the uploaded text file will be in the request string??? can u pls elaborate.....
0
 
LVL 35

Expert Comment

by:girionis
ID: 17029740
I mean once you upload the file (like you described) you can save it to a local file on the server. Then you can read it line by line and tokenize on the tokens you want. I am not sure if you can do it online, i.e. without saving the file first, since the stream you will be reading will be binary.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17029743
The commonMultipartResolver is to just upload the file on the server using Spring. You will still need to parse it line by lien and tokenize on its contents. For info on how to use the COmmonMultipartResolver have a look here: http://www.up.ac.za/services/it/intranet/sysops/docs/spring/SpringReference.htm
0
 

Author Comment

by:subirc
ID: 17030170
Girionis,

Okay, so it is a 2 step process; first upload the file to somwhere on the web tree(in the server) and then read it in with a BufferedReader? But then where do I specify the path on the server where the file will be uploaded? What will my upload form submit to?
<form name="UploadForm onSubmit="??????"> . Can u pls show me (pseudocode) what will I have on the jsp and what will i have on the servlet? Just the relevant portion.....

subirc
0
 
LVL 35

Accepted Solution

by:
girionis earned 120 total points
ID: 17030230
subirc,

> But then where do I specify the path on the server where the file will
> be uploaded? What will my upload form submit to?

You specify it in your source code, in the object where you actually do the upload. Here is some sample code: http://www.servlets.com/jservlet2/examples/ch04/UploadTest.java using the O'Reilly package.

If you go with CommonsMultipartResolver have a look here: http://www.up.ac.za/services/it/intranet/sysops/docs/spring/SpringReference.htm#mvc-multipart for information on how you can do it (source code, HTML code and any XML specific code).

> <form name="UploadForm onSubmit="??????"> . Can u pls show me (pseudocode)
> what will I have on the jsp and what will i have on the servlet? Just
> the relevant portion.....

In the HTML form you need to do something like this:

<form method="post" action="http://www.myserver.com/myservlet" enctype="multipart/form-data">
            <input type="file" name="myfilename"/>
            <input type="text" name="anothervariable"/>
            <input type="submit"/>
        </form>

For uploading the file(s) you need the following bit of code (from O'Reilly)

while (files.hasMoreElements()) {
        String name = (String)files.nextElement();
        String filename = multi.getFilesystemName(name);
        String original = multi.getOriginalFileName(name);
        String type = multi.getContentType(name);
        File f = multi.getFile(name);
        out.println();
      }

and to save a file on the disk you can do:

 MultipartRequest multi =
        new MultipartRequest(req, "/tmp", 50 *1024 * 1024,
          new com.oreilly.servlet.multipart.DefaultFileRenamePolicy());

You only have to change the "/tmp" folder to the one you want to save the files to.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question