• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert String to File object  RSS feed

 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day!

I have a code that lists the contents of a directory.



I after listing the filenames, I want them to be converted to Files or File Object. And save it to the database. Is it possible?

I have tried this one and append it to the code above but it didn't work:



Stack trace:


java.lang.NullPointerException
at org.apache.jsp.HttpUploadDB_jsp._jspService(HttpUploadDB_jsp.java:148)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
at java.lang.Thread.run(Thread.java:595)
java.lang.NullPointerException


Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is this, all in a JSP page? I recommend that you move this code into a class so you can test it better. In particular, what line is that error message coming from?

To get a File object rather than a String filename, you should probably include the parent directory:

Or to make this easier, just use listFiles() rather than list() in the first place.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your reply. Yes, I will move my codes in servlets within the week but as of now I placed it in JSPs for testing purposes.

I followed your advice and modified the code:



But I am not able to iterate it in here.

filename = files[i];

Is says, "Cannot convert from File to String".

Thanks again.

Darren
[ September 10, 2007: Message edited by: Darren Alexandria ]
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
filename = files[i];

Is filename a String? If yes, then it definitely won't work. You can't assigned a File object into a String object. If you want to get the file name, you can do this:



I want them to be converted to Files or File Object. And save it to the database.

You shouldn't do that. If you want to save the files into the database, you should save the content of the file and not the File object.
[ September 10, 2007: Message edited by: Freddy Wong ]
 
mohsen fakhari
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as jim said it is hard to say what causes this NullPointerException before you move the code
into a separate class.Or at least you can insert some try/catch statements.
And as I understood all what you need is the file name(the last part of complete path) and
its size,right?
Isn't it better to define children like this:

File[] children = dir.listFiles();

And then you can print children[i].getName() in your loop and use chldren[i].length() when
you need.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thanks to your replies. I was really enlightened. All the while I thought that I can get the contents of the file using File Object. Actually, what I am trying to do is this: The client uploads files to the server's file system. After that, those will will be retrieved and then be uploaded to the database.
Is that sensible? or Can I do it in from Client and save it straight to the database? I have made some code with that process using HttpClient and FileUpload but it always ends up with "File Not Found".

Please give me advice.
Thanks.

Darren
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!